• rename all files in folder (vb6 or vba)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » rename all files in folder (vb6 or vba)

    Author
    Topic
    #358842

    I have to rename approx 600 files with a prefix in a number of folders. what is the best way to do this?
    surely opening the files and then saving the files with a new name will take too long ?
    Is there a funky VB function that i can use to carry out this change to every file in chosen folders ?
    It’s this or a lot of ……F2…..Home…..Ctrl+V……enter …….. repeat ! grovel

    Viewing 3 reply threads
    Author
    Replies
    • #536373

      I’m thinking the FileSystemObject might do the trick. Unfortunately, I don’t see any method to rename files which would mean you’d need to copy to the new name and delete the old file.

    • #536375

      You canuse teh the VBA statement NAME to rename files as in Name OldName As NewName.

      You could loop through your directory, incrementing your prefix as you do so, and rename each file.

      Look for Name in VBA help for more details.

      Andrew C

      • #536508

        Just what i needed, teamed up with the filesystemobject – job done
        Thank you

    • #536402

      try …

      Dim i As Integer
      Dim fso As Object
      Dim lsFileName As String
      Dim lvFile As Variant
      Dim lsPath As String
      Dim lsNewFileName As String

      Set fso = CreateObject(“Scripting.FileSystemObject”)

      lsPath = “C:Directory
      lsFileName = Dir(lsPath & “*.*”)
      If Not lsFileName = “” Then
      lsNewFileName = ‘formula based renaming method’ End If

      Do Until lsFileName = “” ‘ get the file and copy it
      Set lvFile = fso.getfile(lsPath & lsFileName)

      ‘ rename file
      lvFile.Name = lsNewFileName

      ‘ get next file name
      lsFileName = Dir
      If Not lsFileName = “” Then
      lsNewFileName = ‘formula based renaming method’
      End If
      Loop

    • #536446

      I have adopted a file naming convention for my group where we name files with the data at the beginning, such as “99-01-01 blah.doc”. This created an interesting “Y2K” problem for me, since I wanted the 99 files to sort before the 2000 files. So I wrote a procedure to rename all of the files with a 19 prefix. It included a UserForm interface to drill down through your folder structure to find a target folder, and the option to include all of its subfolders. You will need to make some adjustments in the criteria, but hopefully this will give you a headstart.

      • #536510

        Thanks for this, I am suffering from living in the UK. I spent last night at home working on this problem and then get to work this morning with any number of working examples – anyway, useful code from everyone and lots that i can use, thanks again!

    Viewing 3 reply threads
    Reply To: rename all files in folder (vb6 or vba)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: