That's a good solution aGerman, and the same one I came up with working on this a few weeks ago:
viewtopic.php?f=3&t=2058As far as where to learn, here is a great place, just ask what !field2:~1! means
and you'll get an answer. It's removing the 1st character from the value !field2! which happens to be a space because of this part: ("!line:,=, !"). See multiple commas would be treated as a single comma by the FOR /F loop using , as the delimeter so you'd lose your empty fields.
What aGerman did to resolve this was to change your commas (,) into comma space (, ) so that you could maintain your field delimeter and still use the , as a FOR delimeter. At the end you have to get rid of the spaces though unless you want them in there.
I use a different method, but it's along the same lines. I add a " at the beginning and end of my string and then replace my commas with "," so I end up with each field being double-quoted then I just use the ~ with my FOR variables %%~a, %%~b, %%~c, etc... to remove the quotes.
So I do something like this:
Code: Select all
...
set "line="%%a""
set "line=!line:,=","!"
...
Then this line can simply be:
Code: Select all
for /f "tokens=1-17* delims=," %%b in (!line!) do (
instead of
Code: Select all
for /f "tokens=1-17* delims=," %%b in ("!line:,=, !") do (
And all of your set "fieldx=%%y" lines would be:
Code: Select all
if "%%c" neq "" set "field2=%%~c"
The upside is that field1 no longer has to contain a value, because if it doesn't it's going to = "" instead of <space>
Back to the question on learning... FOR /? and SET /? will both help you out tremendously with parsing files and you can always post your code here and ask questions most folks here are happy to help.
EDIT (again): Here's a thread discussing the substring/replacement being used here:
viewtopic.php?f=3&t=2115