Reputation: 22966
Can some one please help explain how I can write some vbs script that searches for files with a particular string and renames them?
For example, say I have a folder c:\test
I want to search in c:\test for every file with the word John and replace with say the word Dave...
e.g. Contents were:
john_list.txt auto.john.doc
After script:
dave_list.txt auto.dave.doc
Can you help?
Thanks!
SOLUTION:
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder("c:\TEST")
' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "john") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "john", "dave")
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
Upvotes: 1
Views: 19831
Reputation: 200573
For recursing into subfolders you need something like this. Replacing text in the name of the file can be done like this:
f.Name = Replace(f.Name, "john", "dave")
Upvotes: 3