Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    rename all files in folder (vb6 or vba)

    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 ! <img src=/S/grovel.gif border=0 alt=grovel width=31 height=23>

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: rename all files in folder (vb6 or vba)

    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.

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: rename all files in folder (vb6 or vba)

    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

  4. #4
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: rename all files in folder (vb6 or vba)

    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 = "Cirectory
    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
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: rename all files in folder (vb6 or vba)

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

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: rename all files in folder (vb6 or vba)

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

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: rename all files in folder (vb6 or vba)

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •