Skip to content

Instantly share code, notes, and snippets.

@rbtr
Created July 8, 2015 19:38
Show Gist options
  • Save rbtr/d3e40424f52c727b65cd to your computer and use it in GitHub Desktop.
Save rbtr/d3e40424f52c727b65cd to your computer and use it in GitHub Desktop.
Windows Batch File Renumbering
rem BATCH_RENUMBER.bat
rem v1
rem 8 July 2015
rem EAB @ NHF
@echo off
setlocal EnableDelayedExpansion
cls
rem Explaining how it works:
echo BATCH_RENUMBER.bat
echo This script renumbers uniform suffixed files
echo ie: MD01.dwg, MD02.dwg, ...
echo Here, "MD" is the PREFIX, and "01", "02" are the SUFFIX
echo ".dwg" is the EXTENSION
echo Run this from the folder with the files you want to renumber
rem Priming with user input:
echo.
set /p PREFIX="PREFIX = "
set /p EXTENSION="EXTENSION = "
rem We prevent collisions by suffixing with .old
set OLD=".old"
echo Enter file numbers
echo.
rem Here's the prompt>rename loop
: loop
rem Get the file numbers
echo.
set /p OLDNUM=" Current number = "
set /p NEWNUM=" Change to = "
rem Make the filenames
set OLDFILE=%PREFIX%%OLDNUM%%EXTENSION%
set NEWFILE=%PREFIX%%NEWNUM%%EXTENSION%
rem Prevent collisions/check if previous collision has been prevented and correct filenames
rem Check the newfile and move it out of the way if necessary
if exist %NEWFILE% (
ren %NEWFILE% %NEWFILE%%OLD%
)
if exist %OLDFILE%%OLD% (
ren %OLDFILE%%OLD% %NEWFILE%
echo Sucess
) else (
echo %OLDFILE%
if exist %OLDFILE% (
ren %OLDFILE% %NEWFILE%
echo Success
) else (
echo Original file not found, check prefix/extension/numbering
)
)
goto loop
echo Press any key to exit
pause
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment