Multiple delimiters are treated as one (for /f)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Multiple delimiters are treated as one (for /f)

#1 Post by miskox » 01 Aug 2012 03:41

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

Code: Select all

1;2;3;;;4
;;;a;b;c
;y;z;


Results:

Code: Select all

1 2
a b
y z


Preferred results:

Code: Select all

1 2
<empty> <empty>
<empty> y


Is this possible?

The source file is an Excel export (csv) so the fields I need are at the fixed positions.

Thank you.
Saso

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Multiple delimiters are treated as one (for /f)

#2 Post by jeb » 01 Aug 2012 04:36

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

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Multiple delimiters are treated as one (for /f)

#3 Post by miskox » 01 Aug 2012 05:39

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Multiple delimiters are treated as one (for /f)

#4 Post by jeb » 01 Aug 2012 06:56

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

Code: Select all

EOL=:

But there are also ways to disable it really not simply set it to another value.

jeb

Post Reply