Link to home
Start Free TrialLog in
Avatar of Team-PSO
Team-PSO

asked on

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!
Avatar of TakedaT
TakedaT
Flag of United States of America image

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

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"
)

ASKER CERTIFIED SOLUTION
Avatar of TakedaT
TakedaT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!"
)
I was a bit slow posting there clearly!!
Avatar of Team-PSO
Team-PSO

ASKER

Super fast and worked perfectly!!!
Thanks a lot!
Sorry dragon-it...I didn't see your post but I'll try it too...Thanks TakedaT...it worked in my test perfectly.
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
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?!