Handy VBScript to Remove the WhiteSpaces from All File Names in a Folder

Here is a super simple VBScript that will remove any white spaces from filenames in a folder. There’s no need to specify the folder, just copy it to the folder you need and double click to run it. Of course you’ll have to copy the lines of code below into a notepad file and save it with a .vbs extension.

VBScripts are great if you want to automate simple repetitive tasks. Also, they work natively on pretty much any Windows-based system so you don’t need to worry about installing extra software or enabling libraries… basic VBScripting just works anywhere. It’s worth checking out just so you have it as an option if you are in a pinch.

The functionality of the example script below is pretty specific to just removing whitespaces from filenames, but its a starting point for any script you might need that loops through files in a folder and/or that needs to do a replace of a character in the names of those files.

If you glance through the code, you’ll see that the script grabs the name of the current folder that it resides in using: objFSO.GetParentFolderName(Wscript.ScriptFullName) That’s already a handy function in itself, so that you can just paste the script anyplace you want it to run.

The script then grabs the list of files in the folder it is in and loops through them using a for…next loop. It then uses the MoveFile command on each file to replace the text that you don’t want in each file name… in this case that’s removing blank spaces.


Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolderName = objFSO.GetParentFolderName(Wscript.ScriptFullName)
Set objFolder = objFSO.GetFolder(strFolderName)
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFSO.MoveFile strFolderName + "\" + objFile.Name , strFolderName + "\" + Replace(objFile.Name," ", "")
Next

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s