# Renaming all the files in a folder??



## Red2034 (Apr 17, 2007)

I have 10 or so files in a folder:

folder\file_01_01_01.swf
folder\file_01_01_02.swf
folder\file_01_01_03.swf
folder\file_01_01_04.swf
...

i would like to rename these files:

folder\newfilename_01_01_01.swf
folder\newfilename_01_01_02.swf
folder\newfilename_01_01_03.swf
folder\newfilename_01_01_04.swf
....


I have the code to change each file seperately, 

' Sub Rename()
' Dim FileName As String
' Dim NewFileName As String
' On Error Resume Next

' FileName = "C:\folder\file_01_01_01.swf"
' NewFileName = "C:\folder\newfilename01_01_01.swf"
' Name FileName As NewFileName
' End Sub

but i was hoping for a slick way to do it all...


any help...


----------



## WhitPhil (Oct 4, 2000)

How about a rename utility?


----------



## Red2034 (Apr 17, 2007)

I would like to learn how to do this...


----------



## Zack Barresse (Jul 25, 2004)

Hi Red,

Yeah, this can be done fairly easily with VBS...


```
Dim fso, fsoFolder, fsoFile, strPath, strName
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFolder = fso.GetFolder("C:\Documents and Settings\Zachary\Desktop\Test\")
For Each fsoFile In fsoFolder.Files
    strName = fsoFile.Name
    strPath = Left(fsoFile.Path, Len(fsoFile.Path) - Len(strName))
    fso.CopyFile strPath & strName, strPath & "new_addition" & strName
    fso.DeleteFile strPath & strName
Next
```
Set the string path to what you want. Paste it into a text file, save the file as *Name.VBS*. Double click to run. This has no warning messages, so be careful. There are multiple ways to parse your filename btw, so we could do anything you wanted.

HTH


----------



## Red2034 (Apr 17, 2007)

Can i run this code from inside a module - not a .vbs????


and I want the file naming to do exactly what i have above

i just want it to parse out "filename"_01_01_01.swf - "filename"_01_01_09.swf

and replace it with "newfilename"_01_01_01.swf - "newfilename"_01_01_09.swf 


keeping the numbering and the extention (.swf) the same

Thanks


----------



## MWE (Mar 21, 2005)

if I understand you correctly, you have a group of files in a specific folder that are similar to:
OldName_1.dat
OldName_2.dat
OldName_3.dat
etc.

And want to rename them to:
NewName_1.dat
NewName_2.dat
NewName_3.dat

If so, forget about VBS and use the command line emulator built into WinXX (emulates MS DOS); in particular the *Rename* command with wildcards:

Start|Run cmd
navigate to the correct folder
type in rename OldName*.* NewName*.*


----------



## cherry pie (Jun 4, 2007)

You can rename multiple files in windows by selecting them all, press F2 (or right click then rename) and type a file name. the only restriction is that the file name will be followed by the number between brackets so they'll look like this:

folder\file_01_01_01.swf --> folder\newfilename_01_01_.swf
folder\file_01_01_02.swf --> folder\newfilename_01_01_(2).swf
folder\file_01_01_03.swf --> folder\newfilename_01_01_(3).swf
folder\file_01_01_04.swf --> folder\newfilename_01_01_(4).swf

i don't know if there is any control on that but might help if you're ok with this.


----------



## Red2034 (Apr 17, 2007)

these last two post are nice but off the subject:

I am looking for a VBA solution to rename multiple files in a folder


----------



## Zack Barresse (Jul 25, 2004)

What program are you using?

Edit: btw, you can take the code I posted and use it in Excel if you'd like. Just wrap it in a sub routine ..


```
Sub YourSubNameHere()
'... code I posted above
End Sub
```
I haven't tested in any other apps, but should work if it supports VBA and you have WSH (Windows Scripting Host) installed - which it is by default in Windows.


----------



## Rollin_Again (Sep 4, 2003)

Here is another way of doing it. The variable values (ReplaceWhat, ReplaceWith, and vPath) ARE Case Sensitive.


```
Public Sub ReName()

ReplaceWhat = "File"
ReplaceWith = "NewFile"
vPath = "C:\Test\"

vFile = Dir(vPath & "*.swf")

Do While vFile <> ""
Name vPath & vFile As vPath & Replace(vFile, ReplaceWhat, ReplaceWith)
vFile = Dir
Loop

End Sub
```
Regards,
Rollin


----------



## Red2034 (Apr 17, 2007)

Thanks Rollin!!! 

2 additions to this that would make it perfect for me is:

first - it works as long as there is no spaces in the folder structure.... C:\test 001 - will not rename the files.

second - I need it to rename all files in that folder, including files in a subfolder... is this possible?


----------



## Zack Barresse (Jul 25, 2004)

Red, look up the FileSystemObject. You can do this with Dir(), definitely, and a google search will return multipe results. But you can probably wrap your head around the FSO better. Plus, if you use Early Binding you'll have use of Intellisense, which will help greatly. Basically, do a loop through all files in the folder and make sure the criteria is to search subfolders.

HTH


----------



## Rollin_Again (Sep 4, 2003)

Red2034 said:


> I need it to rename all files in that folder, including files in a subfolder... is this possible?


Below is an example using a method similar to what Zack mentioned in his last post (FileSearchObject.) This method works even with spaces in the directory name.


```
Sub Rename()

ReplaceWhat = "File"
ReplaceWith = "NewFile"
vPath = "C:\Test"

With Application.FileSearch
    .NewSearch
    .LookIn = vPath
    .SearchSubFolders = True
    .FileName = "*.swf"
    .FileType = msoFileTypeAllFiles
End With

With Application.FileSearch
    If .Execute() > 0 Then

        For i = 1 To .FoundFiles.Count
            vFile = .FoundFiles(i)
            Name vFile As Replace(vFile, ReplaceWhat, ReplaceWith)
        Next i
    Else
        MsgBox "There were no files found."
    End If
End With
        
End Sub
```
Regards,
Rollin


----------



## Zack Barresse (Jul 25, 2004)

Hi Rollin,

I was actually referring the the FileSystemObject, not the FileSearch object. While I _really_ like the FileSearch object, it is no longer supported in Excel 2007.  Unfortunately, the developer team at Microsoft found it (apparently) too cumbersome. The best alternatives are using Dir() or FSO (Scripting) methods. Of course, it depends on the scope of the application and your particular flavor for which you choose. The FSO method needing the scripting runtime object (late vs early... another debate  ).


----------



## slurpee55 (Oct 20, 2004)

Zachary (  ) ,
Could one copy the files to a new folder with the new names and then delete the old folder, using FSO?


----------



## Rollin_Again (Sep 4, 2003)

Slurpee,

The save path can be modified to save the file anywhere you choose by simply changing the variable *strPath* in Zack's example. To delete a file using FSO you simple need to call the delete function after declaring and instantiating your FSO object.


```
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder "C:\Test"
```
Regards,
Rollin


----------



## slurpee55 (Oct 20, 2004)

I thought so - it may be a crude way to do what is wanted, but it would work, I assume.


----------

