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!
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!
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"
)
@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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!"
)
@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!!
ASKER
Super fast and worked perfectly!!!
Thanks a lot!
Thanks a lot!
ASKER
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
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.Fi leSystemOb ject")
dim objDir : set objDir = objFSO.GetFolder(strSource Dir)
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?!
on error resume next
strSourceDir = "C:\test_folder"
dim objFSO : set objFSO = CreateObject("Scripting.Fi
dim objDir : set objDir = objFSO.GetFolder(strSource
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")>
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?!
Open in new window