Renaming Files

  • 15 years ago

    Hi,


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


    moon

  • 15 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

  • 15 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

  • 15 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

  • 15 years ago

    Yep!


    See that code just above your post ?


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


  • 15 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

  • 15 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

  • 12 years 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 ...


  • 12 years 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

  • 9 years ago

    Hi, I've spent a long time looking on the net and worked out how to used Visual Basic in excel to remame a bunch of files, you need to have the file names in the excel spreadsheet though, and it doesn't copy the files, just replaces the names...make sure you keep a back up! here's the 'code' i used:

    'Visual Basic 6.5 renaming txt files

    Sub RenameFiles1() Dim SourceName As String Dim DestName As String Dim row As Long

    ChDir "C:\Test_folder" 'Folder where all files are located
    
    For row = 2 To 4 'My new filenames are in rows 2 to 4 of the spreadheet list
    
    
    SourceName = ActiveSheet.Cells(row, 1).Value 'old file names in the spreadsheet list
    DestName = ActiveSheet.Cells(row, 2).Value 'new file names in the spreadsheet list
    
    
    Name SourceName & ".txt" As DestName & ".txt" 'txt can be changed for jpg for example
    
    
    Next row
    

    End Sub

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”