126594
Goto Top

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

Content-Key: 307586

Url: https://administrator.de/contentid/307586

Printed on: April 25, 2024 at 06:04 o'clock

Mitglied: 129813
Solution 129813 Jun 19, 2016 updated at 07:32:18 (UTC)
Goto Top
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.
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"  
)
But I would rather use a modern language like powershell, python or perl for csv processing, that's much more reliable.

Regards
Member: batchnewbie
Solution batchnewbie Jun 19, 2016 at 13:49:10 (UTC)
Goto Top
Hi,
vielleicht wenn möglich kannst du den delimiters ; durch ein anderes Zeichen zb. # vorher ersetzten...

set "VariableOrg=auto;boot;citrone;;ente"  
set "VariableNeu=%VariableOrg:;=#%  
echo VariableNeu: %VariableNeu%
for /f....
Mitglied: 126594
126594 Jul 02, 2016 at 14:21:57 (UTC)
Goto Top
Vielen DANK an alle, habs hinbekommen!!!