• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 730
  • Last Modified:

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.
0
netcmh
Asked:
netcmh
  • 2
  • 2
  • 2
  • +3
1 Solution
 
oBdACommented:
I'm assuming that the "-" in the first sequence is a typo, and the separator should be "." throughout?
@echo off
setlocal enabledelayedexpansion
for /l %%i in (1, 1, 3000) do (
	for /l %%j in (1, 1, 100) do (
		set Number=00%%j
		set Number=!Number:~-3!
		echo %%i.!Number!
	)
)

Open in new window

0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Nice, I forgot about just adding leading zeros and taking the "back-end" of the variable!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dimmergeekCommented:
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

0
 
Bill PrewCommented:
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
0
 
paultomasiCommented:
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

0
 
Bill PrewCommented:
I have to give it to you on that one Paul, well done.

~bp
0
 
paultomasiCommented:
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

0
 
netcmhAuthor Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now