| 1. |
Solve : How do delimit an environment variable? |
|
Answer» search around for this but couldn't find anything that would work. I wouldn't make a habit of using numbers for the FOR loop variable especially if you have batch files that use cmd line input or you are calling another label or batch file. I've been using numerals (and most PRINTABLE ASCII special characters in the range of decimal 33 thru' 254) as For loop variables in batch scripting for some time and never ever had a problem which can be associated with their use which I realize is undocumented. Can you provide an example of a numeric For loop variable which causes a problem in a batch script please, typos excluded? Thanks. Code: [Select]for /f "delims=" %%1 in ("%testfile%") do set filenam=%%~n1 Salmon I tried the following from the DOS prompt.. (after setting testfile=sp.cmd) for /f "delims=" %a in ("%testfile%") do echo %a~n1 which returned: sp.cmd~n1 so i tried this: for /f "delims=." %a in ("%testfile%") do echo %a~n1 which returned: sp~n1 so i tried this: for /f "delims=." %a in ("%testfile%") do echo %a:~n1 which returned: sp:~n1 what am i doing wrong? Quote from: skorpio07 on December 13, 2011, 07:44:23 AM what am i doing wrong?Your syntax is off, and it SEEMS you are misinterpreting how the for statement is working. For a quick explanation, your %a is the token that you are setting so the ~n1 is not actually doing anything. If you are looking to grab the name of a file (which is what I am assuming you are wanting using the ~n parsing syntax) then that needs to be set before the variable. Example: for variable %a, to grab the name you would need to use %~na. This is why the help text suggests to use capital letters, as it helps to distinguish parsing syntax from the variable. I.e. %~nA for a %A variable. For what you are trying to accomplish, using the for "delims=." %a in ("%testfile%") do echo %a should get you what you are looking for. The only issue you would end up running into is there is a "." in the file name. If you are looking to grab the name of the file however, you can also try using for /f "delims=" %a in ("%testfile%") do echo %~na and you should end up with identical results, only this one should ALLOW for additional "."s to be in the filename. Remember that when using these in a batch file, you need to double up the percent signs. %a=%%a and so forth. Squashman, T.C.'s method of using ASCII 33-254 will work in most applications. What I have found the most annoying part about all of it is trying to figure out which ASCII character comes next, which is why I continue to use the letter variables. I still remember my alphabet from school most days. thanks raven i should have posted that i had already gotten the right solution (there is obviously more than one) with this: for /f "tokens=1 delims=." %%a in ("%testfile%") do (set temptst=%%a) --running inside a batch file-- but nice to know that this works as well: for /f "delims=." %%a in ("%testfile%") do (set temptst=%%a) and for /f "delims=" %%a in ("%testfile%") do (set tempst=%%~na) and im sure there are other ways as well, my response to salmon was that i was trying addtional ways of getting the filename so i tried his method to see if it would work (my syntax was off, i got it now!) and thanks again to TC, i now know better how to extract tokens...and on that note, one question with regards to tokens: what is the difference between: tokens=1-5 and tokens=0,2 (the question is what is the difference between the dash and the comma) where would i find the microsoft answer to that as well. thanks again Quote from: skorpio07 on December 13, 2011, 10:30:59 AM what is the difference between: You can't have a token 0 (zero), they start at 1 (the first). The difference is that "tokens 1-5" selects tokens 1 to 5 and "tokens=1,5" selects tokens 1 and 5. I can think of at least 4 ways to get familar with how they work: (1) Study the FOR documentation which you can see by opening a command window and typing FOR /? at the prompt. Most commands have help you can see by this method. (2) Study the Microsoft Command-Line Reference A=Z at http://technet.microsoft.com/en-us/library/bb490890.aspx (3) Just Google for stuff about batch command syntax, there are zillions of pages out there. (4) Most fun, I think, and a good idea in many ways, do some experiments at the command prompt. You won't break your computer as long as you don't FOOL around with DEL where you have precious files. Remember that you use one percent sign at the prompt where you would use two in a batch script, so (for example) you would use %A %B etc at the prompt and %%A %%B etc in a batch file. You may as well precede the echo command with an symbol to avoid seeing it on the screen. Code: [Select] C:\>for /f "tokens=1-5" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do echo %A %B ant bee C:\>for /f "tokens=1,5" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do echo %A %B ant egg C:\>for /f "tokens=1,2,6-7" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do echo %A %B %C %D ant bee fire goes C:\>for /f "tokens=1,2,6,8-9" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do echo %A %B %C %D %E ant bee fire hat ink C:\>for /f "tokens=1,9 delims=," %A in ("ant,bee,cat,dog,egg,fire,goes,hat,ink,jet,kit") do echo %A %B ant ink C:\>for /f "tokens=11 delims=," %A in ("ant,bee,cat,dog,egg,fire,goes,hat,ink,jet,kit") do echo %A kit Salmon Trout's way is by far the most bullet proof. If someone puts a period in their file name besides the extension then you are going to start getting undesirable results. So if your file name is My.File.Name.txt, you are not going to get what you want for your output if you are using the delimiters option. By using the MODIFIERS like Salmon Trout did, you will always get the extension removed from the end. I do realize that you can use a number in a FOR LOOP but it is just really bad habit to get into. Just like Salmon Trout warns us for using Double Colons for comments. That too is probably bad coding practice and I am guilty of it but I just like the way it looks. When you write batch files that are hundreds of lines long that use command line input and are calling out to other batch files and other labels you can easily get Mixed up on When you are suppose to be using %%1 versus %1 in your coding. Over the years I can't recall a programming language that used numbers as the FOR Loop variable. My experience is limited to Fortran, Pascal, Basic and shell scripting like BASH, Power Shell and Batch. Just my two cents.many thanks squashman, yes, i agree that upon consideration, ST's method is better, as i won't know for sure that there is only 1 period in the filename. and i don't use ::, just rem's I see that Salmon Trout already beat me too it, but here is what I was working on while he was quicker on the DRAW this time. The tokens within a for statement are not well explained in the help text. I too had some questions on them and CH helped steer me in the right direction, so hopefully I can utilize what I was taught and pass it along. The particular statements within the quotes in the for command help to determine how the input is parsed and assigned. The best way to see this would be to use some examples. For all of these examples, we are going to have a variable called testset. Code: [Select]set testset=one two three four five six seven Now you already know that the delims= statement allows you to set what the delimiters are. The default delimiters are the space and carriage returns. So for the following for statement, the additional text would be what variables are equal to. Code: [Select]for /f %a in ("%testset%") do ( echo %A = %a echo %B = %b echo %C = %c echo %D = %d echo %E = %e echo %F = %f echo %G = %g ) >%A = one >%B = two >%C = three >%D = four >%E = five >%F = six >%G = seven The %a in this case is the explicit token, the one that you explicitly define. The tokens that follow it are the implicit tokens that the for command implicitly defines and assigns. Nothing groudbreaking yet, but we need some ground work to break. With "tokens=", you are able to define which "tokens" are actually assigned data. It gets a little hard to explain, but perhaps is easier to show. Code: [Select]for /f "tokens=1,2" %a in ("%testset%") do echo %a %b >one two To specifically answer your question, the comma seperates the tokens while the dash signifies a range. Try to follow how this next one works. Code: [Select]for /f "tokens=2,4-6" %a in ("%testset%") do echo %a %b %c %d >two four five six Because in this setup, the first delimited token (one) is not designated as assignable, it is discarded and the second delimited token (two) which is designated as assignable is assigned to the first (explicit) token. In this construct, it's easier to think of two different sets of tokens being used (I wish I was creative enough right now to think of a different name to use for them, but I'm not.) The only other caveat to add is the addition of the "*" at the end of the tokens statement. Using tokens=1* in the second code (for /f "tokens=1,2" %a in ("%testset%") do echo %a %b) will actually give you the output of one two three four five six seven because it takes the first delimited token and assigns it to %a, but the "*" tells it to take the rest of the line and assign it to %b. Please let us know if you have any other questions on the for statement, and I'll try to locate the post that taught me about all of this. Quote from: Raven19528 on December 13, 2011, 12:08:16 PM The default delimiters are the space and carriage returns.From the help file. Code: [Select]delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab.Many many thanks for this education, you hit the nail on the head with the explanation. precisely what i was looking for with respect to tokens. and yes if you have that link, i will go and further my knowledgebase. Quote from: Raven19528 on December 13, 2011, 12:08:16 PM I see that Salmon Trout already beat me too it, but here is what I was working on while he was quicker on the draw this time. |
|