| 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 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. |
|