Skip to content

Instantly share code, notes, and snippets.

@pdbartsch
Last active April 15, 2024 11:50
Show Gist options
  • Save pdbartsch/5405134 to your computer and use it in GitHub Desktop.
Save pdbartsch/5405134 to your computer and use it in GitHub Desktop.
Windows batch file to change the names of many files based on a csv. The first "column" of the csv contains the old name name with the old file extension. Then second contains the new name with no file extension.
@echo off
setlocal enableDelayedExpansion
REM sets folder to CD (current directory)
SET folder=%CD%
REM if the renamed folder already exists, I'll assume I've already run this script and go to EOF (end of file)
IF EXIST "%folder%\renamed" GOTO :EOF
REM else move on to :FOLDERDOESNOTEXIST
GOTO :FOLDERDOESNOTEXIST
:FOLDERDOESNOTEXIST
MKDIR "%folder%\renamed"
ECHO "Created Folder %folder%\renamed"
REM start a counter
set "index=0"
REM read file names from a comma delimited (csv) file. Skip the first row which just has column headers.
REM Old names in column 1, new names in column 2.
for /F "skip=1 tokens=1,2 delims=," %%j in (%folder%\rename.csv) do (
REM increment counter by 1 each time
set /a "index+=1"
IF EXIST %folder%\renamed\%%k.pdf (
REM file exists, so increment the file name with the index (counter)
copy "%%j" %folder%\renamed
rename "%folder%\renamed\%%j" %%k___!index!.pdf
) ELSE (
REM file does not exist, so just name it according to the second column (%%k)
copy "%%j" %folder%\renamed
rename "%folder%\renamed\%%j" %%k.pdf
)
)
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment