The batch file should be stored in directory containing all the *.txt files and executed with a double click.
@echo off
for /F "eol=| delims=" %%I in ('dir "*cheese*.txt" /A-D /B 2^>nul') do call :RenameFile "%%I"
set "NewName="
pause
goto :EOF
:RenameFile
set "NewName=%~nx1"
set "NewName=%NewName:cheese=Louise%"
ren "%~1" "%NewName%"
goto :EOF
The command DIR searches
- just for files because of
/A-D
(not attribute directory)
- only in current directory as not using additionally
/S
- for *.txt files containing
cheese
and
- outputs in bare format because of
/B
the names of the found files with file extension, but without path.
The DIR command line is executed by FOR in a separate command process in background which captures the output of this command process (= command DIR) written to handle STDOUT.
A possible error output by DIR on not finding in directory any file matching the wildcard pattern to handle STDERR is suppressed by redirecting it with 2>nul
to device NUL. Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line with using a separate command process started in background.
The command FOR processes each line captured from output of DIR. The default behavior of FOR to ignore lines starting with ;
is disabled by option eol=|
. The character |
can't be used in a file/folder name and so can't be at begin of a file/folder name. The default behavior of FOR to split up each line on horizontal tabs and spaces into substrings is disabled by option delims=
specifying an empty list of delimiters. So FOR really assigns each file name with file extension, but without path to loop variable I
.
It is important here to run FOR on a captured list of file names instead of letting FOR itself search recursively for the TXT files as otherwise it could happen during the file renames that some TXT files are skipped because of the directory entry changes during the loop iterations.
For each file name the subroutine RenameFile
is called with passing the full file name as first argument enclosed in double quotes to the subroutine. The double quotes are necessary to process correct also the file names on containing a space or one of these characters &()[]{}^=;!'+,`~
in file name or file path.
A subroutine is used instead of doing the remaining code directly in the FOR loop as this would require usage of delayed environment variable expansion which would cause problems if file/folder names of the processed files contain by chance one or more exclamation marks.
In the subroutine RenameFile
the name of the current file without path is assigned to environment variable NewName
.
Next a string substitution is applied on file name to replace case-insensitive all occurrences of cheese
by Louise
.
Then the file rename is executed with specifying the file to rename and the new name with file extension without path as required by command REN.
The command ECHO could be inserted left to ren
to run the batch file first for just checking if the file renames would be done as expected.
The DIR option /S
could be added after /B
to search additionally also recursive in all subdirectories of current directory for *.txt files with cheese
in file name. In this case DIR would output all file names with full path. But the batch file as written would work nevertheless correct.
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 /?
for /?
goto /?
pause /?
ren /?
set /?
See also Where does GOTO :EOF return to?