Batch file wizardry...
Author
Discussion

Dave.

Original Poster:

7,903 posts

284 months

Tuesday 13th November 2018
quotequote all
Afternoon all,

I'm looking to create a batch file which will write a series of reference numbers to a text file.

Ie

ABC-123-DEF
ABC-124-DEF
ABC-125-DEF

I want to have a prompt for the first reference, then a prompt for the number of references.

I can do basic stuff with bat files, but I have no idea how to create a loop for n number of lines +1.

My googlefu is weak today.

Thanks in advance.

crmcatee

5,808 posts

258 months

Tuesday 13th November 2018
quotequote all
Excel, cut and paste into a blank notepad file. Save.


Job done.

Dave.

Original Poster:

7,903 posts

284 months

Tuesday 13th November 2018
quotequote all
That's what I was doing when it struck me it would be easier as a batch file....

Thanks though.

Sparkov

130 posts

164 months

Tuesday 13th November 2018
quotequote all
I'm not sure how to go about it with just a single input reference as that would involve analysing and breaking up a string into various parts etc. but here's an example that will ask for a prefix, suffix and start/end number:

@echo off
set /p prefix=Prefix:
set /p suffix=Suffix:
set /p start=Start number:
set /p end=End number:

FOR /L %%i IN (%start%,1,%end%) DO (
ECHO %prefix%%%i%suffix% >> file.txt
)
ECHO Done! Output written to 'file.txt'

Dave.

Original Poster:

7,903 posts

284 months

Tuesday 13th November 2018
quotequote all
Legend! Thanks!

Dave.

Original Poster:

7,903 posts

284 months

Tuesday 13th November 2018
quotequote all
I take that back, wink does everything but write the file.... Fiddled with it for 15mins and can't get it going....

Sparkov

130 posts

164 months

Tuesday 13th November 2018
quotequote all
That's weird, works fine for me. How are you running the .bat file?

robbo145

5 posts

186 months

Tuesday 13th November 2018
quotequote all
Yes just tried and works for me also...takes a few minutes to complete text file. I saved in notepad as .bat then ran from cmd prompt.

Penelope Stopit

11,209 posts

140 months

Tuesday 13th November 2018
quotequote all
This is a very good online tool if you are ever stuck with text manipulation
https://textmechanic.com/

See Text Tools - Basic Text Tools

Merge text line by line is amazing https://textmechanic.com/text-tools/basic-text-too...

Sparkov

130 posts

164 months

Tuesday 13th November 2018
quotequote all
robbo145 said:
Yes just tried and works for me also...takes a few minutes to complete text file. I saved in notepad as .bat then ran from cmd prompt.
I don't experience a delay at all, the text file appears instantaneously. Just tried it with 10,000 iterations and even that only took a few seconds. Maybe it's just Windows Explorer not refreshing quickly enough?

Dave.

Original Poster:

7,903 posts

284 months

Tuesday 13th November 2018
quotequote all
Sparkov said:
That's weird, works fine for me. How are you running the .bat file?
Typed it out, changed the save location to c:\users\%username%\desktop\file.text

I even put a 'pause' between each line, no errors, just no output file confused

I've copied your code and emailed it to myself, I'll try again in the morning.

Sparkov

130 posts

164 months

Wednesday 14th November 2018
quotequote all
Dave. said:
Sparkov said:
That's weird, works fine for me. How are you running the .bat file?
Typed it out, changed the save location to c:\users\%username%\desktop\file.text

I even put a 'pause' between each line, no errors, just no output file confused

I've copied your code and emailed it to myself, I'll try again in the morning.
When you say you typed it out, are you copying the text from my first post into notepad and then saving as a *.bat file?

Dave.

Original Poster:

7,903 posts

284 months

Wednesday 14th November 2018
quotequote all
Yup.

Tried the copy and paste, same result, no output file....

Tried a straight "ECHO 123 >> file.txt and that worked fine....

Like a sump, I'm baffled....

Sparkov

130 posts

164 months

Wednesday 14th November 2018
quotequote all
Dave. said:
Yup.

Tried the copy and paste, same result, no output file....

Tried a straight "ECHO 123 >> file.txt and that worked fine....

Like a sump, I'm baffled....
You're not putting in a smaller number for 'End number' than you put in for 'Start number'? That's the only way I can get it to not create a file.

TonyRPH

13,552 posts

199 months

Wednesday 14th November 2018
quotequote all
Working fine for me on Windows 7

I named my batch file with a 'cmd' extension by force of habit, but I'm sure it makes no difference.

EDIT: It works just as well with a .bat extension.

EDIT(2): OP, you could try changing this line:

"ECHO %prefix%%%i%suffix% >> file.txt"

to

"ECHO %prefix%%%i%suffix%"

And then the result should just echo out to the console rather than a file - and see if you get anything? Keep the start / finish to less than 10 lines though!


C:\Temp\test>t.cmd
Prefix:ABC
Suffix:DEF
Start number:1
End number:20
Done! Output written to 'file.txt'

C:\Temp\test>type file.txt
ABC1DEF
ABC2DEF
ABC3DEF
ABC4DEF
ABC5DEF
ABC6DEF
ABC7DEF
ABC8DEF
ABC9DEF
ABC10DEF
ABC11DEF
ABC12DEF
ABC13DEF
ABC14DEF
ABC15DEF
ABC16DEF
ABC17DEF
ABC18DEF
ABC19DEF
ABC20DEF


Edited by TonyRPH on Wednesday 14th November 16:50

Dave.

Original Poster:

7,903 posts

284 months

Wednesday 14th November 2018
quotequote all
Sparkov said:
Dave. said:
Yup.

Tried the copy and paste, same result, no output file....

Tried a straight "ECHO 123 >> file.txt and that worked fine....

Like a sump, I'm baffled....
You're not putting in a smaller number for 'End number' than you put in for 'Start number'? That's the only way I can get it to not create a file.
Start 001
End 009


Dave.

Original Poster:

7,903 posts

284 months

Wednesday 14th November 2018
quotequote all
Balls!

It appear the three digit padding is messing it up somewhat!

Thanks all, I'm an ejit....