Skip to content

Instantly share code, notes, and snippets.

@hsandid
Last active August 9, 2020 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsandid/f4d85eba187f7a5aed39fda2693367f3 to your computer and use it in GitHub Desktop.
Save hsandid/f4d85eba187f7a5aed39fda2693367f3 to your computer and use it in GitHub Desktop.
@echo off
SETLOCAL
title BatchRename.bat
echo ----------------------------------------------------------------------------------------
echo Script : BatchRename.bat
echo Description : Rename all files in directory using user-defined prefix.
echo Note : Entering certain characters in the prefix will cause errors (/,\,:,^?,^",^<,^>,^|)
echo ----------------------------------------------------------------------------------------
set /A fileCounter=0
set /p filePrefix="Input Prefix: "
for /f "tokens=* delims=" %%f in ('dir /b /a-d') do (
IF not %%f==BatchRename.bat call :RenameFileAndIncrement "%%f"
)
GOTO ProcessComplete
:RenameFileAndIncrement
SET "oldFileName=%~1"
for /f "tokens=2 delims=." %%a in ("%oldFileName%") do set fileExtension=%%a
set "newFileName=%filePrefix%%fileCounter%.%fileExtension%"
echo Renaming "%oldFileName%" to "%newFileName%"
MOVE "%oldFileName%" "%newFileName%"
set /A fileCounter=fileCounter+1
exit /b
:ProcessComplete
echo Batch Renaming is Complete...
ENDLOCAL
pause
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment