Script to rename & number files

Microsoft Windows xp professional w/serv...
June 7, 2010 at 08:45:51
Specs: Windows XP
I'm sure it's been asked before, and I've read a TON of questions/answers in here after searching for different iterations of "rename files". I'm striking out finding a solution that fits my situation.

I'm looking for a script to rename a bunch of files. Files are currently named:

Training.pdf
Training_E06FA.pdf
Training_E07B5.pdf
Safety.pdf
Safety_E063E.pdf
Safety_E0871.pdf

At run time, i'd like the script to request a name variable, then I'd like them all the files to be renamed using that variable.

Variable: Bob Smith
(note, there is a space between Bob and Smith, this is important to preserve)

Expected output, given the files above all existing in the same directory:

Bob Smith Training_001.pdf
Bob Smith Training_002.pdf
Bob Smith Training_003.pdf
Bob Smith Safety_001.pdf
Bob Smith Safety_002.pdf
Bob Smith Safety_003.pdf

It doesn't matter to me which training file becomes _001, or _002, or which safety file becomes _001, _002, etc.


See More: Script to rename & number files

Report •


#1
June 14, 2010 at 05:33:24
I figured this out after re-reading a bunch of different similar topics here & combining ideas from each. Here is the script in case it helps anyone else:

Renamer.bat (Usage: c:\renamer [folder files are located in])

@Echo Off

REM Initialize counters
Set CountA=0
Set CountB=0
Set CountC=0
Set CountD=0
Set CountE=0
Set CountF=0
Set CountG=0
Set CountH=0
Set CountI=0
Set CountJ=0
Set CountK=0
Set CountL=0
Set CountM=0
Set CountN=0
Set CountO=0
Set CountP=0
Set CountQ=0
Set CountR=0
Set CountS=0

REM Collect Employee Name for file name
REM
Echo Enter Employee name or Q to quit
Set /p Employee=
if /I "%employee%"=="Q" GOTO END

REM Create folder structure for renamed/moved files
md c:\2\Benefits\"%employee%"
md c:\2\Personal\"%employee%"
md c:\2\Medical\"%employee%"
md c:\2\Comp\"%employee%"
md c:\2\I9\"%employee%"
md c:\2\SelfID\"%employee%"

REM Create empty folder structure for the destination directory on DocMgmtServer
md c:\3\Benefits\"%employee%"
md c:\3\Personal\"%employee%"
md c:\3\Medical\"%employee%"
md c:\3\Comp\"%employee%"
md c:\3\I9\"%employee%"
md c:\3\SelfID\"%employee%"

REM Select only the appropriate files for renaming & numbering
REM
For /F "tokens=* " %%A in ('Dir /B %~f1\enro*') Do Call :RNa "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\401k*') Do Call :RNb "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\atte*') Do Call :RNc "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\pers*') Do Call :RNd "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\perf*') Do Call :RNe "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\trai*') Do Call :RNf "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\payr*') Do Call :RNg "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\rece*') Do Call :RNh "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\lega*') Do Call :RNi "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\flex*') Do Call :RNj "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\cobr*') Do Call :RNk "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\depe*') Do Call :RNl "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\insu*') Do Call :RNm "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\clai*') Do Call :RNn "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\post*') Do Call :RNo "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\medi*') Do Call :RNp "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\on*') Do Call :RNq "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\I9*') Do Call :RNr "%~f1\%%A"
For /F "tokens=* " %%A in ('Dir /B %~f1\self*') Do Call :RNs "%~f1\%%A"

GoTo :EOF

:RNa
REM Rename & Move Enrollment Forms
Set /A CountA=%CountA%+1
If %CountA% lss 1000 Set Ext=
If %CountA% lss 100 Set Ext=0
If %CountA% lss 10 Set Ext=00
move %1 "c:\2\benefits\%employee%\%employee% Enrollment Forms_%Ext%%CountA%.pdf"
GoTo :EOF

:RNb
REM Rename & Move 401k/Profit Share
Set /A CountB=%CountB%+1
If %CountB% lss 1000 Set Ext=
If %CountB% lss 100 Set Ext=0
If %CountB% lss 10 Set Ext=00
move %1 "c:\2\benefits\%employee%\%employee% 401K Profit Share_%Ext%%CountB%.pdf"
GoTo :EOF

:RNc
REM Rename & Move Attendance
Set /A CountC=%CountC%+1
If %CountC% lss 1000 Set Ext=
If %CountC% lss 100 Set Ext=0
If %CountC% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Attendance_%Ext%%CountC%.pdf"
GoTo :EOF

:RNd
REM Rename & Move Personal Docs
Set /A CountD=%CountD%+1
If %CountD% lss 1000 Set Ext=
If %CountD% lss 100 Set Ext=0
If %CountD% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Personal Docs_%Ext%%CountD%.pdf"
GoTo :EOF

:RNe
REM Rename & Move Performance Docs
Set /A CountE=%CountE%+1
If %CountE% lss 1000 Set Ext=
If %CountE% lss 100 Set Ext=0
If %CountE% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Performance Docs_%Ext%%CountE%.pdf"
GoTo :EOF

