• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Batch Renamer that appends Index for Windows 2003

I need a batch file that fill rename this:
lonewolf 2006-06-04 23;00;01.zip
lonewolf 2006-06-05 23;00;01.zip
quickbooks 2006-06-04 23;30;01.zip
quickbooks 2006-06-05 23;30;01.zip
showingdesk 2006-05-09 00;00;01.zip
showingdesk 2006-05-10 00;00;01.zip

into this:
lonewolf-1.zip
lonewolf-2.zip
quickbooks-1.zip
quickbooks-2.zip
showingdesk-1.zip
showingdesk-2.zip

The way I thought of it:
- A for loop that goes through the list
- Grabs the first word of each filename as variable A, appends "-n", where in is increasing starting from 1
- It has to verify when variable A changes, in order to reset the counter back to 1

That's all :).
Thanks guys.
Again it has to be a batch file, and it has to run on Windows 2003.
0
gigelkent
Asked:
gigelkent
1 Solution
 
oBdACommented:
That should do the trick; just run it in the folder where your files are. Note that the script is currently in test mode, it will only display the rename commands it would otherwise run; remove the "ECHO" where indicated to run it for real once you've tested it.

@echo off
setlocal
set CurrentName=
set /a Index=1
for /f "delims=" %%a in ('dir /b /o:n /a:-d *.zip') do call :process "%%a"
goto :eof

:process
set FullName=%~1
set NewExt=%~x1
for /f "tokens=1 delims=. " %%a in ("%FullName%") do set NewName=%%a
if /i not "%CurrentName%"=="%NewName%" (
  set /a Index=1
  set CurrentName=%NewName%
)
:: *** Test mode: remove the "ECHO" in front of the following line to run the script for real:
ECHO ren "%FullName%" "%NewName%-%Index%%NewExt%"
set /a Index+=1
goto :eof
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now