Batch file wizardry...
Discussion
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.
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.
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'
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...
https://textmechanic.com/
See Text Tools - Basic Text Tools
Merge text line by line is amazing https://textmechanic.com/text-tools/basic-text-too...
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?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.textI even put a 'pause' between each line, no errors, just no output file

I've copied your code and emailed it to myself, I'll try again in the morning.
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.textI even put a 'pause' between each line, no errors, just no output file

I've copied your code and emailed it to myself, I'll try again in the morning.
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.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....
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!
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
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.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....
End 009
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



does everything but write the file.... Fiddled with it for 15mins and can't get it going....