Help with loop increment & variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
terus
Posts: 14
Joined: 26 Apr 2013 22:40

Help with loop increment & variables

#1 Post by terus » 27 Apr 2013 00:35

EDIT - Removed example/pseudo code. Figured out how to articulate the root of my problem(s).

What I am trying to do is use a loop. I don't quite understand how to loop anything at all. I also don't understand or know how to incrementally change from one sequential file and/or variable to the next. That is my main problem.
Looking at a directory/folder for example what I mean when I say sequential is the files & their order.

I am able to execute everything without incident. When I do so without looping. The reason I want the loop is so that one segment of code can be used. Instead of having to copy/paste/alter everything an indefinite amount of times.

So my questions are:
Can someone please explain how to loop a segment of code? If not direct me somewhere that explains it simply. My searches haven't turned up anything I could understand. Simply seeing code to loop "echo Hello World" a set number of times would suffice.

How does one go from one sequential variable to the next & how is it defined as first, last, etc.. is it possible?
Example if %variable1% is set in %primaryVariable% how would you automatically set it to %variable2%, %variable3%, etc.. & then stop incrementing at the appropriate time?

I was also wondering if there was a way to read through a directory alphanumerically focusing only on a specific file extension. Even without extension specificity if need be. Then set the individual file names as a variable.

Sorry I've not been clear enough. I tried to explain as best I could with pseudo and example code as I wasn't even entirely sure what I needed help with. Hopefully this clears things up. Any & all help is greatly appreciated. I understand you're taking time out of your day to pay my little problem attention. And I thank you all for that.

Also thank you abc0502 for the info on the quotes. I've been doing that wrong for far too long now.
Last edited by terus on 27 Apr 2013 23:45, edited 6 times in total.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Help with loop increment & variables

#2 Post by Endoro » 27 Apr 2013 03:54

I don't understand your pseudo code: "%variableB% Installed" but never set? :?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help with loop increment & variables

#3 Post by abc0502 » 27 Apr 2013 04:10

if you can explain in details, that will make us help you faster, instead of keeping ask you for more information in each post.

note:
Set varA00="c:\folder\patch1.ext"
the red quote should be like this
Set "varA00=c:\folder\patch1.ext"
make it around the whole variable

terus
Posts: 14
Joined: 26 Apr 2013 22:40

Re: Help with loop increment & variables

#4 Post by terus » 27 Apr 2013 13:38

Actually... With a clear and fresh head. The more I look at this, the more it seems I'm over complicating things. Would it be easier, & possibly more sensible, to read from separate path & description files? That way I would just have to ensure the line count was consistent between path & description. However I don't quite know how to go about that either.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with loop increment & variables

#5 Post by foxidrive » 27 Apr 2013 18:56

Explain what it is you want to do in English, and let the contributors work out the actual best way to implement it.

It seems that you want to add description files - FILE_ID.DIZ - or something to archives.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help with loop increment & variables

#6 Post by abc0502 » 28 Apr 2013 03:09

how to loop a segment of code?
You can loop a block of code using different methods, but which one to use will finally depend on the use of your block of code.

1) using labels and goto Command
This way you can loop for infinite time unless your block of code has a condition that exit the loop
@echo off
:LabelName
echo Hello World
goto :LabelName
This will keep printing the word Hello World until you exit the batch.
The Red word is a lable, starts with a single colon and followed by any name you set.

2) using a For Command with /L switch
This will loop for a certain times, you set inside the for command.
@Echo Off
For /L %%A In (1 1 5) Do echo Hello World
pause
This will print the Hello World five times then will pause.
The blue number "1" is the starting number, that till the for command where to start counting.
The green number "1" is the amount the for command will raise the blue number after each executing of the echo command.
The red number "5" is the number the the for command will reach then exit the command, and go to the next command "pause".

3) using functions to repeat a block of code
This can be used if you have a block of code that is used for example to process a file, and you will process 10 files so you can use a function and pass the file to it.
@echo off
Call :FunctionName "Test0" "Test1"
Call :FunctionName "Test2" "Test3"
pause
exit
:FunctionName
echo First Word is: %~1, and The Second is: %~2
Goto :EOF
This function will print any word you pass to it.
Function name in red is a label name, but the functions exist after the "exit" command, and always end with "Goto :EOF" command.
The %~1, and %~2 is used as a variable for the passed text to the functions, %~1 takes the first passed text and the %~2 is for the second and so will be the %~3 till %~9.
You use "CALL" caommand to use a function, not "Goto", Call will keep track of what it do, it will go to the function and run it then when it will reach "Goto :EOF" it will then go back where it was and run the next command, but "goto" command will just move to the function, run it, and when reach the "Goto :EOF" will just end the batch, and won't run the next command.

I Think these are the looping methods that can be used to repeat a block of code to use it more than once.

