We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Renaming Files

Last post 05-01-2008 8:12 AM by itsmerajbhatia. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 05-18-2005 10:46 PM

    Renaming Files

    Hi,

    I was wondering how to rename a certain file using vb6? can anybody help?

    moon
    • Post Points: 25
  • 05-18-2005 11:22 PM In reply to

    • denis
    • Top 100 Contributor
    • Joined on 02-15-2005
    • Fanatic Member
    • Points 1,125

    hoe rename files in visual basic


    I assume you mean a file other than one of your project file ? If so I suggest you try this

    Dim OldName, NewName
    OldName = "OLDFILE": NewName = "NEWFILE"    ' Define filenames.
    Name OldName As NewName    ' Rename file.

    OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
    Name OldName As NewName    ' Move and rename file.

    It works in VB5 so you may need to tweek for VB6

    Denis
    • Post Points: 5
  • 05-18-2005 11:31 PM In reply to

    • hollystyles
    • Top 150 Contributor
    • Joined on 03-22-2005
    • United Kingdom
    • Fanatic Member
    • Points 575

    Embarrassed [:$] hoe rename files in visual basic

    use the MoveFile method of the Scripting.FileSystemObject.
    You'll need to set a reference to the scripting library in your project

    Code:

           Dim oFSO As FileSystemObject
       Dim sSourceFile As String
       Dim sDestinationFile As String

       Set oFSO = CreateObject("Scripting.FileSystemObject")

       sSourceFile = "C:\SourceFile.txt"
       sDestinationFile = "C:\DestinationFile.txt"

       oFSO.MoveFile sSourceFile, sDestinationFile

           Set oFso = Nothing
    Holly
    ------------------------------------
    • Post Points: 10
  • 05-19-2005 6:52 AM In reply to

    • INDIKA
    • Not Ranked
    • Joined on 05-19-2005
    • New Member
    • Points 5

    Angel [A] hoe rename files in visual basic

    i'm a begginer to the programming thats why i choose the visual basic programming . i know some basic functions. i what to rename the files using vb
    • Post Points: 5
  • 05-19-2005 9:36 AM In reply to

    • hollystyles
    • Top 150 Contributor
    • Joined on 03-22-2005
    • United Kingdom
    • Fanatic Member
    • Points 575

    Smiley Face [:)] hoe rename files in visual basic

    Yep!

    See that code just above your post ?

    That's vb that is, and it renames files !!

    Holly
    ------------------------------------
    • Post Points: 5
  • 05-19-2005 7:42 PM In reply to

    • Shmosel
    • Top 150 Contributor
    • Joined on 02-13-2004
    • Fanatic Member
    • Points 940

    hoe rename files in visual basic

    i don't know if there's actually a rename function. what i usually do is :
    Code:
    FileCopy "C:\Path\Source.txt", "C:\Path\Destination.txt"

    to copy the file, then:
    Code:
    Kill "C:\Path\Source.txt"

    to delete the old file.


    Shmosel
    • Post Points: 5
  • 05-19-2005 8:34 PM In reply to

    Smiley Face [:)] hoe rename files in visual basic

    Hi

    sorry denis as your response is a bit confusing for me as i am a beginner.

    However, i understood hollystyles & shmosel's reply. Actually their code demonstrates renaming a txt file while i need to rename an .mdb file.

    I'll try both methods out...

    Thank you EVERY ONE  for quick response.

    moon
    • Post Points: 5
  • 04-30-2008 1:12 PM In reply to

    Re: hoe rename files in visual basic

    The above code is useful when u know the name of the source file.. my source file name changes everyday..the file name changes as per the date like "source file 30-4", "source file 1-5"... i want to change this filename to "source file"....so that the subsequent code that i have written can run. Plz help....
    • Post Points: 10
  • 04-30-2008 1:35 PM In reply to

    • hollystyles
    • Top 150 Contributor
    • Joined on 03-22-2005
    • United Kingdom
    • Fanatic Member
    • Points 575

    Re: hoe rename files in visual basic

    Man!! do you know how old this thread is ??? 

     The files are named with a known scheme, you just need to grab the system date to work out what the file name is going to be and concatenate it together.
     

    Dim oFSO As FileSystemObject
    Dim f As File
    Dim sSourceFile As String
    Dim sysDate As Date

    sysDate = now()
    sSourceFile = "Source file  " & DatePart(DateInterval.Day,sysDate ) & "-" & DatePart(DateInterval.Month, sysDate )
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    f = oFSO.GetFile(sSourceFile)

    'Your file processing code follows ...


    Holly
    ------------------------------------
    • Post Points: 15
  • 04-30-2008 4:58 PM In reply to

    • Jugatsu
    • Top 500 Contributor
    • Joined on 02-15-2006
    • Member
    • Points 365

    Re: hoe rename files in visual basic

    If there is no naming convention, as hollystyles mentioned
    You maybe can loop through the directory to find the latest updated file

    Private Function FindLatestFileinDir(ByVal sDir As String) As String
        Dim oFSO
        Dim f, fld, tFil
        Dim ldate As Date
        Dim sFile As String
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set fld = oFSO.GetFolder(sDir)
        For Each tFil In fld.Files
            Set f = oFSO.GetFile(tFil)
            If ldate < f.DateLastModified Then
                ldate = f.DateLastModified
                sFile = tFil
            End If
        Next
        Return sFile
    End Function

    • Post Points: 5
  • 05-01-2008 8:12 AM In reply to

    Re: hoe rename files in visual basic

    i dont knw p of programming thats y i googled my doubt and got directed to the thread..i was hoping sm1 wud reply and u did...thks! this the code that i have collated from various sources Sub Macro5() ' ' Macro5 Macro ' ' Dim ObjFso Dim StrSourceLocation Dim StrDestinationLocation Dim StrSourceFileName As String Dim sysDate As Date Dim StrDestinationFileName StrSourceLocation = "C:\" StrDestinationLocation = "C:\" StrSourceFileName = "Dispatches " & DatePart(DateInterval.Day, sysDate) & "-" & DatePart(DateInterval.Month, sysDate) StrDestinationFileName = "Dispatches.xls" 'Creating the file system object Set ObjFso = CreateObject("Scripting.FileSystemObject") 'Copying the file ObjFso.CopyFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\" & StrDestinationFileName, False End Sub StrSourceFileName = "Dispatches " & DatePart(DateInterval.Day, sysDate) & "-" & DatePart(DateInterval.Month, sysDate) has a problem...it says object not found... my source file is dipsatches 1-5.xls and my destination file (which is used for further code processing) is dispatches.xls.... plz suggest the changes i need to make in the code.. thanks a lot!
    • Post Points: 5
Page 1 of 1 (11 items)