:RNf
REM Rename & Move Training
Set /A CountF=%CountF%+1
If %CountF% lss 1000 Set Ext=
If %CountF% lss 100 Set Ext=0
If %CountF% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Training_%Ext%%CountF%.pdf"
GoTo :EOF

:RNg
REM Rename & Move Payroll
Set /A CountG=%CountG%+1
If %CountG% lss 1000 Set Ext=
If %CountG% lss 100 Set Ext=0
If %CountG% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Payroll_%Ext%%CountG%.pdf"
GoTo :EOF

:RNh
REM Rename & Move Receipts
Set /A CountH=%CountH%+1
If %CountH% lss 1000 Set Ext=
If %CountH% lss 100 Set Ext=0
If %CountH% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Receipts_%Ext%%CountH%.pdf"
GoTo :EOF

:RNi
REM Rename & Move Legal Notices
Set /A CountI=%CountI%+1
If %CountI% lss 1000 Set Ext=
If %CountI% lss 100 Set Ext=0
If %CountI% lss 10 Set Ext=00
move %1 "c:\2\Personal\%employee%\%employee% Legal Notices_%Ext%%CountI%.pdf"
GoTo :EOF

:RNj
REM Rename & Move Flexible Spending
Set /A CountJ=%CountJ%+1
If %CountJ% lss 1000 Set Ext=
If %CountJ% lss 100 Set Ext=0
If %CountJ% lss 10 Set Ext=00
move %1 "c:\2\Benefits\%employee%\%employee% Flexible Spending_%Ext%%CountJ%.pdf"
GoTo :EOF

:RNk
REM Rename & Move Cobra Notice
Set /A CountK=%CountK%+1
If %CountK% lss 1000 Set Ext=
If %CountK% lss 100 Set Ext=0
If %CountK% lss 10 Set Ext=00
move %1 "c:\2\Benefits\%employee%\%employee% Cobra Notice_%Ext%%CountK%.pdf"
GoTo :EOF

:RNl
REM Rename & Move Dependant Verification
Set /A CountL=%CountL%+1
If %CountL% lss 1000 Set Ext=
If %CountL% lss 100 Set Ext=0
If %CountL% lss 10 Set Ext=00
move %1 "c:\2\Benefits\%employee%\%employee% Dependant Verification_%Ext%%CountL%.pdf"
GoTo :EOF

:RNm
REM Rename & Move Insurance Term Forms
Set /A CountM=%CountM%+1
If %CountM% lss 1000 Set Ext=
If %CountM% lss 100 Set Ext=0
If %CountM% lss 10 Set Ext=00
move %1 "c:\2\Benefits\%employee%\%employee% Insurance Term Forms_%Ext%%CountM%.pdf"
GoTo :EOF

:RNn
REM Rename & Move Claim Forms
Set /A CountN=%CountN%+1
If %CountN% lss 1000 Set Ext=
If %CountN% lss 100 Set Ext=0
If %CountN% lss 10 Set Ext=00
move %1 "c:\2\Medical\%employee%\%employee% Claim Forms_%Ext%%CountN%.pdf"
GoTo :EOF

:RNo
REM Rename & Move Post Hire
Set /A CountO=%CountO%+1
If %CountO% lss 1000 Set Ext=
If %CountO% lss 100 Set Ext=0
If %CountO% lss 10 Set Ext=00
move %1 "c:\2\Medical\%employee%\%employee% Post Hire_%Ext%%CountO%.pdf"
GoTo :EOF

:RNp
REM Rename & Move Medical Notes
Set /A CountP=%CountP%+1
If %CountP% lss 1000 Set Ext=
If %CountP% lss 100 Set Ext=0
If %CountP% lss 10 Set Ext=00
move %1 "c:\2\Medical\%employee%\%employee% Medical Notes_%Ext%%CountP%.pdf"
GoTo :EOF

:RNq
REM Rename & Move On Job Accident
Set /A CountQ=%CountQ%+1
If %CountQ% lss 1000 Set Ext=
If %CountQ% lss 100 Set Ext=0
If %CountQ% lss 10 Set Ext=00
move %1 "c:\2\Comp\%employee%\%employee% On Job Accident_%Ext%%CountQ%.pdf"
GoTo :EOF

:RNr
REM Rename & Move I9
Set /A CountR=%CountR%+1
If %CountR% lss 1000 Set Ext=
If %CountR% lss 100 Set Ext=0
If %CountR% lss 10 Set Ext=00
move %1 "c:\2\I9\%employee%\%employee% I9_%Ext%%CountR%.pdf"
GoTo :EOF

:RNs
REM Rename & Move Self ID
Set /A CountS=%CountS%+1
If %CountS% lss 1000 Set Ext=
If %CountS% lss 100 Set Ext=0
If %CountS% lss 10 Set Ext=00
move %1 "c:\2\SelfID\%employee%\%employee% Self ID_%Ext%%CountS%.pdf"
GoTo :EOF

:END


Report •
Related Solutions


Ask Question