I'm very close I think. I have a folder where-in I'm trying to rename all the sub folders and files such that caps are stripped, symbols replaced with applicable words, and spaces changed to hyphens in both the files AND their parent sub-folders.
Here's the batch file I have so far:
cd d:\scripts\testing\
for /r %%D in (.) do @for /f "eol=: delims=" %%F in ('dir /l/b "%%D"') do @ren "%%D\%%F" "%%F"
SET location=d:\scripts\testing\
for /R %location% %%A in (*.*) do call :replspace "%%A"
for /R %location% %%A in (*.*) do call :repland "%%A"
goto :eof
:replspace
set "_fn=%~nx1"
ren %1 "%_fn: =-%"
:repland
set "_fn=%~nx1"
ren %1 "%_fn:&=and%"
As you might be able to see, first it goes through and renames everything (files and folders) in d:\scripts\testing\ to lower-case. Next it renames all files in that same directory to replace spaces with hyphens and "&" with the word "and". This all works... except I need to do the same symbol and space changes to the folders and I'm not finding any real info on how to do that.
Anyone have any suggestions?
BTW, this runs on server 2012 r2, however for interoperability issues, the scripts have to be old fashioned batch scripts.
CodePudding user response:
You can perform the entire replacement task with one for loop., by using dir /s
to recursively search the directory.
@echo off
setlocal enabledelayedexpansion
pushd "d:\scripts\testing\" || goto :EOF
for /f "delims=" %%i in ('dir /b /l /s') do (
set "item=%%~i"
set "item=!item:%%~dpi=!"
set "item=!item: =-!"
ren "%%~fi" "!item:&=and!"
)
popd
I did not set
the &
replacement as a variable seeing as there are only two replacements done, so simply using the substitution on the last item makes sense without the need to set
again. If you have more replacements to add, add them before the ren
line:
Note that this example will merely echo
the results for testing purpose. Only remove echo
once you're convinced the results are as expected.
Then, take note of || goto :EOF
in the pushd
statement. This is critical for good reason. Should it fail to cd
or pushd
, generally the script will continue with the rename, from the working directory it was started in, or a previous cd
pushd
etc. In this case, if it fails to find the directory, or it does not have permission, it will skip the remainder of the script entirely.
Final Note. Should your files or folders contain !
this will need to change. You can then simply revert to moving the set
and ren
to a label, then call the label as delayedexpansion
will cause the !
to be lost.
CodePudding user response:
While Gerhard's script is clean and should work, it trips on itself under certain circumstances like mine. I wound up using this script:
rem #### Step 1: move to working directory ####
cd "d:\scripts\testing\"
rem #### Step 2: change case on everything ####
for /r %%D in (.) do @for /f "eol=: delims=" %%F in ('dir /l/b "%%D"') do @ren "%%D\%%F" "%%F"
rem #### Step 3: set location ####
SET location=d:\scripts\testing\
for /R %location% %%A in (*.*) do call :replspace "%%A"
for /R %location% %%A in (*.*) do call :repland "%%A"
rem #### Step 4: replace spaces/symbols on directories ####
setlocal enabledelayedexpansion
pushd "D:\scripts\testing\" || goto :EOF
for /f "delims=" %%i in ('dir /l /b /s /ad') do (
set "item=%%~i"
set "item=!item: =-!"
move "%%~fi" "!item:&=and!"
)
popd
rem #### variables ####
:replspace
set "_fn=%~nx1"
ren %1 "%_fn: =-%"
:repland
set "_fn=%~nx1"
ren %1 "%_fn:&=and%"
Its a combination of my own script and a slight modification of Gerhard's script. Bassically I ended up stepping through the modifications.
- change everything to lower case.
- replace spaces and symbols in the file names.
- replace spaces and symbols in the directory names.
I know it's repetitive and I'd like to do it with less lines, but it works.