about
How does one go from one sequential variable to the next & how is it defined as first, last, etc.. is it possible?
Example if %variable1% is set in %primaryVariable% how would you automatically set it to %variable2%, %variable3%, etc.. & then stop incrementing at the appropriate time?
I don't exactly understand what do you mean, sorry'


reading through a directory alphanumerically, focusing only on a specific file extension, set the individual file names as a variable

Using a "DIR" command with a /O:N switch will list any files in a directory in order by name.
using it with a combination of /O:N switch and /B will list the names only without the size and the other information, but will also list the folders.
using /O:N and /B and /A:-D will not list folder, "/A:-D" will list all but folders.

Now combine DIR command with For command with /F switch.
@Echo OFF
For /F "delims=" %%A In ( ' DIR /B /O:N /A:-D "*.txt" ' ) Do SET "File=%%A"
pause
This will do all what you need, list files sorted by name, and will list only a ".txt" files, and set it's name in a variable, as %%A will be temp variable that will hold the file name till you set it to a variable "File".
"delims=" is used in case the file has space in it, so it will get it's name right.
Red commas is important, you always use them when you use a command with a for command.

The for command here list file by file and set it to the variable "File", then get the next file name and set it again to the variable "File" and so on till it reach the last file ( the File variable will hold the last file name only in this code )
and if you want to use the file name to pass it to a code, the for command must be like this:
@Echo OFF
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In ( ' DIR /B /O:N /A:-D "C:\*.txt" ' ) Do (
SET "File=%%A"
echo !File!
)
pause
but when you use a variable the will keep changing and hold different values, you use the command "SETLOCAL EnableDelayedExpansion" and the variables instead of writing them like "%File%" will be written like this "!File!".

so The code here will do this:
list all ".txt" file in the C:\ directory in ordered by it's name, and for each one it find, it will set it's name in the variable "File" then print that name on screen, and go back again and list the next file, and will keep doing that till it list all the .txt files in that directory.

I hope that is clear enough :)

terus
Posts: 14
Joined: 26 Apr 2013 22:40

Re: Help with loop increment & variables

#7 Post by terus » 28 Apr 2013 04:31

Thank you a thousand times over! Problems solved.
As for the sequential stuff I was wanting. It's no longer important. The last example you gave for reading a directory is a far better alternative.

Funny thing about not being able to loop. I tried the following method several times before asking.

Code: Select all

@echo off
:LabelName
echo Hello World
goto :LabelName


I can only ever seem get "goto" to move forward & never backward. Which I just do not understand.
I mean I use "goto" all the time for confirmation dialogs such as yes/no prompt. Which works very well moving forward.
Anyway the alternative loop methods you posted work just fine. So I'm not too concerned about it, just curious at this point.

Thanks again for all your help!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help with loop increment & variables

#8 Post by abc0502 » 28 Apr 2013 04:40

moving forward or backward is depending on where your label is.
when using goto to check answers like yes/no, you use it to move on in the code.

the way the example work is just for the first time the code move like any batch executing command after the other but it reach to the goto command which point it to go to a label, which happen to be at the start, so it go there and repeate the commands and it keep repeating it self more and more till you end the batch.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with loop increment & variables

#9 Post by foxidrive » 28 Apr 2013 04:42

terus wrote:

Code: Select all

@echo off
:LabelName
echo Hello World
goto :LabelName


I can only ever seem get "goto" to move forward & never backward. Which I just do not understand.
I mean I use "goto" all the time for confirmation dialogs such as yes/no prompt. Which works very well moving forward.


Try the example above - it works.

terus
Posts: 14
Joined: 26 Apr 2013 22:40

Re: Help with loop increment & variables

#10 Post by terus » 28 Apr 2013 15:08

The probem I was having with "goto" moving back makes me feel like a fool. It was a user error involving the space character.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with loop increment & variables

#11 Post by foxidrive » 28 Apr 2013 22:14

The positive thing is that you now understand why, and realise it works. :thumbsup:

terus
Posts: 14
Joined: 26 Apr 2013 22:40

Re: Help with loop increment & variables

#12 Post by terus » 29 Apr 2013 03:28

Everything is done & works flawlessly. Thanks again for the help. I learned a lot along the way. Biggest thing being to stop using notepad & move on to notepad++. It's striking how much better everything is when you can see what you're doing. Such as an open parenthesis or a misplaced space.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Help with loop increment & variables

#13 Post by trebor68 » 29 Apr 2013 06:16

Here are two different codes.

Code 1 - Only see "Hallo World" again when you input "Y".

Code: Select all

@echo off
setlocal EnableExtensions
:LabelName
echo Hallo World
set /p var1=See this again? [Y/N]
if /i "%var1%" equ "Y" goto :LabelName


Code 2 - You see as long "Hallo World" as is "N" entered to.

Code: Select all

@echo off
setlocal EnableExtensions
:LabelName
echo Hallo World
set /p var1=Exit this loop? [Y/N]
if /i "%var1%" neq "Y" goto :LabelName

Post Reply