More For Questions :P
Moderator: DosItHelp
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
More For Questions :P
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
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
Re: More For Questions :P
No, it shouldn't.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"?
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.
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)
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
Re: More For Questions :P
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"
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: More For Questions :P
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..?
And about my last question..?
Re: More For Questions :P
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.
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.
Re: More For Questions :P
@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.
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.
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
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
Re: More For Questions :P
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, sois the same asor
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:
Antonio
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)
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)
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
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: More For Questions :P
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
Thanks
Re: More For Questions :P
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 /?
Re: More For Questions :P
Dunno about 2000 or millenium but in XP, it works.foxidrive wrote:Sometimes TAB doesn't work (like in Windows NT for example, from memory)
Re: More For Questions :P
Ed Dyreen wrote:Dunno about 2000 or millenium but in XP, it works.foxidrive wrote:Sometimes TAB doesn't work (like in Windows NT for example, from memory)
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.
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: More For Questions :P
Hey guys... more questions
Didn't want to start a new thread cuz it might be considered spamming 
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


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
-
- Posts: 76
- Joined: 21 Dec 2011 14:21
Re: More For Questions :P
john924xps wrote:Hey guys... more questionsDidn't want to start a new thread cuz it might be considered spamming
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?
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: More For Questions :P
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
Re: More For Questions :P
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