More For Questions :P

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

More For Questions :P

#1 Post by john924xps » 28 Oct 2012 07:33

In the example
FOR %%G IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (md C:\demo\%%G)
Doesn't the first character "a" become %%G, then "b" becomes "%%H" and so on? So why does that example work? shouldn't it only create a folder "a"?
And is there an easier way of creating files with letters as their names besides pre-stating the names of the folders inside the brackets? That's just gonna take a long time to code.

So if I've got it right…
FOR /F "tokens=1-5"
will set the first to fifth pieces of data into metavariables, whilst
FOR /F "tokens=1,5"
would only set the first AND fifth into tokens? same goes with "1,*" and "1-*"?

Thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: More For Questions :P

#2 Post by Ed Dyreen » 28 Oct 2012 07:44

john924xps wrote:In the example
FOR %%G IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (md C:\demo\%%G)
Doesn't the first character "a" become %%G, then "b" becomes "%%H" and so on? So why does that example work? shouldn't it only create a folder "a"?
No, it shouldn't.

Code: Select all

>for /?
Run a specified command for each file in a set of files.

FOR %variable IN (set) DO command [commandparameters]
  (set) Specifies a set of one or more files.
Thus

Code: Select all

FOR %%G IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (md C:\demo\%%G)
equals

Code: Select all

cd /d C:\demo
md a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

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

Re: More For Questions :P

#3 Post by abc0502 » 28 Oct 2012 07:51

Also, the a become %%G and b become %%H Only work when you are useing tokens and the text between the ( and ) is a text or a file, but as aGerman said in your case the %%G will iterate through all letters "a to z will become %%G"

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

Re: More For Questions :P

#4 Post by john924xps » 28 Oct 2012 08:00

So the computer will add more metavariables only and only if tokens are used? And what would happen if you used delims but didn't use tokens??
And about my last question..?

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

Re: More For Questions :P

#5 Post by abc0502 » 28 Oct 2012 08:16

no no no, the tokens is used to select a piece of a text with the help of the delims, and the sequence from a to z isn't a text because the text in a for/loop is between double quotes.

Edit:
In your case i think you want to set a variable to each letter so you must then convert this sequence to a text by adding it between double quotes and using tokens and a comma as a delims. So the a become %%G and b become %%H and so is the rest of the letters.

this : FOR /F "tokens=1-5" I wasn't able to make it work for me before but the For command document says what you wrote.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: More For Questions :P

#6 Post by dbenham » 28 Oct 2012 08:56

@john924xps - You are confusing a simple FOR with FOR /F. The syntax is nearly the same, but the behavior is completely different and they serve different purposes.

The simple FOR will iterate the values between the parentheses. If a value contains a wildcard (* or ?) then it will assume it represents a file and it will effectively list the files that match the pattern. But that is not an issue with your code.

Code: Select all

for %%A in (a,b) do echo %%A

The FOR /F is extremely complicated - it can parse lines within files, parse a string of text, or parse the output of a command. The action taken depends on the contents of IN(). If no quotes then it will parse each line found within each file specified within the parentheses. In this case it will parse the lines found in the file named A and file named B.

Code: Select all

for /f %%A in (a,b) do echo %%A

It is all perfectly logical, but it can be extremely confusing until you get used to the syntax. You should carefully read the HELP documentation: available by typing HELP FOR or FOR /? within a command window.

Dave Benham

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: More For Questions :P

#7 Post by Aacini » 28 Oct 2012 09:27

You must realize that FOR %%G and FOR /F %%G are two different comands.

In the first one the set of values may be separated with space, comma, semicolon or equal sign, so

Code: Select all

FOR %%G IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (md C:\demo\%%G)
is the same as

Code: Select all

