Output parameter as incremental filename

February 21, 2010 at 20:35:44
Specs: Windows 7
I have an app that outputs a file and lets me specify the file name and path like:

app.exe -o D:\out\new001.txt

I want the naming to be like: new001.txt new002.txt new003.txt etc.

I would like to write a batch script to scan through D:\out\ and figure out what's the next filename to write

For example, if new001.txt through new080.txt and new091.txt exists in D:\out, do app.exe -o D:\out\new092.txt


See More: Output parameter as incremental filename

Report •


#1
February 22, 2010 at 04:44:19
It's an interesting question, but the most difficult feature is to convert 1 to 001, 2 to 002, ... etc.

Apart from THAT (somebody here will cover that) ...

@echo off

set filename_prefix=new
set extension=.txt
set cnt=1

:begin
if NOT exist %filename_prefix%%cnt%%extension% goto newfile
set /a cnt+=1
goto begin

:newfile
echo Found out %filename_prefix%%cnt%%extension% does not exist yet
echo xxx> %filename_prefix%%cnt%%extension%
goto :EOF


Report •

#2
February 22, 2010 at 08:30:41
It's an interesting question, but the most difficult feature is to convert 1 to 001, 2 to 002, ... etc.
set cnt=000%cnt%
set cnt=%cnt:~-3%


Report •

#3
February 23, 2010 at 02:31:20
By coincidence, somebody else was asking a similar question (in http://www.computing.net/answers/pr... ) and your solution got mentioned there ...

I like it, not a lot of coding ... but, one problem : what if number is bigger than 999 ?


Report •

Related Solutions

#4
February 23, 2010 at 02:59:12
Tested:

@echo off

set filename_prefix=new
set extension=.txt
set cnt=1

:begin
set cnt_word=0000%cnt%
set cnt_word=%cnt_word:~-4%
set newname=%filename_prefix%%cnt_word%%extension%
if NOT exist %newname% goto newfile
set /a cnt+=1
if %cnt% GTR 9999 goto error1
goto begin

:newfile
echo Found out %newname% does not exist yet
echo xxx> %newname%
goto :EOF

:error1
echo Max counter number reached, aborting actions
goto :EOF


Report •

#5
February 23, 2010 at 03:41:21
what if number is bigger than 999 ?
It won't be after you run those two lines. :V

But really, every program has built in limitations. If you need a number greater than 999, write your script that way.

set cnt=0000000000%cnt%
set cnt=%cnt:~-10%
Now you'll run into problems with 32-bit integers before the script's number pool runs out.

Report •

#6
February 24, 2010 at 18:02:13
Thanks so much! Works awesome

Report •

#7
February 25, 2010 at 04:09:41
> Now you'll run into problems with 32-bit integers
> before the script's number pool runs out.
>

Hmm OK ... so then the limit is 10**32 ? There must be a limit ...


Report •


Ask Question