Tech Support Guy banner
Status
Not open for further replies.
1 - 17 of 17 Posts

·
Registered
Joined
·
178 Posts
Discussion Starter · #1 ·
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...
 

·
Registered
Joined
·
5,459 Posts
Hi Red,

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

Code:
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
 

·
Registered
Joined
·
178 Posts
Discussion Starter · #5 ·
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
 

·
Registered
Joined
·
185 Posts
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*.*
 

·
Registered
Joined
·
345 Posts
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.
 

·
Registered
Joined
·
5,459 Posts
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 ..

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

·
Registered
Joined
·
4,936 Posts
Here is another way of doing it. The variable values (ReplaceWhat, ReplaceWith, and vPath) ARE Case Sensitive.

Code:
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
 

·
Registered
Joined
·
178 Posts
Discussion Starter · #11 ·
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?
 

·
Registered
Joined
·
5,459 Posts
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
 

·
Registered
Joined
·
4,936 Posts
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.

Code:
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
 

·
Registered
Joined
·
5,459 Posts
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 ;) ).
 

·
Registered
Joined
·
4,936 Posts
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.

Code:
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder "C:\Test"
Regards,
Rollin
 
1 - 17 of 17 Posts
Status
Not open for further replies.
Top