FOR %%G IN (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO (md C:\demo\%%G)
or

Code: Select all

FOR %%G IN (a b,c;d=e,f g=h;i,j k l,m,n,o=p=q=r;s;t;u ,;=v=;, w,,,x;;;y===z) DO (md C:\demo\%%G)

In the case of FOR /F command, only when "tokens" option indicate more than one token (in combination with standard or any given delimiter), then more than one metavariable is created.

There is no direct way to generate a list of letters with a FOR command. You may generate a list of ASCII codes this way: FOR /L %%G IN (97,1,122) DO and then convert the numbers to ASCII characters, or create first a string and then extract individual letters this way:

Code: Select all

setlocal EnableDelayedExpansion
set letters=abcdefghijklmnopqrstuvwxyz
for /L %%i in (0,1,25) do md C:\demo\!letters:~%%i,1!


Antonio

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

Re: More For Questions :P

#8 Post by john924xps » 29 Oct 2012 06:28

So just to clarify, the for command (no /F) will not allow multiple metavariables, but most of the others would allow them if tokens and delimeters are specified? Like, for each loop the metavariable for a for command with no /F would just keep resetting?

Thanks

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

Re: More For Questions :P

#9 Post by foxidrive » 29 Oct 2012 07:08

john924xps wrote:So just to clarify, the for command (no /F) will not allow multiple metavariables, but most of the others would allow them if tokens and delimeters are specified?


Only when tokens are specified, with or without special delimiters. The default delimiters are TAB and space. Sometimes TAB doesn't work (like in Windows NT for example, from memory)

There are FOR modifiers that can be used though - see the last page of the help for FOR /?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: More For Questions :P

#10 Post by Ed Dyreen » 29 Oct 2012 07:25

foxidrive wrote:Sometimes TAB doesn't work (like in Windows NT for example, from memory)
Dunno about 2000 or millenium but in XP, it works.

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

Re: More For Questions :P

#11 Post by foxidrive » 29 Oct 2012 08:36

Ed Dyreen wrote:
foxidrive wrote:Sometimes TAB doesn't work (like in Windows NT for example, from memory)
Dunno about 2000 or millenium but in XP, it works.


Yeah, I was referring to the Windows before 2000 - the first Windows NT of the NT line. Dunno why I mentioned it really - but it's like when I write goto :EOF as the EOF had to be in upper case for Windows NT and I still use uppercase for it. Similarly the LSS/EQU/GTR comparison etc had to be upper case.

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

Re: More For Questions :P

#12 Post by john924xps » 30 Oct 2012 08:43

Hey guys... more questions :P Didn't want to start a new thread cuz it might be considered spamming :P
First question: How do you make a for /L loop count numbers in a loop? I used IN(1,1,*), but that didn't work. And I don't want to use a goto loop since that's just a waste of memory and time.

Second: Can someone please explain the special characters for a for command? For example, single quotation marks are used to specify files.. or at least provide a source where I can find these?

Third: What if I wanted to copy a file in the DO() section of a for loop, but the file I wanted to copy had spaces in it? I can't use double quotation marks, since that makes it considered as a string. Or does it..?

Fourth: What happens if in tokens you put an asterix right next to a digit? (tokens=1*)

Thanks... you guys are great help :3

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: More For Questions :P

#13 Post by timbertuck » 30 Oct 2012 09:10

john924xps wrote:Hey guys... more questions :P Didn't want to start a new thread cuz it might be considered spamming :P
First question: How do you make a for /L loop count numbers in a loop? I used IN(1,1,*), but that didn't work. And I don't want to use a goto loop since that's just a waste of memory and time.
Second: Can someone please explain the special characters for a for command? For example, single quotation marks are used to specify files.. or at least provide a source where I can find these?
Third: What if I wanted to copy a file in the DO() section of a for loop, but the file I wanted to copy had spaces in it? I can't use double quotation marks, since that makes it considered as a string. Or does it..?
Fourth: What happens if in tokens you put an asterix right next to a digit? (tokens=1*)
Thanks... you guys are great help :3


1) for /L %%a in (1,1,10) do (echo count %%a)
2) run FOR /? but it depends on how you use the double quotes "
3) for /F "usebackq" %%a in ("file with space.txt") do echo %%a
4) the asterisk is used for leftover, so in your case, 1* means TWO tokens, the first and whatever else is left over. since there are no delims, then SPACE and TAB are used by default.

that help?

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

Re: More For Questions :P

#14 Post by john924xps » 30 Oct 2012 18:25

For the 1st question, I asked for creating a forever loop with the for /L. Not count to 10, sorry bout that. And for the second question, I meant ALL the special characters... " ' dat stuf

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

Re: More For Questions :P

#15 Post by foxidrive » 30 Oct 2012 20:11

john924xps wrote:For the 1st question, I asked for creating a forever loop with the for /L. Not count to 10, sorry bout that. And for the second question, I meant ALL the special characters... " ' dat stuf


A forever loop - not with FOR /L
The largest you can go is 2^31 - 1

Here's a forever loop.

Code: Select all

:loop
echo loop
goto :loop


Read FOR /?
as there is a lot of useful information there

Post Reply