I've spent two days on this and have looked at most every forum file I can find. Here's the scenario:
I have tens of thousands of image files: img_123.jpg, img_124.cr2, img_125.mov, etc. and I need them named: 64,001.jpg 64,002.cr2 64,003.mov and so on. Basically renaming the file while keeping the original extension in tact, while putting a comma in the thousands position.
Thanks to reading through the helpful Q&A's on Stack Overflow, I was able to write something that either A) renames the files serially but with no comma (64001.jpg 64002.cr2 64003.mov, etc.); or B) renames only the very first file for a unique type (img_123.jpg becomes 64,001.jpg just fine, while img_124.jpg img_125.jpg, etc. each fail with an error saying the filename is not unique.
Here's my code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\sourcedir"
SET /a num=64000
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
IF !num! lss 1000000 (
SET "newname=!num:~0,-3!,!num:~-3!"
) ELSE (
SET "newname=!num:~0,-6!,!num:~-6,3!,!num:~-3!"
)
ECHO REN "%sourcedir%\%%a" "!newname!%%~xa"
SET /a num+=1
)
GOTO :EOF
All you'd need to do is set up sourcedir to suit your system.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Here's what worked, thanks to Magoo for coming up with 95% of this.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=G:\ch\dos batch tests\# Projects\see if you can insert a coma"
SET /p num=enter the next higher image number here:
FOR /f "delims=" %%a IN (
'dir *.jpg *.cr2 *.mov /b /a-d "%sourcedir%\*.jpg *.cr2 *.mov" '
) DO (
IF !num! lss 1000000 (
SET "newname=!num:~0,-3!,!num:~-3!"
) ELSE (
SET "newname=!num:~0,-6!,!num:~-6,3!,!num:~-3!"
)
REN "%sourcedir%\%%a" "!newname!%%~xa"
SET /a num+=1
)
GOTO :EOF
Related
I'm brand new to batch scripting so I appreciate any help. I've seen similar problems here but can't get my move function to work.
I have files with the following format:
19013_01-PG-18-1000_NC_IL2RG_Ex2_F_D01.ab1
19013_01-PG-18-1000_NC_IL2RG_Ex2_R_H01.ab1
I want to make folders with the following format:
01-PG-18-1000_NC_IL2RG_Ex2
And then move all the files that have *01-PG-18-1000_NC* into that folder name.
Here's what I have so far. It's making the folders the way I want, but I can't get the files to move at all. Tried multiple iterations of the move function, but I'm not totally understanding the tokens and how it relates to the files for moving.
#ECHO OFF
SETLOCAL
SET "sourcedir="whatever my directory name is"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4,5 delims=_" %%a IN (
'dir /b /a-d "*_*-*-*-*_*_*_*.*"'
) DO (
MD %%b_%%c_%%d_%%e 2>nul
MOVE "%%b_%%c_%%d_%%e" "%%b_%%c_%%d_%%e"
)
POPD
GOTO :EOF
Real quickie - and untested
#ECHO OFF
SETLOCAL
SET "sourcedir="whatever my directory name is"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4,5,* delims=_" %%a IN (
'dir /b /a-d "*_*-*-*-*_*_*_*.*"'
) DO (
echo ++%%a++%%b++%%c++%%d++%%e++%%f++
MD %%b_%%c_%%d_%%e 2>nul
MOVE "%%a_%%b_%%c_%%d_%%e_%%f" "%%b_%%c_%%d_%%e"
)
POPD
GOTO :EOF
You were very close - the echo line should show you how the filename is parsed into %%a..%%f. The parts separated by ++ which is simply a very obvious separator and shows whether there are spaces in any element.
Adding * to the token-list means "everything after the highest-mentioned token number". Then reconstruct the filename from the parts - string together starting at %%a, re-inserting all the underscores.
This should work also:
#ECHO OFF
SETLOCAL
SET "sourcedir="whatever my directory name is"
PUSHD %sourcedir%
FOR /f %%q IN (
'dir /b /a-d "*_*-*-*-*_*_*_*.*"'
) do (
FOR /f "tokens=1,2,3,4,5,* delims=_" %%a IN ("%%q") do
echo ++%%a++%%b++%%c++%%d++%%e++%%f++FROM++%%q++
MD %%b_%%c_%%d_%%e 2>nul
MOVE "%%q" "%%b_%%c_%%d_%%e"
)
)
POPD
GOTO :EOF
In this version, %%q acquires each filename in turn, then "%%q" can be parsed by for /f and the original filename remains unmolested in %%q ready for use in the move statement.
[actually, quite minor] Revision:
REM <!-- language: lang-dos -->
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR /f "tokens=1,2,3,4,5,* delims=_" %%a IN (
'dir /b /a-d "*_*-*-*-*_*_*_*.*"'
) DO IF EXIST "%%a_%%b_%%c_%%d_%%e_%%f" (
MD %%b_%%c_%%d_%%e 2>nul
MOVE "*%%b_%%c*" "%%b_%%c_%%d_%%e"
)
POPD
SET "sourcedir=U:\sourcedir\t h r e e"
PUSHD "%sourcedir%"
FOR /f %%q IN (
'dir /b /a-d "*_*-*-*-*_*_*_*.*"'
) do IF EXIST "%%q" (
FOR /f "tokens=1,2,3,4,5 delims=_" %%a IN ("%%q") DO (
MD %%b_%%c_%%d_%%e 2>nul
MOVE "*%%b_%%c*" "%%b_%%c_%%d_%%e"
)
)
POPD
GOTO :EOF
Yes - misread that you wanted to move all files containing - been up gaming all night...
The above batch is in two sections, the first using %%a..%%f and the second incorporating %%q.
The difficulty faced is that move *[pattern]* will move all of the files, as desired BUT for /f...'dir... builds a list of ALL of the matching files that were originally in the directory.
Once the first filename is processed, those other files containing the 01-PG-18-1000_NC will have been moved, so you'll get a "no files found" error on the next 01-PG-18-1000_NC file in the the list for has built.
Sure, it's possible to cruft together some mechanism for ensuring that the pattern 01-PG-18-1000_NC is only processed once, but a simple if exists for the full filename returned by for...%%a... and rebuilt can be used to gate the MD/MOVE commands as the file will no longer exist when the next 01-PG-18-1000_NC is processed (as it's already been moved). So much easier using the %%q method though.
Of course, you could also simply dispose of the error messages instead of installing a gate - but that's probably regarded as being crude.
Also aschipfl's suggestion of adding delims= to the for...%%q... is quite valid if you have separators like Space in your filenames (and costs nothing, regardless)
Oh - and I'd missed the ( in the do part in my initial response - always happens when you don't actually test the code, especially when you're tired.
I'm writing batch script which I'll use to copy files from location A to location B with rename of a source file from location A if the same file exists already in location B.
Currently Im using snippet from another topic here on stack but it doesnt work on files from subfolders, could anyone help me with code below so it work on all files and subdirectiories from both locations? Many thanks!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
SET /a count=0
for %%c in (%sourcedir%\*.*) do (
CALL :select
ECHO copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s
)
GOTO :EOF
:select
SET /a count+=1
IF EXIST "%destdir%\%%c" GOTO select
GOTO :eof
Replace your for loop with the following for loop:
for /R "%sourcedir%" %%c in (*.*) do (what you like)
Also, why do you want the following piece of code?
copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s
Just copy "%%c" %destdir%
More generally you can write:
#ECHO OFF
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
:: SET /a count=0
for /R "%sourcedir%" %%c in (*.*) do (
:: SET /a count+=1
IF NOT EXIST "%destdir%\%%c" (
echo copy "%%c" %destdir%
)
)
Hope you are fine with this, possible dublicate of Windows batch file with loop through subfolders
Sharing with what I managed to get so far, works for what I needed, however still not doing well for subfolders:
#ECHO OFF
SET "sourcedir= "
SET "destdir= "
SET "HH=%TIME:~0,2%"
SET "MM=%TIME:~3,2%"
SET "SS=%TIME:~6,2%"
SET "_Time=%HH%%MM%%SS%"
FOR /R "%sourcedir%" %%G IN (*.*) DO (
IF EXIST "%destdir%\%%~nG%%~xG" (
COPY /V /Z "%%G" "%destdir%\%%~nG_duplicate_%_Time%%%~xG"
) ELSE (
COPY /V /Z "%%G" "%destdir%\%%~nG%%~xG")
)
I need a big help from the community, please if somebody can give me some hints. I have the following windows batch script which is supposed to read more than 10 million records as different CSV files and merge them all together. I am running the script on the server. So it's not very slow. But the problem is that the code doesn't handle duplicated records. I am not sure how to change the script in order to handle the duplication records and only passed unique records. I would be very very appreciated for your help.
rem Set current working directory to Task folder
set FilePath=%~dp0
set FolderPath=%FilePath:~0,-1%
rem Set Space environment variables
call "%FolderPath%"\..\SpaceEnv.bat
rem Set Task specific environment variables
set TaskName=MergeCSVfiles
set fileName=result.csv
set LogFile=%TaskName%_%LogDateTime%.log
:begin
cd ..
cd "Source Files\DCM_Source\Inbox"
echo Staring merge %fileName% at: %time%
setlocal enabledelayedexpansion
set "first=1"
>%fileName% (
for %%F in (msource*.csv) do (
if not "%%F"=="%fileName%" (
set /p "header="<"%%F"
if defined first (
type "%%F"
set "first="
) else (
type "%%F" |find /V "!header!"
)
)
)
)
endlocal
echo Finish merging %fileName% at: %time%
******UPDATED******
Example of CSV file
Sites|Level 2 sites|Date-time (visit start)|Visit ID|Unique visitor ID|Date-time (event)|Sources|Visitor categories|Visitor ID|Visits
SE Romania|PRM|2018-01-01T00:30:04|1|-6427177464|2018-01-01T00:30:04|Portal sites|-|0|2
SE Romania|PRM|2018-01-01T00:30:04|1|-6427177464|2018-01-01T00:30:04|Portal sites|-|0|2
This code will dedupe a file. In order to do that it must be sorted. This means any header record at the top of the file will be sorted into the file. This is code I received from dbenham. I can't remember if he originally posted it on StackOverflow or DosTips.com. If the file is very large it will more than likely crash with an out of memory error.
#echo off
:: Call function to dedupe file
CALL :DEDUPE "filename.txt"
goto :eof
:DEDUPE
:: DEDUPE file
setlocal disableDelayedExpansion
set "file=%~1"
set "sorted=%file%.sorted"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^
::The 2 blank lines above are critical, do not remove
sort "%file%" >"%sorted%"
>"%deduped%" (
set "prev="
for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%sorted%") do (
set "ln=%%A"
setlocal enableDelayedExpansion
if /i "!ln!" neq "!prev!" (
endlocal
(echo %%A)
set "prev=%%A"
) else endlocal
)
)
>nul move /y "%deduped%" "%file%"
del "%sorted%"
GOTO :EOF
#ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filenamecommon=q49264647*.csv"
:: switch to required source directory
PUSHD "%sourcedir%"
:: get header line
FOR %%f IN (%filenamecommon%) DO FOR /f "delims=" %%h IN (%%f) DO SET "header=%%h"&goto gotheader
:gotheader
COPY %filenamecommon% atempfilename
SET "lastline="
>resultfilename (
ECHO %header%
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%d IN ('sort atempfilename' ) DO (
IF "%%d" neq "!lastline!" IF "%%d" neq "%header%" ECHO %%d
SET "lastline=%%d"
)
endlocal
)
DEL atempfilename
popd
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used file/dirctorynames that suit my system for testing.
Note : datafiles containing the characters ! or ^ or unbalanced " will not be processed correctly.
First, find the header line by setting header from any matching filename. Once header is set, forcibly abort the for loops.
copy and concatenate all of the required files to a tempfile.
output the header line, then sort the tempfile to group identical lines. Read the result and output only those lines that differed from the previous and were not header lines.
Applying /i to the if statements will make the entire routine disregard character-case.
Sort the tempfile
Ok. Give this code a try. I think this code would generate the result file with not duplicated records not matters its size. However, the time the program will take depends on several factors, although IMHO it should not be excessive because the core part of the process is based on findstr.exe command.
#echo off
setlocal
del result.csv 2>NUL
rem Process all input files
for /F "delims=" %%f in ('dir /B /O:-S msource*.csv') do (
echo Merging file: %%f
if not exist result.csv (
rem Initialize output file with first input file
copy "%%f" result.csv > NUL
) else (
rem Get records in this file that are not in result file
findstr /V /G:result.csv "%%f" > newRecords.csv
rem and add they to the result file
type newRecords.csv >> result.csv
)
)
del newRecords.csv
You may also try to eliminate the dash in /O:-S switch of dir command; perhaps this change will speed up the process a little...
I have a folder with 46 different text files, from 001.txt to 046.txt and I need to add another file to, say, spot 30. Is there a ways to rename all the files from 030.txt until 046.txt up by one number, so there is an empty spot for the new 030.txt? (Operating on Windows 7)
You can use PowerShell, which is build-in in Windows 7:
46..30|Rename-Item -Path {'{0:000}.txt'-f$_} -NewName {'{0:000}.txt'-f($_+1)}
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "insertat="
SET /p "insertat=Insert at which number ? "
IF NOT DEFINED insertat GOTO :EOF
SET /a insertat1=1%insertat%
SET /a howmany=1
SET /p "howmany=Insert How many ? [%howmany%]"
IF "%howmany%"=="0" GOTO :EOF
FOR /f "delims=" %%a IN (
'dir /b /o-n /a-d "%sourcedir%\*.txt" '
) DO (
CALL :isnum %%~na
IF NOT DEFINED notnumber SET /a maxnum=1%%~na&GOTO insert
)
ECHO maxnum NOT found
GOTO :eof
:insert
SET /a newnum=maxnum + howmany
IF EXIST "%sourcedir%\%maxnum:~1%.txt" ECHO(REN "%sourcedir%\%maxnum:~1%.txt" %newnum:~1%.txt
SET /a maxnum -=1
IF %maxnum% GEQ 1%insertat% GOTO insert
GOTO :EOF
:: Determine whether %* is purely numeric
:isnum
SET "notnumber=%~2"
IF DEFINED notnumber GOTO :EOF
SET "notnumber=9%~1"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
GOTO :eof
I may as well post this regardless.
It automatically locates the highest filenumber and allows any number of slots to be inserted (default of 1 for Enter)
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Here is a simple pure batch solution
#echo off
setlocal enableDelayedExpansion
set increment=1
set "start=030"
for /f %%F in (
'dir /b /a-d /o-n ???.txt^|findstr /rx "[0-9][0-9][0-9]\.txt"'
) do if "%%~nF" geq "%start%" (
set /a new=1%%~nF+increment
ren %%F !new:~1!%%~xF
)
i am writing a batch script monotonic file renamer. basically, it makes the titles of all the files 1 2 3 4 .... and so on. i have since expanded it to be able to handle files of different types (txt, doc, flv, etc) but not everything is working out.
my main concern is i have broken the delayed expansion calls i was making before. now using !var1! is never expanded, or never recognized as a variable.
here is a verbosely commented version of my script
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET tempfile=temp.txt
SET exttemp=exttemp.txt
if [%1] == [] goto usage
::make sure your dont overwrite something useful
if EXIST %tempfile% (
ECHO Temp file already exists, are you sure you want to delete?
del /P %tempfile%
)
if EXIST %exttemp% (
ECHO EXT Temp file already exists, are you sure you want to delete?
del /P %exttemp%
)
::initialize
SET /a counter=0
SET type=
SET /a ender=%1
::write filenames to tempfile
DIR /B /ON > %tempfile%
::read lines one by one
for /f "usebackq delims=" %%a in (%tempfile%) do (
REM make sure we do not rename any of the working files
if NOT "%%a"=="renamer.bat" (
if NOT "%%a"=="temp.txt" (
if NOT "%%a"=="exttostr.bat" (
SET /a counter+=1
REM get file extension
exttostr %%a > %exttemp%
SET /P type= < %exttemp%
REM housekeeping
del /F %exttemp%
REM rename
ren %%a !counter!.!type!
ECHO Renamed "%%a" to "!counter!.!type!"
)))
REM exit when we have run enough
if "!counter!"=="!ender!" goto exit
)
goto exit
:usage
echo Usage: renamer NUMFILES
:exit
::final housekeeping
DEL temp.txt
the idea is i drop my two files, renamer.bat(this file) and exttostr.bat(helper to get the file extension) into the folder and run it, it will rename files sorted alphabetically from 1 to how ever many files i specify.
when i run the code, it never uses the variables marked for delayed expansion appropriately, always leaving them as "!varname!", so it renames the first file "!counter!.!type!" and throws errors for the rest because there is already a file in the directory with that name.
this brings me to a secondary issue. sorting the dir list alphabetically results in a poor handling of numbered files. for example the list:
"1 7 15 75 120"
is sorted:
"1 120 15 7 75"
i have not been able to find a way around this yet, only that it is indeed the intended result of the dir sort. the only workaround i have is padding numbers with enough zeroes in the front.
thanks in advance for any insight!
everything is sorted but the second problem. i think i have not spoken well. i have this issue when i take IN the directory file names, not when writing out. so they already need to be padded. i has hoping there was some other way to read the directory and have it be sorted appropriately.
the most promising thing i have found is here: http://www.dostips.com/DtCodeBatchFiles.php#Batch.SortTextWithNumbers
#ECHO OFF
if "%~1"=="/?" (
echo.Sorts text by handling first number in line as number not text
echo.
echo.%~n0 [n]
echo.
echo. n Specifies the character number, n, to
echo. begin each comparison. 3 indicates that
echo. each comparison should begin at the 3rd
echo. character in each line. Lines with fewer
echo. than n characters collate before other lines.
echo. By default comparisons start at the first
echo. character in each line.
echo.
echo.Description:
echo. 'abc10def3' is bigger than 'abc9def4' because
echo. first number in first string is 10
echo. first number in second string is 9
echo. whereas normal text compare returns
echo. 'abc10def3' smaller than 'abc9def4'
echo.
echo.Example:
echo. To sort a directory pipe the output of the dir
echo. command into %~n0 like this:
echo. dir /b^|%~n0
echo.
echo.Source: http://www.dostips.com
goto:EOF
)
if "%~1" NEQ "~" (
for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
set f=,%%B
(
set f0=!f:~0,%n%!
set f0=!f0:~1!
rem call call set f=,%%%%f:*%%f0%%=%%%%
set f=,!f:~%n%!
)
for /f "delims=1234567890" %%b in ("!f!") do (
set f1=%%b
set f1=!f1:~1!
call set f=0%%f:*%%b=%%
)
for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`##$*_-+=:;',.?/\ " %%b in ("!f!") do (
set f2=00000000000000000000%%b
set f2=!f2:~-20!
call set f=%%f:*%%b=%%
)
echo.!f1!!f2!!f!,%%B
rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)
this code can sort the filenames with one number in them (i.e. video100.mov is fine, video100video10.mov would break it)
the issue i have is i think adding a call to this helper fn will break it again, so i will be trying to include this in my modified renamer.bat now. any help is appreciated.
Probably the batch for extracting the extension reset the local environment.
But, you don't need it. You may extract the extension with the ~x option. Something similar to this ....
:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
if exist %%~fa (
set /a counter += 1
echo ren %%~fa !counter!%%~xa
)
)
goto :eof
to include leading zeroes in the counter, so that the directory sorts correctly, replace the previous rename command with three lines
set zcounter=0000!counter!
set zcounter=!zcounter:~-4!
echo ren %%~fa !counter!%%~xa
So putting all pieces together, add the monotonicrename function you just created in the batch file that can be as simpler as...
#echo off
setlocal enabledelayedexpansion
call :monotonicrename %1
goto :eof
:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
if exist %%~fa (
set /a counter += 1
set zcounter=0000!counter!
set zcounter=!zcounter:~-4!
echo ren %%~fa !zcounter!%%~xa
)
)
goto :eof
I didn't experience any issues with delayed expansion, everything worked fine for me (except, of course, for the fact that I didn't have the exttostr.bat helper script.)
Anyway, there are several things that could be improved about your script:
You don't need to store the result of DIR into a file to read it afterwards. You can read the output directly in the FOR loop.
You don't need the helper batch script. The extension can be extracted from %%a by using the ~x modifier with the loop variable: %%~xa. You can read more about modifiers by issuing HELP FOR from the command prompt.
The renamer batch file's own name can be referenced in the script as %0. You can apply the ~n modifier where you only need to use the name without the extension. The combined modifier of ~nx will give you the name with the extension.
So, here's how your script might look like with the above issues addressed:
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
IF [%1] == [] GOTO usage
::initialize
SET /A counter=0
SET type=
SET /A ender=%1
::read lines one by one
FOR /F "usebackq delims=" %%a IN (`DIR /B /ON`) DO (
REM make sure we do not rename any of the working files
IF NOT "%%~a"=="%~nx0" (
SET /A counter+=1
RENAME "%%~a" "!counter!%%~xa"
ECHO Renamed "%%~a" to "!counter!%%~xa"
)
REM exit when we have run enough
IF "!counter!"=="!ender!" GOTO :EOF
)
GOTO :EOF
:usage
ECHO Usage: %~n0 NUMFILES
As for your secondary issue, it can be easily resolved like this:
Use something like 100000 as counter's initial value. (Use however many 0s you like, but possibly no more than nine.) Add the same value to ender as well.
When renaming files, instead of !counter! use the expression that removes the first character (the 1): !counter:~1! (in fact, this is not about removal, but about extracting a substring starting from the offset of 1, learn more about it with the HELP SET command).
Here's the modified version of the above script:
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
IF [%1] == [] GOTO usage
::initialize
SET /A counter=1000
SET type=
SET /A ender=%1
SET /A ender+=counter
::read lines one by one
FOR /F "usebackq delims=" %%a IN (`DIR /B /ON`) DO (
REM make sure we do not rename any of the working files
IF NOT "%%~a"=="%~nx0" (
SET /A counter+=1
RENAME "%%~a" "!counter:~1!%%~xa"
ECHO Renamed "%%~a" to "!counter:~1!%%~xa"
)
REM exit when we have run enough
IF "!counter!"=="!ender!" GOTO :EOF
)
GOTO :EOF
:usage
ECHO Usage: renamer NUMFILES
You can also see that I made some other enhancements, like making sure the file name is enclosed in double quotes, and using GOTO :EOF instead of GOTO exit (:EOF is a special pre-defined label that points at the end of the batch script so you don't need to define your own).