|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
problem with string substititution using delayedexpansion
I have a problem with string parsing. This probably has to do with strings with exclamation marks and colons, but I think this should work anyway. Does anyone know? ---clip--- @echo off rem this works set str=just test!ng: set str2=%str:*test=work% echo %str2% pause rem doesn't work too well with delayedexpansion setlocal enabledelayedexpansion enableextensions set str=just test!ng: set str2=%str:*test=doesn't work% echo %str2% pause rem or the way I need it set str=just test!ng: set str2=!str:*test=doesn't work! echo %str2% pause ---clip--- -- Antti Actually, the code works, it just does what you have told it to do, not what
you want it to do. So what, exactly, is it you want it to do? Modify a variable whose contents include punctuation such as "!" to something "test!ng" to "work!ng". I tried it interactively, and it seemed to "work" for me, at least the second block where "%" is used to flag a variable reference. Instead of showing just your script, show us the actual output, and also show us what you would like the output to be. /Al Show quote "Antti" <a*@cd.ef> wrote in message news:egwSW8SdHHA.3976@TK2MSFTNGP06.phx.gbl... > Hi! > > I have a problem with string parsing. This probably has to do with strings > with exclamation marks and colons, but I think this should work anyway. Does > anyone know? > > ---clip--- > > @echo off > > rem this works > set str=just test!ng: > set str2=%str:*test=work% > echo %str2% > pause > > rem doesn't work too well with delayedexpansion > setlocal enabledelayedexpansion enableextensions > set str=just test!ng: > set str2=%str:*test=doesn't work% > echo %str2% > pause > > rem or the way I need it > set str=just test!ng: > set str2=!str:*test=doesn't work! > echo %str2% > pause > > ---clip--- > > > -- > Antti > > Well, just c&p it to a .bat and run it and you'll see that the first block
works, the second doesn't (with delayed expansion that is, you can have that interactively too with some switches)and neither does the third. If you don't get it: I wan't to find the first occurrence of some characters in a string and delete everything before it. That's what the first one does. The second and the third blocks show that with delayedexpansion (which is necessary in my script (very very long and uncommented - I won't paste it here)) it doesn't work. Does anyone know how to make it work or work around it using only cmd. Thanks. -- Show quoteAntti "Al Dunbar [MS-MVP]" <alan-no-drub-spam@hotmail.com> wrote in message news:O0graPadHHA.420@TK2MSFTNGP04.phx.gbl... > Actually, the code works, it just does what you have told it to do, not > what > you want it to do. > > So what, exactly, is it you want it to do? Modify a variable whose > contents > include punctuation such as "!" to something "test!ng" to "work!ng". I > tried > it interactively, and it seemed to "work" for me, at least the second > block > where "%" is used to flag a variable reference. > > Instead of showing just your script, show us the actual output, and also > show us what you would like the output to be. > > > /Al > > "Antti" <a*@cd.ef> wrote in message > news:egwSW8SdHHA.3976@TK2MSFTNGP06.phx.gbl... >> Hi! >> >> I have a problem with string parsing. This probably has to do with >> strings >> with exclamation marks and colons, but I think this should work anyway. > Does >> anyone know? >> >> ---clip--- >> >> @echo off >> >> rem this works >> set str=just test!ng: >> set str2=%str:*test=work% >> echo %str2% >> pause >> >> rem doesn't work too well with delayedexpansion >> setlocal enabledelayedexpansion enableextensions >> set str=just test!ng: >> set str2=%str:*test=doesn't work% >> echo %str2% >> pause >> >> rem or the way I need it >> set str=just test!ng: >> set str2=!str:*test=doesn't work! >> echo %str2% >> pause >> >> ---clip--- >> >> >> -- >> Antti >> >> > >
Show quote
On Apr 2, 9:59 am, "Antti" <a***@cd.ef> wrote: The problem is the exclamation point (of course). I assume it's> Hi! > > I have a problem with string parsing. This probably has to do with strings > with exclamation marks and colons, but I think this should work anyway. Does > anyone know? > > ---clip--- > > @echo off > > rem this works > set str=just test!ng: > set str2=%str:*test=work% > echo %str2% > pause > > rem doesn't work too well with delayedexpansion > setlocal enabledelayedexpansion enableextensions > set str=just test!ng: > set str2=%str:*test=doesn't work% > echo %str2% > pause > > rem or the way I need it > set str=just test!ng: > set str2=!str:*test=doesn't work! > echo %str2% > pause > > ---clip--- > > -- > Antti because the command processor interprets it as the beginning of the variable and uses the end of the line as the end. The only workaround I found was to replace the exclamation point temporarily (with delayed expansion off) and then put it back later ... rem doesn't work with ! & delayedexpansion set str=just test!ng: rem Workaround set str1=%str:!=@@% echo %str1% pause setlocal enabledelayedexpansion enableextensions set str2=%str1:*test=is work% rem Workaround setlocal disabledelayedexpansion set str2=%str2:@@=!% echo %str2% pause rem Workaround set str2= setlocal enabledelayedexpansion set str2=!str1:*test=is work! setlocal disabledelayedexpansion rem Workaround setlocal disabledelayedexpansion set str2=%str2:@@=!% pause Tom Lavedas =========== http://members/cox.net/tglbatch/wsh/ "Tom Lavedas" <tglba***@cox.net> wrote in message I thought of that, too. If the colon is removed from the string, the news:1175612493.238916.295490@n76g2000hsh.googlegroups.com... > The problem is the exclamation point (of course). I assume it's > because the command processor interprets it as the beginning of the > variable and uses the end of the line as the end. substitution almost works (! vanishes) even with the exclamation point. If I put %-signs in the first block, it won't work in a batch, but will interactively. Strange. > The only workaround I found was to replace the exclamation point That is the way I suppose. I will probably just replace all the exclamation > temporarily (with delayed expansion off) and then put it back > later ... points (and % if possible) in the file with dots or something, since they are not that important in this case. Thanks. -- Antti |
|||||||||||||||||||||||