netcmh
asked on
batch file number generator
Hello,
I need to be able to generate a sequence of numbers like this:
1-001
1-002
1-003
....
1-100
2.001
2.002
.....
2.100
3.001
.....
9.001
.....
9.100
10.001
......
10.100
.....
100.001
....
100.100
.....
3000.100
How do I go about doing that using a batch file?
I don't necessarily need the leading zeros. It would be nice to have, but not essential.
Thanks in advance.
I need to be able to generate a sequence of numbers like this:
1-001
1-002
1-003
....
1-100
2.001
2.002
.....
2.100
3.001
.....
9.001
.....
9.100
10.001
......
10.100
.....
100.001
....
100.100
.....
3000.100
How do I go about doing that using a batch file?
I don't necessarily need the leading zeros. It would be nice to have, but not essential.
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice, I forgot about just adding leading zeros and taking the "back-end" of the variable!
If you're trying to write results to a text file....
@echo off
ECHO Writing loop counter to text file...
for /L %%G IN (1,1,3000) DO (
for /L %%H IN (1,1,100) DO ECHO %%G.%%H >> C:\textfile.txt
)
ECHO You will find loop counter at 'C:\textfile.txt'...
pause
Of course you could also send the output from oBda's post to a file by running the BAT file and redirecting the output, like below. This let's you test it to the screen, then capture the output easily to a file when needed.
myfile.bat > myfile.txt
~bp
myfile.bat > myfile.txt
~bp
Can just as well do it like this:
@echo off
setlocal enabledelayedexpansion
for /l %%a in (1,1,3000) do (
for /l %%b in (1001,1,1100) do (
set /a number=%%b
echo %%a.!number:~1!
)
)
I have to give it to you on that one Paul, well done.
~bp
~bp
Thank you bill.
If the asker had asked for:
0001.001
:
3000.100
Then I would have given this:
If the asker had asked for:
0001.001
:
3000.100
Then I would have given this:
@echo off
setlocal enabledelayedexpansion
for /l %%a in (10000,1,13000) do (
set /a a=%%a
for /l %%b in (1001,1,1100) do (
set /a b=%%b
echo !a:~1!.!b:~1!
)
)
ASKER
Thank you all for your help. I appreciate all the inputs.
I'm awarding the points to oBdA as it was the simplest and direct approach.
I'm awarding the points to oBdA as it was the simplest and direct approach.
But you could also use a couple of "for" statements and cycle through it in batch.
MUCH easier without the leading zeros:
Open in new window
The following includes the leading zeros.
Open in new window
Both examples above just display the output - if you need to do something more, in example 1, %%a = the first set of numbers (1-3000) and %%b = the second set of numbers, 1 to 100.
In the first example, you can specify maximum numbers of at least 32767 (maybe more, but I've not tried going beyond the basic integer value).
In the second example, because you want to maintain leading 0's, you can change the 3000 to a maximum of 9999 (without potentially getting inconsistent formatting) and the second set to 999, again, without potentially getting inconsistent formatting.