loop through filename-mask with numbers

  • Thread starter Thread starter Jens Richter
  • Start date Start date
J

Jens Richter

In a directory, 1000 files of ABCxxx.TXT may exist, where xxx are
numbers 0...9, i.e. the files may be ABC000.TXT ... ABC999.TXT.
The BATch program should find the first file in this sequence, which
does not exist yet in the directory.

Hence, the check must begin at ABC000.TXT, has to loop until ABC999.TXT
and must exit at that file which does not exist yet in the directory.

Could anybody give a helful hint how to accomplish that ?

Thank you very much in advance !

Jens Richter
 
Jens said:
In a directory, 1000 files of ABCxxx.TXT may exist, where xxx are
numbers 0...9, i.e. the files may be ABC000.TXT ... ABC999.TXT.
The BATch program should find the first file in this sequence, which
does not exist yet in the directory.

Hence, the check must begin at ABC000.TXT, has to loop until ABC999.TXT
and must exit at that file which does not exist yet in the directory.

Could anybody give a helful hint how to accomplish that ?

Thank you very much in advance !

Jens Richter

=====begin c:\cmd\demo\FindNextNumber.cmd ====================
01. @echo off
02. setlocal
03. set next=
04. for /l %%a in (1000,1,1999) do call :check %%a
05. set next
06. goto :EOF
07. :check
08. set number=%1
09. if exist c:\yourpath\abc%number:~1%.txt goto :EOF
10. if not defined next set next=%number:~1%
11. goto :EOF
=====end c:\cmd\demo\FindNextNumber.cmd ====================
 
Thank you very much for this work, Phil !
The code returns what it should return.
However, I couldn't get rid of the loop until 1999 - independently of
the result.
I.e., if the right result is ABC004.TXT, the code loops nevertheless
until 1999.
Have you a hint for avoiding this ?

Thank you in advance !
Regards
Jens Richter
 
Jens said:
Thank you very much for this work, Phil !
The code returns what it should return.
However, I couldn't get rid of the loop until 1999 - independently of
the result.
I.e., if the right result is ABC004.TXT, the code loops nevertheless
until 1999.
Have you a hint for avoiding this ?

You can't avoid it. There is no way to 'break out' before 1999. But
you could do something like the following before the loop to set the
start value upwards or the end value downwards. I also changed line 13
so that the batch file will return the value of 'next' in environment
variable 'FindNextNumber'.

=====begin c:\cmd\demo\FindNextNumber.cmd ====================
01. @echo off
02. setlocal
03. set next=
04. if exist d:\junkdir\abc500.txt (
05. set start=1500
06. set max=1999
07. ) else (
08. set start=1000
09. set max=1501
10. )
11. for /l %%a in (%start%,1,%max%) do call :check %%a
12.
13. endlocal&set %~n0=%next%&goto :EOF
14. :check
15. set number=%1
16. if exist d:\junkdir\abc%number:~1%.txt goto :EOF
17. if not defined next set next=%number:~1%
18. goto :EOF
=====end c:\cmd\demo\FindNextNumber.cmd ====================
 
Jens said:
Thank you very much for this work, Phil !
The code returns what it should return.
However, I couldn't get rid of the loop until 1999 - independently of
the result.
I.e., if the right result is ABC004.TXT, the code loops nevertheless
until 1999.
Have you a hint for avoiding this ?

Thank you in advance !
Regards
Jens Richter

Assuming that the ABC*.txt files are always created in chronological sequence,
that is, ABC234.txt is always created after ABC233.txt, then you could also
use the following approach to find the next number without the 'FOR /l' loop.
This is much faster, but this particular version requires that you be running
either Win2000 or WinXP (I forget whether you said which version of NT you
were using). If you like this method better but are using NT4.0, let me know
and I will post another version that will work on all three (NT 4.0 does not
have 'set /p' in line 4 so another method is needed).

=====begin c:\cmd\demo\FindZLastNum.cmd ====================
1. @echo off
2. setlocal
3. dir /o-d /b d:\yourpath\abc*.txt > %temp%\zwork.001
4. set /p last=<%temp%\zwork.001
5. set /a next=1%last:~3,3% + 1
6. set next=%next:~1%
7. set next
=====end c:\cmd\demo\FindZLastNum.cmd ====================
 
Jens said:
Thank you very much for this work, Phil !
The code returns what it should return.
However, I couldn't get rid of the loop until 1999 - independently of
the result.
I.e., if the right result is ABC004.TXT, the code loops nevertheless
until 1999.
Have you a hint for avoiding this ?

Thank you in advance !
Regards
Jens Richter

Hi, Jens:

Here's another version that avoids using 'FOR /l . . .' and should thus
exit much earlier if the number of files is low. It will work in any
version of NT.

=====begin c:\cmd\demo\LoopToFindLastFile.cmd ====================
01. @echo off
02. setlocal
03. set start=999
04. set next=
05. :loop
06. set /a start += 1
07. if not exist d:\yourpath\abc%start:~1%.txt set next=%start%
08. if not defined next goto :loop
09. set next=%next:~1%
10. set next
=====end c:\cmd\demo\LoopToFindLastFile.cmd ====================
 
That's it - Magnifico ! Phil, thank you yery much for your continued
efforts !!!
I'm happy, that you helped me here - I wouldn't had been able to solve
this problem for myself.

Regards with best wishes for you
Jens
 
Back
Top