Page 1 of 1
Multiple delimiters are treated as one (for /f)
Posted: 01 Aug 2012 03:41
by miskox
Hi all,
maybe this questoin was answered before (couldn't find it). So here it is:
Code: Select all
for /f "tokens=1,2 delims=;" %%c in (myfile.tmp) do echo %%c %%d
myfile.txt contains
Results:
Preferred results:
Is this possible?
The source file is an Excel export (csv) so the fields I need are at the fixed positions.
Thank you.
Saso
Re: Multiple delimiters are treated as one (for /f)
Posted: 01 Aug 2012 04:36
by jeb
Hi miskox,
it can be done, but it's need a trick, as multiple delims are treated as only one.
The other problem is that ";" is also the EOL character, so if it's the first character in a line, the complete line will be skipped.
You need to prefix/postfix all your delim character with a non delim character first, and later remove it again.
Code: Select all
setlocal DisableDelayedExpansion
for /f "EOL=: delims=" %%L in (myfile.tmp) do (
set "line=%%L"
setlocal EnableDelayedExpansion
set "preparedLine=#!line:;=;#!"
FOR /F "tokens=1,2 delims=;" %%c in ("!preparedLine!") DO (
endlocal
set "param1=%%c"
set "param2=%%d"
setlocal EnableDelayedExpansion
set "param1=!param1:~1!"
set "param2=!param2:~1!"
echo $1=!param1! $2=!param2!
endlocal
)
)
jeb
Re: Multiple delimiters are treated as one (for /f)
Posted: 01 Aug 2012 05:39
by miskox
Thank you! Wow!
Very impressive. It took me 10 minutes to understand it.
One more question:
my example has a semicolon in the 1st position but it is *not* treated as an EOL character. First non semicolon character is treated as first parameter and so on.
Saso
Re: Multiple delimiters are treated as one (for /f)
Posted: 01 Aug 2012 06:56
by jeb
miskox wrote:One more question:
my example has a semicolon in the 1st position but it is *not* treated as an EOL character. First non semicolon character is treated as first parameter and so on.
Good question
The rule is, the default EOL character is a semicolon, but the EOL will be disabled when the EOL character is in the list of the delimiters.
So in your case it will be disabled, but in my code I need to "disable" it with setting
But there are also ways to disable it really not simply set it to another value.
jeb