126594
Jun 18, 2016, updated at 21:09:44 (UTC)
1233
3
0
Batch for f Befehl erkennt doppelte Zeichen als eines
Hallo zusammen.
Ich versuche mich zur Zeit am Bearbeiten von Variablen in Batchdateien.
Eigentlich bin ich schon ganz gut voran gekommen.
Nun will ich eine Variable aufteilen, die durch ; getrennt sind nach jedem Querstrich soll der Text bis zum nächsteb Querstrich in eine extra Variable geschrieben werden.
Beispiel:
Variable=Text;Text1;Text2;;Text4;Text5;;Text7 oder Text;;Text2;Text3;Text4;Text5;;Text7
Schreibe ich jetzt die Vriablen mit:
for /f "tokens=1 delims=;" %%a in ("%variable%") do @set variable1=%%a
for /f "tokens=2 delims=;" %%a in ("%variable%") do @set variable2=%%a
for /f "tokens=3 delims=;" %%a in ("%variable%") do @set variable3=%%a
for /f "tokens=4 delims=;" %%a in ("%variable%") do @set variable4=%%a
usw.
So werden die doppelten ; als eines erkannt.
Die ; sind nicht immer an der gleichen Stelle, mein Ziel ist es, dass die Variable, die normalerweise zwischen 2 ; steht, einfach leer bleibt.
Geht das technisch überhaupt?
Viele Grüße und ein Danke für eure HIlfe ;)
Markus
Ich versuche mich zur Zeit am Bearbeiten von Variablen in Batchdateien.
Eigentlich bin ich schon ganz gut voran gekommen.
Nun will ich eine Variable aufteilen, die durch ; getrennt sind nach jedem Querstrich soll der Text bis zum nächsteb Querstrich in eine extra Variable geschrieben werden.
Beispiel:
Variable=Text;Text1;Text2;;Text4;Text5;;Text7 oder Text;;Text2;Text3;Text4;Text5;;Text7
Schreibe ich jetzt die Vriablen mit:
for /f "tokens=1 delims=;" %%a in ("%variable%") do @set variable1=%%a
for /f "tokens=2 delims=;" %%a in ("%variable%") do @set variable2=%%a
for /f "tokens=3 delims=;" %%a in ("%variable%") do @set variable3=%%a
for /f "tokens=4 delims=;" %%a in ("%variable%") do @set variable4=%%a
usw.
So werden die doppelten ; als eines erkannt.
Die ; sind nicht immer an der gleichen Stelle, mein Ziel ist es, dass die Variable, die normalerweise zwischen 2 ; steht, einfach leer bleibt.
Geht das technisch überhaupt?
Viele Grüße und ein Danke für eure HIlfe ;)
Markus
Please also mark the comments that contributed to the solution of the article
Content-Key: 307586
Url: https://administrator.de/contentid/307586
Printed on: April 26, 2024 at 13:04 o'clock
3 Comments
Latest comment
Hi.
This is normal behavior in a for-loop. Two or more characters which are delimiters and are directly flollowing each other, are combined as one.
You can avoid this with a replacement of those doubles before you loop.
But I would rather use a modern language like powershell, python or perl for csv processing, that's much more reliable.
Regards
This is normal behavior in a for-loop. Two or more characters which are delimiters and are directly flollowing each other, are combined as one.
You can avoid this with a replacement of those doubles before you loop.
set "variable=Text;Text1;Text2;;Text4;Text5;;Text7"
set "variable=%variable:;;=; ;%
for /f "tokens=1-7 delims=;" %%a in ("%variable%") do (
set "variable1=%%a"
set "variable2=%%b"
set "variable3=%%c"
set "variable4=%%d"
set "variable5=%%e"
)
Regards