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

0
QlemoBatchelor, 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"
)

0
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

0

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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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!"
)
0
Steve KnightIT ConsultancyCommented:
I was a bit slow posting there clearly!!
0
Team-PSOAuthor Commented:
Super fast and worked perfectly!!!
Thanks a lot!
0
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.
0
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
0
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?!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.