Page 1 of 1

For while loop or just a goto statement

Posted: 05 Oct 2018 10:03
by cvau89
I searched some but couldn't come up with the right key words to find what I'm looking for. I need to repeatedly run a process which creates one file, than another process converts that file to another format. So I believe i need a infinite for while loop where I can just CTR-C break out when I need. I know I could get more fancy - but I just need it to work. As I want to retain these two files on each iteration, I need some variable to start at zero or 1 or what ever, run my two processes and then rename the two files with the incrementing variable.

So first pass I have File.txt and NewFile.txt - they can be renamed File1.txt and NewFile1.txt -
Second pass process create new File.txt and Newfile.txt - variable increases by 1 so my rename would be File2.txt and NewFile2.txt.
until I break out.

All seems so simple in my head but my experience with batch file and vb scripting has been very basic run commands ect. I've read some things but truely getting a headache so I thought I'd ask for a friendly shove in the right direction.

Thanks in advance

Re: For while loop or just a goto statement

Posted: 05 Oct 2018 13:24
by aGerman
Sure you could use GOTO for an infinite loop. Another possibility is a FOR /L loop with an empty pair of parentheses.

Code: Select all

@echo off &setlocal
set /a n=0
for /l %%i in () do (call :sub &set /a n+=1)

:sub
REM Do whatever you need here
echo %n%
exit /b
Steffen

Re: For while loop or just a goto statement

Posted: 05 Oct 2018 13:45
by Squashman
The SET and FOR commands are both limited to 32 bit integers. So you can basically just use a FOR loop to count to the max. This way you can just use the FOR variable for your counter.

Code: Select all

@ECHO off
FOR /L %%G IN (1 1 2147483647) DO (
	ECHO Number: %%G
)
If you are creating more than a million files, let alone 2 billion files I would think you are doing something nefarious.

Re: For while loop or just a goto statement

Posted: 05 Oct 2018 13:59
by aGerman
You're right Squashman although I'm afraid the next question will be related to delayed expansion :lol:

Re: For while loop or just a goto statement

Posted: 09 Oct 2018 07:13
by cvau89
I think I understand - now what's the syntax for renaming the files using the variable counter?

i.e.

ren file.txt file+%%n+.txt ? (lol I'm laughing because I hope that was not incredibly wrong!)

Re: For while loop or just a goto statement

Posted: 09 Oct 2018 08:58
by kwsiebert
No need for the +s here, you want:

ren file.txt file%%n.txt