Link to home
Start Free TrialLog in
Avatar of netcmh
netcmhFlag for United States of America

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Easiest thing to do would be an excel sheet exported to a CSV file and then use "FOR" to cycle through one file...

But you could also use a couple of "for" statements and cycle through it in batch.

MUCH easier without the leading zeros:
for /l %%a in (1,1,3000) do (
   for /l %%b in (1,1,100) do (
      echo %%a.%%b
   )
)

Open in new window


The following includes the leading zeros.

@echo off
SETLOCAL EnableDelayedExpansion 
For /l %%a in (1,1,3000) do (
	For /l %%b in (1,1,100) do (
		If %%a LSS 10 (
			Set Var1=000%%a
		) Else (
			If %%a LSS 100 (
				Set Var1=00%%a
			) Else (
				If %%a LSS 1000 (
					Set Var1=0%%a
				) Else (
					Set Var1=%%a
				)
			)
		)
		If %%b LSS 10 (
			Set Var2=00%%b
		) Else (
			If %%b LSS 100 (
				Set Var2=0%%b
			) Else (
				Set Var2=%%b
			)
		)
		Echo !Var1!.!Var2!
	)
)
EndLocal

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.
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

Open in new window

Avatar of Bill Prew
Bill Prew

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
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!
  )
)

Open in new window

I have to give it to you on that one Paul, well done.

~bp
Thank you bill.

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!
  )
)

Open in new window

Avatar of netcmh

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.