Community discussion forum

Renaming Files

  • 4 years ago

    Hi,


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


    moon

  • 4 years ago


    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

  • 4 years ago

    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

  • 4 years ago

    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

  • 4 years ago

    Yep!


    See that code just above your post ?


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


  • 4 years ago

    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

  • 4 years ago

    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

  • 1 year ago
    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....
  • 1 year ago

    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 ...


  • 1 year ago

    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

  • 1 year ago
    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 a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback