Help of cmd.exe
output on running in a command prompt window cmd /?
explains on last page with last paragraph that file names containing a space or one of these characters &()[]{}^=;!'+,`~
must be enclosed in double quotes.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do (
set "NewName=%%I"
set "NewName=!NewName:Digital=digital!"
set "NewName=!NewName: (2 covers)=!"
move "%%I" "!NewName!"
)
endlocal
See also How to set environment variables with spaces?
On the command lines with SET the string set
is argument 0 and NewName=...
is argument 1. The entire argument string must be enclosed in double quotes, especially on containing space or one of these characters &()[]{}^=;!'+,`~<|>
.
Well, the command SET has a very special argument string parsing which makes it often possible to omit the double quotes even on assigning a string value with spaces and brackets to an environment variable. But the command block executed by FOR starts with (
. The next )
found by Windows command processor on parsing entire command block before executing the command FOR is interpreted as end of command block, except )
is within a double quoted string or is escaped with ^
to be interpreted as literal character instead of end of a command block. For that reason it is highly recommended to enclose an argument string always in double quotes if it is not 100% guaranteed that the argument string does not contain command line critical characters.
One more note: File names containing one or more !
would not be processed correct by the above batch file because of enabled delayed environment variable expansion.
The solution is avoiding usage of delayed expansion by using a subroutine.
@echo off
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
goto :EOF
:RenameFile
set "NewName=%~1"
set "NewName=%NewName:Digital=digital%"
set "NewName=%NewName: (2 covers)=%"
move %1 "%NewName%"
goto :EOF
See also Where does GOTO :EOF return to?
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
dir /?
echo /?
endlocal /?
for /?
goto /?
move /?
set /?
setlocal /?