VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder
Sometimes I wanted to remove underscores in file name and replace it with space. For example I would like to see the filename as 'Sharifah Aini - Suasana Hari Raya.mp3' instead of 'Sharifah_Aini_-_Suasana_Hari_Raya.mp3'. I often have this kind of files when I download mp3s on the internet. I know this maybe so simple because you can just select the file in Windows Explorer, Press F2 and manually rename the file. But for a collections of files in a folder, this may be a boring repetitive task that I'll hate to do.
So, as a damn lazy coder , I've wrote a short Visual Basic Script to automatically do the task with only one simple command. I think this VBScript maybe useful for you too. So, I would like to share my VBScript to replace underscores in filename with spaces for all files within a folder. Here's the script...
To use the script, you just need to copy this scripts to a text file and then name the textfile with 'rmus.vbs' or any other name you like with '.vbs' extension which is for vbscript.
There is lots of ways to use the script. One of them is, copy the .vbs file to the folder you want the task to be done and then double click the .vbs file in that folder. When the script is done, it will prompt you with a message box which says 'Completed!'. I'll post the other ways to run this kind of scripts later. Feel free to bookmark or subscribe this blog to automatically get updates on this blog.
If you like this code, feel free to use it and if you like to spread it on the net, please gimme a link to this page. I would love to hear your comments too!
So, as a damn lazy coder , I've wrote a short Visual Basic Script to automatically do the task with only one simple command. I think this VBScript maybe useful for you too. So, I would like to share my VBScript to replace underscores in filename with spaces for all files within a folder. Here's the script...
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder(".")
' 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, "_") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "_", " ")
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
To use the script, you just need to copy this scripts to a text file and then name the textfile with 'rmus.vbs' or any other name you like with '.vbs' extension which is for vbscript.
There is lots of ways to use the script. One of them is, copy the .vbs file to the folder you want the task to be done and then double click the .vbs file in that folder. When the script is done, it will prompt you with a message box which says 'Completed!'. I'll post the other ways to run this kind of scripts later. Feel free to bookmark or subscribe this blog to automatically get updates on this blog.
If you like this code, feel free to use it and if you like to spread it on the net, please gimme a link to this page. I would love to hear your comments too!
Comments
I know s**t about coding so i need some help...
If InStr(1, fil.Name, "_") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "_", " ")
' replace dash with space
sName = Replace(sName, "-", " ")
' rename the file
fil.Name = sName
End If
that's it.. good luck!!
Could you show us how to modify it so it changes only a single file? I've done some fiddling with it and can't get it to work.
THANKS!!!!!
Bump for the replace folders mod though...still have a few of those left :)
Another mod that'd be nice would be to include subdirectories.
sName = Replace(fil.Name, "_", " ")
to:
sName = Replace(fil.Name, "_", "")
--------------
adding
If InStr(1, fil.Name, " ") <> 0 Then
' replace double space with space
sName = Replace(fil.Name, " ", " ")
Thanks so much!!
I wanted the opposite of this, replace spaces with underscore, switched the lines, and voila!
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder(".")
' 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, "The ") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "The ", "") & ", The"
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
i have Files in a given folder that has Names in them...
ie.
The Last Star Fighter
Gone with the wind
The Good, the Bad, and the Ugly.
With the modified VBS above Ive run it and i get the following...
Last Star Fighter.mp4, The
Gone with the Wind
Good, the Bad, and the Ugly.mp4, The
How do i get rid of the extention in the middle of the name? What am I doing wrong?
check your code again. Search for line of code containing "sName = Replace" and remove the code after ')'.
Specifically, the code: '& ", The"'
have to be removed.
Now i get...
Gone with the Wind --ok
Good, the Bad, and the Ugly --Problem
Last Star Fighter --Problem
They Should read...
Gone with the Wind
Good, the Bad, and the Ugly, The
Last Star Fighter, The
I made a new post to answer your question. Check it out here:
VBScript Get File Extension
1) For windows XP (actually, I have Fedora as well, but I don't use it very often, so XP is more convenient)
2) To modify the file name in itunes (which I believe uses mp3 tags, so modifying them would work as well).
I have many songs stored on my family's network drive, which I've added to itunes, but they all have underscores instead of spaces, which is frustating.
I've seen scripts that will modify the file name, but none that change the Mp3 tags.
Thanks for any help.
Thanks a million for saving me hours of work!
I was searching for so long to try and find something to fix this.
SNO=2222
FIRSTNAME=rajesh
LASTNAME=kumar
INITIAL=kuppala
i have basic knowledge on VB. Could you please suggest me how to solve this.
Anonymous said…
Is there any way to overwrite if a file of the new name already exists?
February 7, 2008 at 11:10 PM
ApOgEE said…
Sure you can! you can add an if statement to check if the file is already exists, delete the old file and rename the new file. that will work like overwriting isn't it?
Please provide a sample of the code with the "if" statement inserted.
Error " Wrong number of Assignment or Invalid property assignment
My concern is to remove the space between the excel file file with _.
Here What I modified.
Sub replace()
Dim sName
Dim fso
Dim fol
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("C:\Users\arunp\Downloads\test")
For Each fil In fol.Files
If InStr(1, fil.Name, " ") <> 0 Then
sName = replace(fil.Name, " ", "_")
fil.Name = sName
End If
Next
End Sub
The problem with your modification is you name your sub as Sub replace() which conflict with VBScript Replace function. You have to change it to something else like for example Sub MyReplace().
Your code is throwing Error because it confuse that whether you want to call VBScript Replace function or your new replace function recursively.
Change your sub name and it will work. Good luck!