|
Answer» I'm trying to use a FOR loop to parse 4 comma-delimited values, some of which may be null values. The syntax I'm using isn't interpretting the null values as I'd like. Any suggestions? (I need to do this in a batch file.)
Here's my current code:
@echo off setlocal enabledelayedexpansion
Set Count=0 For /F "tokens=1,2,3,4* delims=," %%A in (TestInput.csv) Do ( Set Var1=%%A Set Var2=%%B Set Var3=%%C Set Var4=%%D Set /A Count+=1
echo Var1=!Var1! echo Var2=!Var2! echo Var3=!Var3! echo Var4=!Var4! echo Count=!Count! echo.
)
Here's what I have in TestInput.csv:
Line1Token1,,,Line1Token4 Line2Token1,,,Line2Token4
Here's my current output:
Var1=Line1Token1 Var2=Line1Token4 Var3= Var4= Count=1
Var1=Line2Token1 Var2=Line2Token4 Var3= Var4= Count=2
What I want returned is:
Var1=Line1Token1 Var2= Var3= Var4=Line1Token4 Count=1
Var1=Line2Token1 Var2= Var3= Var4=Line2Token4 Count=2 I tried everything I could think of and couldn't get a null value to be imported from the testinput.csv into the bat file. However, I was able to get the output to be very similar to how you want the output to work and may possibly work.
A space where you want your null values will be treated as a character and skip. So the testinput.csv would look like:
Line1Token1, , ,Line1Token4 Line2Token1, , ,Line2Token4
Would this work for what you're trying to do?Thanks for trying this out and for the suggestion. Unfortunately, I think we're pretty much stuck with the existing format of the input file. If I find out otherwise, at that point we may be able to put "NULL" or some other dummy text string in for each of the fields that's now empty, or the space that you suggested.
In doing some more research, I saw a reference elsewhere on the web referring to the behavior of FOR in ignoring null values between delimiters as "a misfeature, wart, or bug". After giving that opinion, it GOES on to suggest that maybe the use of the term "token" implies "that only logical elements are of interest, that is, the line is parsed into symantic units rather than split into LITERAL fields (the latter [being] more useful to batch programmers)."
I may have another way around this that I'm CONFIRMING with co-workers. The third item is always expected to be NULL, and the 4th item is expected to contain one of three possible text strings. Based on the 4th item's value, I believe I can determine whether the first or second item should be populated (with the other being NULL).
If the above holds true, I can still use a FOR loop to read in each line. Var1 and Var2 would always be populated. I'd probably limit the tokens to 3 and use the * in the FOR tokens list to pull any remaining text into Var3 -- and stop with an error if anything is found there. (I'd no longer use Var4.) Then, based on the value of Var2, I'd determine how to use the value in Var1, eventually storing all values away in a new set of VARIABLES for further processing.
Not want I wanted, but unless someone else comes up with another suggestion, looks like it may work.Quote from: ranman65 on October 18, 2007, 03:32:26 PM but unless someone else comes up with another suggestion
use a better language than batch for parsing files.
|