(Jan 29, 2016 04:48 AM)bunny-Giles Wrote: Hi,
I am a beginner with batch scripting and would be grateful for some advice...
Welcome! ::purr::
You have found a good place here. Lots of friendly people/cats to play and learn with. :>
Quote:Is it possible to use a for loop within a for loop in a batch script?
Yes it is! Although, there are a couple of different methods...
Quote: - I need to loop through 1000 files and for each file repeat a 3 second cycle up to 300 times
Are all of the files in the same folder?
If that's the case, you can get the list of 1000 files using
dir /b /a:-d
(Dirbad is actually the name of a pirate, true story..)
The
/b bit means "use basic formatting"
The
/a:-d part is for selecting "attributes" of the files we want, in this case, we want only files that are NOT directories (hence "-d" instead of "d")
Try typing
dir /b /a:-d at a command prompt so you can see what this command does. :>
Then you need to loop over the files...
So I would suggest to use a
for /f loop to iterate over each entry in the
dir /b /a:-d output.
Since we don't want the loop to split the line up into tokens, we should also specify that there are no delimiters.
Code
for /f "delims=" %%a in ('dir /b /a:-d') do (
echo Oki now I am processing %%a
)
Are you following along so far? :> This is the outer part of the loop. We can cover the inner part too, but let you know if you have any questions about this so far.. :>
Quote:To that end, how do I break out of the second for loop early?
Spoiler alert... we use a GOTO statement. :P
Quote:Also, can you explain how delayed expansion works - i've seen it in scripts, but don't get what it does...would I need it here?
You
might need it here, depending on how you choose to do your loops.
Okay... here goes my Delayed Expansion explanation.
In batch, normally we refer to variables like
%variable%
If we enable Delayed Expansion, we can also refer to that same thing as
!variable!
The exclamation marks are a different set of symbols, which also denote a variable.
Example:
Code
@echo off
setlocal EnableDelayedExpansion
set "var=Cats are best"
echo %var%
echo !var!
pause
Code
Cats are best
Cats are best
Press any key to continue . . .
Okay, so hopefully this gets you comfortable with the idea that !variable! == %variable%
Now, the question is, what does this let you do?
Well, the difference comes with the way the variables are evaluated.
The crux is that
percentages are evaluated when the interpreter reaches that line
whereas
exclamations are evaluated when the interpreter reaches the variable
So for example, when we run a line of code like this:
Code
echo %var%
echo !var!
First batch evaluates any percentage-variables to their actual values, and the line becomes this:
Code
echo Cats are best
echo !var!
So that is why it's called "Delayed Expansion"
Because exclamation variables are not evaluated when the line is reached.. their evaluation is "delayed" until the last possible moment. :>
Delayed Expansion is very useful for creating array-like behaviour, or for iterating over characters in a string, and for a number of other advanced things.
Quote:Any help will be gratefully received.
Thanks
And likewise, we will fall all over ourselves to help you out, when you show the slightest interest in learning.. :> ::purr::