For Questions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

For Questions

#1 Post by john924xps » 25 Oct 2012 22:24

What is the purpose of the for command? I mean... what does the for test? This is the layout for a FOR command:
FOR /F ["Options"] %%Variable IN ('Command') DO Command [Parameters]

But what does it test? What is the %%Variable for? And how do I obtain and use multiple delimiters? THANKS!

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

Re: For Questions

#2 Post by foxidrive » 25 Oct 2012 22:59

Run this in a folder and see what it does.

Code: Select all

@echo off
for %%a in (*.*) do echo %%a
pause



Your question is poor - someone would have to teach you every aspect of the for-in-do BY TYPING.

It's better for you to show an example and ask your question relating to it.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: For Questions

#3 Post by john924xps » 26 Oct 2012 00:16

Does this help?
FOR /F "tokens=1-5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
What does the %%A represent? Since I see the use of 5 of those parameters here...

And just to confirm, the for loop is used to keep looping in search of the correct variable or file(s)? Thanks again

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

Re: For Questions

#4 Post by foxidrive » 26 Oct 2012 00:42

john924xps wrote:Does this help?
FOR /F "tokens=1-5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
What does the %%A represent? Since I see the use of 5 of those parameters here...


%%A is a metavariable. When used it contains part of the input line, or all of it, depending on the tokens and delims that are used.

If you have "tokens=1,3" then you will get the first word in %%A and the third word in %%B.


And just to confirm, the for loop is used to keep looping in search of the correct variable or file(s)? Thanks again


It parses multiple lines of text, or a single line of text. Each time the DO command part is executed it is operating on a different line - until it runs out of lines.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: For Questions

#5 Post by john924xps » 26 Oct 2012 02:14

So what you're saying is that I can specify ANY character for the metavariable?
FOR /F "tokens=1-5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
can also be considered as
FOR /F "tokens=1-5 delims=, " %%B IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E ?

And what if I have:
FOR /F "tokens=2,5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
what would happen then?

I'm completely confused by the layout of the command...

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

Re: For Questions

#6 Post by foxidrive » 26 Oct 2012 02:38

john924xps wrote:So what you're saying is that I can specify ANY character for the metavariable?


You can start with any ascii printable character, including wildcards as some foolish people do, without realising how new people to batch can become confused.

The sequence of metavariables is determined by their ASCII value. %%A is not the same as %%a, also. Case is significant.


FOR /F "tokens=1-5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
can also be considered as
FOR /F "tokens=1-5 delims=, " %%B IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E ?


No. Try it and see what it prints to the console.

If you wanted to start at %%B then you would need to adjust the metavariables.
FOR /F "tokens=1-5 delims=, " %%B IN ("This text will be shown. ") DO echo %%B %%C %%D %%E %%F


And what if I have:
FOR /F "tokens=2,5 delims=, " %%A IN ("This text will be shown. ") DO echo %%A %%B %%C %%D %%E
what would happen then?


It will print out 'text shown.' Always try the code and it can become clearer. It also adds %C %D %E because those metavariables haven't been defined in tokens.

In your examples the delims=, " is doing nothing, A) because there are no commas in the input text and B) because space is a default delimiter. Try removing that portion and come to grips with the tokens part first.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: For Questions

#7 Post by john924xps » 26 Oct 2012 04:43

Thank you. I know have a brief understanding of how the for command works... Ima go try it on some files... :D

Post Reply