Currently I have a CSV file with 100 lines. In each line there are 10 image names following folder name in the first column.
FolderName1,ImageName1,ImageName2,ImageName3,...,ImageName10
FolderName2,ImageName1,ImageName2,ImageName3,...,ImageName10
... and so on upto
FolderName100,ImageName1,ImageName2,ImageName3,...,ImageName10
I also have 100 folders with 10 images in each. I need to bulk rename images according to in which catalog they are by line in CSV. And this is what I tried so far, Reading the CSV file,
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=1-10 delims=," %%a in (CSVExample.csv) do (
echo %%a
echo %%b
echo %%c
echo %%d
echo %%e
echo %%f
echo %%g
echo %%h
echo %%i
echo %%j
echo %%k
)
pause
However, above code doesn't read read the 10th image name(ImageName10). I couldn't understand why?..
And also, this is what I'm trying to do with renaming part,
OldImageName1 to ImageName1
OldImageName2 to ImageName2
OldImageName3 to ImageName3
OldImageName4 to ImageName4
OldImageName5 to ImageName5
OldImageName6 to ImageName6
... and so on.
Old image name doesn't have to match any criteria. I just need to rename the images regardless of its existing name string.
Can someone shed some light on this please? Also please direct me with an effective way to include the renaming process with this script.
CodePudding user response:
I believe this may solve your problem. Relevant methodology commentary is included as rem
remarks.
@ECHO Off
SETLOCAL
rem The following settings for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "filename1=%sourcedir%\q72507524.txt"
FOR /f "usebackqtokens=1*delims=," %%b IN ("%filename1%") DO (
rem %%b contains the directoryname, %%c a comma-separated list of the new filenames for that subdirectory
FOR /f "delims=" %%e IN ('dir /b /a-d "%sourcedir%\%%b"') DO (
rem %%e contains each filename (only) in the directory
FOR %%y IN (%%c) DO IF NOT EXIST "%sourcedir%\%%b\%%y" IF EXIST "%sourcedir%\%%b\%%e" REN "%sourcedir%\%%b\%%e" "%%y"
)
)
GOTO :EOF
Always verify against a test directory before applying to real data.
CodePudding user response:
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.
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 /?
... 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.