VB Script to Rename All Files in a Folder
February 24th, 2009 by Andrew Chen
After writing previous post about using window
Let’s say you have a few thousand files named in the same pattern like transaction_xxxxx.txt in a folder called test. You want to rename all these files so that they are named like trans_xxxxx.txt. Here is the VB Script code to do that.
Dim fso,f
Set fso=CreateObject(”Scripting.FileSystemObject”)
Set f = fso.GetFolder(”d:/test”)
For Each file In f.Files
fso.MoveFile file.Name, Replace(file.Name,”Transaction”,”Tran”)
Next
Set f = Nothing
Set fso = Nothing
Put the above code into a text file, rename the text file to rename.vbs and put it into the test folder. Double click on the vbs file to execute it. And that is it.


(2 votes, average: 3.5 out of 5)
What if all the files have different names?
Eg, all my photo files need renaming but they all have different naming conventions applied.
Cheers.
I think it should be easy to replace the following line with some if statements to implement whatever naming convention you want to follow.
fso.MoveFile file.Name, Replace(file.Name,”Transaction”,”Tran”)
for example you can have
if instr(file.name, “Transaction”)>0 then
fso.MoveFile file.Name, Replace(file.Name,”Transaction”,”Tran”)
end if
Andrew:
Sounds like you enjoy posting automation scripts based on new ideas. I appreciate that - your effort helps out a lot of people.
Have you looked at biterscripting (http://www.biterscripting.com for free installation) ? It is simple to learn, but you can take it to a cerebral level. Documentation is available with the ‘help’ command. It is my guess that you will probably like posting scripts in biterscripting.
Sen
Thank Sen! I will check that out
I know this post is really old, but I just wanted to let you know that I looked all over the place for a solution like this and finally stumbled across your post. It was exactly what I needed.
I did have to retype all of the quotation marks into the script once I copied and pasted it into notepad. Windows doesn’t like the ones that were in your post.
Thanks a million!!