1.

Solve : Batch works, but token arguement looks wrong??

Answer»

So I put together this batch with some help from google, and I changed it to GET the output I wanted which was the 3 letter Day instead of 10/25/2011. I REPLACED the "tokens=*" with "tokens=3" and end up with what I want, BUT I dont think I am using this token arguement correctly as for even placing values of 1 and 8 in 3's place I still get the same result. I am guessing that I am misinterpreting the token arguement even though I am getting the result I want. I assumed that the token arguement was to specify how many character places on the line or range to read in on the line. So when i put in 1, I expected to see just a T for Tue and when using 8 I expected to see Tue 10/2. I FEEL I am way off on my understanding of the token arguement and someone here will point out where I am wrong so I can learn the correct method and what is happening with the token= statement here to allow for the result I want, when just about any number gives same results. I could have just run with this batch since it works, but I dont like running a BOTCHED batch. I'd rather have it batched correctly and learn from my mistakes even though I am getting the results I want. This is probably the very first post here asking for help for a batch that works, but is probably way wrong..lol 


Code: [Select]ECHO OFF
FOR /F "tokens=3" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
ECHO %Today% >Day.txtYour tokens= actually isn't setting anything to do with the tokens, but rather how many there are. Your delims= would actually be what seperates the tokens. Since it isn't specified, delims= assumes any space to be a delimiter, which is why it doesn't matter in your example what number you put in tokens=. In your example, if you were to say tokens=1, then you would only be designating that there is one token, so the first delimiter encountered would determine the token and the rest of the line would be dismissed. Doing something like tokens=1* says that the first delimiter encountered determines the first token and the rest of the line is assigned to the second token, regardless of delimiters.

So using your date /t example (which today outputs "Tue 10/25/2011"), using "tokens=1" designates %A as the three letter day (because there is a space after the day) and nothing else is assigned. Using "tokens=1*" designates %A as the three letter day, again, but assigns the rest of the line to %B. You can manipulate this a little by assigning different delimiters. For example, "tokens=1 delims=/" would assign everything up to the first "/" as %A token. Now, if you were to use "tokens=1* delims=/" then "Tue 10" would be assigned to %A, and "25/2011" would be assigned to %B (notice that even though a delimiter is in the string, because we designated the "*" it takes everything else and assigns it to the second token.) In this example, "tokens=2 delims=/" would assign "Tue 10" to %A and assign "25" to %B and dismiss everything else. You can use these two parameters to parse out lines fairly intricately.

The other parameter that can be set is the "eol" parameter, which allows you to designate an end of line parameter in case you are wanting to ignore everything after a certain character(s). Using "eol=/ tokens=1*" would assign “Tue” to %A and “10” to %B. Everything after the “/” would be dismissed.

So hopefully that explains what you are looking for. If you are still confused, please say what you are confused on and I will try to explain it better. Quote

Your tokens= actually isn't setting anything to do with the tokens, but rather how many there are

Not quite. tokens=3 does not define 3 tokens, it defines 1 token. The tokens clause defines which values in the parsed string are addressable.  The parsing (based on the delims clause) is done ahead of the tokens/value assignments.

Code: [Select]for /f  %i in ("one two three four five six seven") do echo %i

The default for tokens is 1 and delims is space or tab. The above snippet will assign %i to a value of one

Code: [Select]for /f  "tokens=3,5,7" %i in ("one two three four five six seven") do echo %i %j %k

Now the fun begins. Only the third, fifth and seventh values in the parsed string are defined as addressable by the tokens clause. The %i token is explicitly defined and subsequent tokens will be assigned sequentially. The delimiter is still a space, so the tokens values shake out as %i is three, %j is five and %k is seven.

You can also use ranges in the tokens clause and even mixed them will single positional values:

Code: [Select]for /f  "tokens=2-4,6" %i in ("one two three four five six seven") do echo %i %j %k %l

The above snippet will assign %i to two, %j to three, %k to four and %l to six

There are other variations. The for statement can muck up the readability of a batch file, but is also a workhorse among the few batch instructions available.

 

The snippets above are design for command prompt execution. Double up the percent symbols for use in batch FILES.


Discussion

No Comment Found