1

So basically I have this batch file:

@echo off bps -f bps.txt exit

Everytime I run the batch the new bps.txt file overwrites the old one. What I want is to somehow everytime I run it the new file must come out as e.g. bps1.txt . If I run it again it should come out as bps2.txt and so on. So for example if I run the batch 3 times, I would have bps.txt, bps1.txt, bps2.txt .

4 Answers 4

2

there is no build-in-function for this. Use a counter, check if the file exists, if not increment the counter and try again:

set "counter=" :SearchFreeFile if exist bps%counter%.txt ( set /a counter+=1 goto :SearchFreeFile ) bps -f bps%counter%.txt 
Sign up to request clarification or add additional context in comments.

1 Comment

I must say, this is much more elegant than my answer.
1

Looping is time consuming. If you expect that the filenames are created consecutively you can do it like this:

for /f %%a in ('dir /b bps*.txt^|find /c /v ""') do set /a next=%%a bps -f bps%next%.txt 

The for command here is NOT used to loop - it merely saves the output of the command in apostrophes in the %%a variable. With dir we list the existing files with that name pattern, the find command counts them. So if you already have 2 files of that name the output will be "2".

Different to your example the very first file will be named "bps0.txt", not "bps.txt". If you want to avoid this, use this instead:

for /f %%a in ('dir /b bps*.txt^|find /c /v ""') do set /a next=%%a if %next% EQU 0 (set bps=bps) else (set bps=bps%next%) bps -f %bps%.txt 

4 Comments

I thougt about that too. But what, if some files get deleted (because they are old)? This solution would then probably overwrite one of the existing files instead of creating a new file. (might or might not be a problem)
Would it help to determine the highest filenumber plus one as the next enumeration? That would leave gaps but that might be desired.
difficult because files are listed alphabetically (1. 10, 11. ...,19, 2, 20, 21, ...). Alternative: dir /od (newest file should be the one with the highest number, but this isn't true anymore, when files were edited in the meantime.)
Yes, I noticed that as well. Cleanest scheme would be to name the files 'bps.000', 'bps.001' etc. But then you're limited to 1000 files only.
0

You could do this:

@echo off if not exist "bps.txt" ( bps -f bps.txt exit ) for /l %%i in (1,1,10000) do ( set "number=%%i" if not exist "bps%%i.txt" goto :break ) echo 10000 files with this name already exist pause exit :break bps -f bps%number%.txt pause exit 

Comments

0

My previous answer takes the first "free" counter - if you delete some of the "old" files, it would fill the gaps with new files.

Another way (a bit unusual, but effective): use alternative datastream to "save" the counter. Adds +1 for every instance, independent of possible deletion of some (or all) previous files (at least until you reach the limits of Integer).

<%~f0:count set /p count= set /a count+=1 (echo %count%)>%~f0:count bps -f bps%count%.txt 

You can "reset" the counter with (echo 0)>mybatch.bat:count

(note: no check, if the file exists)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.