For while loop or just a goto statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cvau89
Posts: 2
Joined: 05 Oct 2018 09:49

For while loop or just a goto statement

#1 Post by cvau89 » 05 Oct 2018 10:03

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: For while loop or just a goto statement

#2 Post by aGerman » 05 Oct 2018 13:24

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: For while loop or just a goto statement

#3 Post by Squashman » 05 Oct 2018 13:45

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: For while loop or just a goto statement

#4 Post by aGerman » 05 Oct 2018 13:59

You're right Squashman although I'm afraid the next question will be related to delayed expansion :lol:

cvau89
Posts: 2
Joined: 05 Oct 2018 09:49

Re: For while loop or just a goto statement

#5 Post by cvau89 » 09 Oct 2018 07:13

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

kwsiebert
Posts: 42
Joined: 20 Jan 2016 15:46

Re: For while loop or just a goto statement

#6 Post by kwsiebert » 09 Oct 2018 08:58

No need for the +s here, you want:

ren file.txt file%%n.txt

Post Reply