Skip to main content

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!!!!!
Mike O 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
Joey 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"...??
extraanis 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 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!
Justin 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.
Justin 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!
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.

Popular posts from this blog

How to Create Hyperlink on Blogger Post

Well, this time I make a basic simple tutorial. For all advanced geek who read this blog, please ignore this post... I just wanna help myself not to teach the same basic thing on every newbies friends that really need help.

OK guys, back to the howto...

There are two ways to create a hyperlink on your blog post. One is the simple GUI way and the other is coders way. I prefer the coder way as I can have more control on it.

I like to teach by example. So, let say you wanna create a hyperlink on this line of words...

You can go here to see my art blog

where you wanna link the word "here" to the other page on your blog or on the internet. In this example, I wanna link it to my Art Of Apogee Blog post with the address:
http://artofapogee.blogspot.com/2008/03/gimp-best-free-photoshop.html

And here is how to do it using both ways...

The simple GUI way

If you rollover every button on your Compose toolbar, you can see one button called "Link" where the word Link will pop up when yo…

How to Show and Hide Text in Blog Post

I would like to share on how to make a Show/Hide text in your blog post. I'm sorry if I didn't explain detail enough because I want to keep my post short as I'm in a hurry. However, you can read the detail explanation on Blogger Help.

In this example, I assume you have sub topics that you want to make it hidden until reader clicks on the headline. Without the show/hide trick, it would look like this...

Title OneThis is the example text for title one.
Title TwoThis is the example text for title two.
Title ThreeThis is the example text for title three.
Here's my step to do this... Firstly, edit your template and add the following code in the CSS area.


.posthidden {display:none}
.postshown {display:inline}
Then add the following javascript code on your blog template between the <head></head> tag.

<script type="text/Javascript"> function expandcollapse (postid) { whichpost = document.getElementById(postid); if (whichpost.className==&q…

VBScript Get File Extension

Before anything else, you may love to read this book too!...


Actually, I got this question from the reader of my previous blog post about VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder, and I decided to answer it here.

Question from Justin:
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…