Batch Script - Rename Directory Files (Remove Space and Replace with Underscore)

Hey folks,

I think the title states it all, I need a batch CMD or VBscript that I can run on my NT box that will search the directory, and subdirectories, and take the files in them, and rename them to include "underscores" instead of "spaces."

That's the naming standard now and I need some help with the script.

Thanks a ton!
Team-PSOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TakedaTCommented:
This should do it.  Save it as a vbs and change line 2 to reflect the path to your directory to search.
on error resume next
strSourceDir = "D:\profile\desktop\test\"
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objDir : set objDir = objFSO.GetFolder(strSourceDir)
 
SearchDir objDir
Function SearchDir(strCurrentDir)
	For Each file In strCurrentDir.Files
		file.name = replace(file.name," ","_")
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder
	Next
End Function

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If you are certain that the result is ok, remove the word echo from the code below to really execute it. As-is, the code will only echo the command used.

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=* delims=" %%A in ('dir /a:-d /s "c:\* *"') do (
  set fn=%%~nA
  set fn=!fn: =_!
  echo ren "%%~A" "%%~dpA!fn!%%~xA"
)

TakedaTCommented:
Sorry, forgot some error handling.  Try this.   Save it as a vbs and change line 2 to reflect the path to your directory to search.
on error resume next
strSourceDir = "D:\profile\desktop\test\"
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objDir : set objDir = objFSO.GetFolder(strSourceDir)
 
SearchDir objDir
Function SearchDir(strCurrentDir)
	For Each file In strCurrentDir.Files
		strCurFile = file.name
		If instr(strCurFile," ")>0 then file.name = replace(file.name," ","_")
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder
	Next
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Steve KnightIT ConsultancyCommented:
Sure.  Try this:

@Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
CD /d c:\startdir

REM Process files AND directories under the above startdir
REM Remove the word ECHO fromt the RENAME line to make it work

FOR /f "TOKENS=*" %%a in ('DIR /b *.*') DO (
  SET filename=%%~nxa
  SET filename=!filename: =_!
  ECHO RENAME "%%a" "!filename!"
)
Steve KnightIT ConsultancyCommented:
I was a bit slow posting there clearly!!
Team-PSOAuthor Commented:
Super fast and worked perfectly!!!
Thanks a lot!
Team-PSOAuthor Commented:
Sorry dragon-it...I didn't see your post but I'll try it too...Thanks TakedaT...it worked in my test perfectly.
Steve KnightIT ConsultancyCommented:
Not a problem.  I mis-read anyway thinking you wanted to rename the dirs too.  Otherwise very similar to Qlemo's, we all hit submit about the time i guess.

Steve
joewilson00Commented:
I love this script! I modified to replaces & with and, remove ', remove extra -------- and to change mpeg to mpg..


on error resume next
strSourceDir = "C:\test_folder"
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objDir : set objDir = objFSO.GetFolder(strSourceDir)
 
SearchDir objDir
Function SearchDir(strCurrentDir)
      For Each file In strCurrentDir.Files
            strCurFile = file.name
            If instr(strCurFile," ")>0 then file.name = replace(file.name," ","_")
            If instr(strCurFile,"&")>0 then file.name = replace(file.name,"&","and")
            If instr(strCurFile,"'")>0 then file.name = replace(file.name,"'","")
            If instr(strCurFile,"--")>0 then file.name = replace(file.name,"--","-")
            If instr(strCurFile,".mpeg")>0 then file.name = replace(file.name,".mpeg",".mpg")
      Next
      For Each folder In strCurrentDir.SubFolders
            SearchDir folder
      Next
End Function




ONE QUESTION THOUGH!!   How can you modify the code to overwrite files that already exist?!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.