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

'========================================================
' 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

Anonymous said…
Exactly what I was looking for. Thank you!
Anonymous said…
the program is excellent for my needs well done!! can you tell me how can i edit this script and add multiple values for example, if i want it to remove both "_" and "-" with a space??
I know s**t about coding so i need some help...
ApOgEE said…
sure you can. Simply modify the if statement and add the lines in bold as follows..

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!!
Anonymous said…
Works great for files but is there anyway to make this replace underscores in folder names?
Anonymous said…
Fantastic! Thanks a lot!
Anonymous said…
Is there any way to overwrite if a file of the new name already exists?
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?
Anonymous said…
Wow thanks for helping all of us underachievers who haven't bothered to become skilled in vbs yet. (me, me, oooo pick me)

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!!!!!
Unknown said…
Top job mate, works a treat. this will save lots of time. keep up the good work
Overkill said…
I combined this with AutoIt Scripting Language and made it into a very powerful tool. I had 4096 music files in a total of roughly 400 directories, replaced all underscores with spaces in about 40 seconds.

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.
wyanne said…
you are very good!!!! this is very helpful. my bf wanted me to edit all those mp3 titles with underscores on them... talk about love huh! nice! very nice!
wyanne said…
posted you code at http://gallery.menalto.com/node/33931#comment-302030
Unknown said…
Is there a way to change all subfolder names within a folder? Say we have a folders named Folder_1, Folder_2, is there a way to run a script to change the names to Folder1 and Folder2?
Overkill said…
change:
sName = Replace(fil.Name, "_", " ")

to:
sName = Replace(fil.Name, "_", "")
Dilbert said…
Excellent tool! But is there a way to remove random text? I mean to change a file name from "Artist Name - Track No. - Track Name" into the more simple "Track No. - Track Name"...??
Unknown said…
What about extra periods....don't touching in file extension of course

--------------

adding
If InStr(1, fil.Name, " ") <> 0 Then
' replace double space with space
sName = Replace(fil.Name, " ", " ")
Filipe said…
Very useful, congratulations and thanks for the time saved (and some unburned eyelashes...;)).
Greg Tompkins said…
Is there a way to strip out all non alphanumeric (A-Z,0-9, a-z) characters not including anything in the extension, so for example .mp3, .mp4 wouldn't be changed to .mp without specifying every character specifically? I have an older mp3 player and I already wrote a pretty cool batch file to randomly go through my library and copy files not to exceed 4GB into my "weeklymp3" folder that I scheduled as a task so I have new music every week. My player sometimes chokes on strange characters, like the "ë" in Tiësto. Also, what is the way to overwrite an existing file? Must I do an IF then replace? THANK YOU!
rhazz said…
You are a life saver! Had about 300+ files with underscores to rename, but all done in an instant. Wish I had discovered this earlier.
Thanks so much!!
Libran1010 said…
Thanks! you rock!
I wanted the opposite of this, replace spaces with underscore, switched the lines, and voila!
Unknown said…
I tried something simular..but modified your VBS..


'========================================================
' 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?
ApOgEE said…
Dear Justin,
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.
Unknown said…
Ok i Changed the Line in question...and Reran the code...

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
ApOgEE said…
Dear Justin,

I made a new post to answer your question. Check it out here:
VBScript Get File Extension
Anonymous said…
I, lacking knowledge of programming, was wondering if there is a way to do this:

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.
Tom said…
Fan-tas-tic!!!
ProDigit said…
Million stars to you man!
Thanks a million for saving me hours of work!
Richard Clark said…
This is great. Can the original be modified to work on Folder Names within a directory? I was unable to remove a dash '-' from file names and also wondered if you could ask to add a space either side of a dash? Great stuff.
Unknown said…
Thenk you very much. This helped me alot.
Unknown said…
Thankyou this code worked wonders!!!

I was searching for so long to try and find something to fix this.
Unknown said…
I have a file name like 2222_rajesh_kumar_kuppala.pdf its like SNO_FIRSTNAME_LASTNAME_INITIAL.pdf .I want the output

SNO=2222
FIRSTNAME=rajesh
LASTNAME=kumar
INITIAL=kuppala

i have basic knowledge on VB. Could you please suggest me how to solve this.
Alex said…
Vinodkumar, go look at regular expressions and you should be able to figure it out. Otherwise, go post on stack overflow not the comments thread of an 11-year-old blog post...
Unknown said…
This comment has been removed by a blog administrator.
Unknown said…
You answered a query:
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.
jnh1564 said…
Just saved a ton of time using this script! Thanks so much!! Simple script with great benefit. Thank you for sharing.
arun said…
This comment has been removed by a blog administrator.
arun said…
Whenever I tried to run this code it throw the error in Replace.

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


ApOgEE said…
Dear arun,

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!

Popular posts from this blog

How to Create Hyperlink on Blogger Post

How to Add a Sudo User on AlmaLinux 9.2 (Turquoise Kodkod): A Step-by-Step Guide

How to Check AlmaLinux Version on the Command Line: A Quick Guide