There could be used the following batch file for this task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "%~dp0CSVExample.csv" echo ERROR: Missing file: "%~dp0CSVExample.csv"& exit /B 1
for /F "usebackq tokens=1* delims=," %%G in ("%~dp0CSVExample.csv") do if exist "%%~G\" (
for /F "delims=?" %%I in ('set ? 2^>nul') do set "?%%I?="
for %%I in ("%%~G\*") do set "?%%~nxI?=1"
for %%I in (%%H) do (
set "FileRenamed="
for /F "delims=?" %%J in ('set ? 2^>nul') do if not defined FileRenamed (
ren "%%~G\%%J" "%%~I"
if not errorlevel 1 (
set "?%%J?="
set "FileRenamed=1"
)
)
)
)
endlocal
The batch file does not use delayed variable expansion to work also for folder paths and file names containing an exclamation mark.
Each row in the CSV file in the directory of the batch file is first split up into two substrings using comma as delimiter. The first value – the folder name – is assigned to the specified loop variable G
and all file names separated by commas to the next loop variable H
according to the ASCII table.
NOTE: Neither the folder names nor the file names can contain a comma for that reason. Folder or file names containing a space or one of these characters &()[]{}^=;!'+`~
must be enclosed in "
in the CSV file or the processing does not work correct.
There is first check if the folder currently assigned to loop variable G
exists at all. The current row in the CSV file is ignored if the folder specified with absolute path or with a path relative to current directory does not exist.
There is next executed a for /F
loop which deletes all environment variables starting and ending with a question mark.
Then a standard for
loop is run to get the names of all non-hidden files in the current folder in the order returned by the file system to define an environment variable with the file name beginning with ?
and ending with ?
with value 1
which does not matter. The question mark is used as beginning and end of the variable name as no file name can contain a question mark. So there is finally a list of environment variables beginning and ending with ?
in memory which are the names of the files in current folder.
The third for
loop processes now the comma separated file names in current row of the CSV file.
For each file name is first undefined the environment variable FileRenamed
.
Next there is executed like before the command SET with just ?
as argument in background by a command process started by for /F
to output all environment variables of which name starts with a question mark. So output is the list of file names determined before for the current folder and this list is captured and processed by the for /F
loop.
A captured line from which the file name is extracted from the environment variable name beginning and ending with a question mark is processed only if there was not already done a rename for the current file name read from the row in the CSV file.
The next file in current folder is otherwise renamed to the current file name in the current row of the CSV file. If that file rename was successful, the name of the renamed file is removed from the list of environment variables and the environment variable FileRenamed
is defined to skip all other file names of the files originally found in the current folder.
This procedure makes it possible that the number of files in a folder does not match with the number of files in a row in the CSV file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
... explains %~dp0
... drive and path of argument 0, the batch file path ending always with a backslash.
echo /?
endlocal /?
exit /?
for /?
if /?
ren /?
set /?
setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on the FOR command lines to be interpreted as literal character when Windows command interpreter processes these command lines before executing command FOR which executes the embedded set
command line with using a separate command process started in background.