Hi cardscomp.
Qlemo has given you a working example that answers your question.
I sense that perhaps you aren't fully conversant with how the FOR command can be used to split up lines into chunks and allow you to keep one or other of those chunks for whatever purpose you want, so I would like to just add in a little explanation if it helps you to employ the same theory for other uses. Please just treat this as just additional free advice.
What you have to do is look carefully at your text string and decide what is best to use as the field-separator (delimeter) that will break the lines into as many separate pieces as you need to get the job done. Your example is pretty easy. There is one dot right at the end of the piece of text you want to capture, so you use that as the delimeter. That would split it into two parts and you would use the first one.
With the FOR command, whatever is in the brackets will be done first. In Qlemo's batch file it just specifies the source of the text string as the input. The part of the command outside and to the left of the brackets defines how the source is to be treated, and the part of the command after the brackets dictates what happens to the processed input after the FOR command has worked with it. The bit inside the brackets could be a command if surrounded by single quotes, for example a DIR command to capture a file/folder listing, but it still forms the source that the FOR command is told to then process. Having a text file makes it easy to see what is being input to the FOR command, whereas if the source text was the result of a command being run, you would really have to be familiar in advance with the layout of the source text.
The tokens=" part can be confusing. Think of the expression as "what chunks to keep" as the FOR command walks through the line looking at each part that is separated by whatever you specify as your chunk separator with the "delims=" part. In your case, Qlemo has used "tokens=1", because you just want that first chunk before it gets to the . delimeter.
If you wanted to only keep the 2nd chunk, ie. "value2", then the easiest way to visualise the FOR command getting it is like this in a batch file:
for /f "tokens=1,2 delims=." %%a in (textfile.txt) do @echo %%b
I used "a" rather than "L" as the starting variable just for clarity in case it threw you off the scent. The starting variable letter specified after the %% in a batch file or % straight from the command line (as explained by Qlemo) can be an uppercase or lowercase letter, and it can be almost any letter. If you have an uppercase letter to the left of the bracketed part of the command, then you must use an uppercase one to the right of the brackets also.
It's like declaring the starting point. In the example above the FOR command will start walking through the line until it gets to the dot delimeter. Whatever string it has read in up until that point will be held in the variable %%a. It continues looking for another dot, but doesn't find another on that line, so the part after the dot is stored in variable %%b. It would carry on like that through a line grabbing the chunks (tokens) and filing them as %%c, %%d, etc, etc. as long as your "tokens" covered that number of chunks. Translating this to Qlemo's chosen starting variable of %%L, the 2nd token would be %%M, the third (if there was one) would be %%N, etc, etc.
You can specify tokens like this:
"tokens=*" - capture all chunks found
"tokens=1,2,3*" - captures chunks 1, 2, 3, and all others thereafter
"tokens=1-8" - captures chunks 1,2,3,4,5,6,7,8
"tokens=1,3,5" - only captures chunks 1,3, and 5
You can specify more than one delimeter including a space like this:
"delims=., "
which would break the line into chunks when it encountered a dot, comma, or space, but doing this kind of thing you have to really look at the line you are parsing and start counting through the number of chunks that those 3 delimeters would break it into, and then adjust your "tokens" declaration accordingly.
The part at the right of the brackets is simple. You are just recalling whatever chunk has been stored in the variable and using it however you wish. In Qlemo's example it is just being echoed back. You could use it as part of another command, set another variable to now equal your %%a, %%b, %%L, or whatever, or subject the chunk of data stored in the variable to further processing.
You just have to remember that for EACH LINE that is found in your text file (or the source data), the FOR command walks through each separately and the %%a or %%L will hold a different chunk of data for each pass. The FOR command is a loop, so recalling whatever is held in your variable is live and only applicable to whatever line is currently being read at the time. There are ways that you can preserve whatever data was held in a variable from an earlier pass, but that is beyond the scope of my simplified explanation.
Back to Qlemo's command. That is what you need to get the job done as asked, so that's your answer.
Bill
Main Topics
Browse All Topics





by: QlemoPosted on 2009-11-02 at 09:15:31ID: 25721297
for /f "tokens=1 delims=." %L in (textfile.txt) do @echo %L
In a batch file, you have to use %%L (both occurances).