preserving exclamation marks in variable between setlocals batch -
trying preserve exclamation marks in variables. simplified, illustrative script below:
echo off set testvar="c:\windows\testofillegals[!]" echo pre-enablede: %testvar% setlocal enableextensions enabledelayedexpansion echo post-enablede: %testvar% setlocal disabledelayedexpansion echo ssetlocal sub-instance... echo testvar after re-disablede: %testvar% set modtestvar=%testvar% echo testvar new var, modtestvar: %modtestvar% endlocal & set "retvar2=%modtestvar%" echo modtestvar in main script: %retvar2% setlocal disabledelayedexpansion echo modtestvar, main script in setlocal diasblede instance: %retvar2% endlocal pause exit /b
this produces output:
pre-enablede: "c:\windows\testofillegals[!]" post-enablede: "c:\windows\testofillegals[]" ssetlocal sub-instance... testvar after re-disablede: "c:\windows\testofillegals[!]" testvar new var, modtestvar: "c:\windows\testofillegals[!]" modtestvar in main script: "c:\windows\testofillegals[]" modtestvar, main script in setlocal diasblede instance: "c:\windows\testofillegals[]"
why isn't exclamation mark being preserved in modtestvar? there way this?
(i know people have said before "post full script" - rather long , represents core issue. however, happy post if helpful).
thanks
it's preserved in variable, need use delayed expansion.
when used percent expansion in delayed expansion mode variable expanded , content, exclamation marks parsed parsed later, , single exclamation mark removed.
echo off set testvar="c:\windows\testofillegals[!]" echo pre-enablede: %testvar% setlocal enableextensions enabledelayedexpansion echo post-enablede: !testvar! setlocal disabledelayedexpansion echo ssetlocal sub-instance... echo testvar after re-disablede: %testvar% set modtestvar=%testvar% echo testvar new var, modtestvar: %modtestvar% endlocal & set "retvar2=%modtestvar%" echo modtestvar in main script: !retvar2! setlocal disabledelayedexpansion echo modtestvar, main script in setlocal diasblede instance: %retvar2% endlocal pause
the other/only problem when try transfer variable on endlocal barrier (like endlocal & set "retvar2=%modtestvar%"
).
not trivial.
this batch macro this, used %endlocal% modtestvar
setlocal disabledelayedexpansion set lf=^ set ^"\n=^^^%lf%%lf%^%lf%%lf%^^" %= use ede enabledelayeexpansion , dde disabledelayedexpansion =% set ^"endlocal=for %%# in (1 2) if %%#==2 (%\n% setlocal enabledelayedexpansion%\n% %= take variable names varname array =%%\n% set varname_count=0%\n% %%c in (!args!) set "varname[!varname_count!]=%%~c" ^& set /a varname_count+=1%\n% %= build 1 variable list of set statements each variable delimited newlines =%%\n% %= lists looks --> set result1=mycontent\n"set result1=mycontent1"\nset result2=content2\nset result2=content2\n =%%\n% %= each result exists 2 times, first case returning dde, second ede =%%\n% %= correct line detected (missing) enclosing quotes =%%\n% set "retcontent=1!lf!"%\n% /l %%n in (0 1 !varname_count!) (%\n% /f "delims=" %%c in ("!varname[%%n]!") (%\n% set "content=!%%c!"%\n% set "retcontent=!retcontent!"set !varname[%%n]!=!content!"!lf!"%\n% if defined content (%\n% %= complex block replacing '!' '^!' =%%\n% %= first replacing '"'->'""q' '^'->'^^' =%%\n% set ^"content_ede=!content:"=""q!"%\n% set "content_ede=!content_ede:^=^^!"%\n% %= it's poosible use call set , replace '!'->'""e!' =%%\n% call set "content_ede=%%content_ede:^!=""e^!%%"%\n% %= it's possible replace '""e' '^', effectivly '!' -> '^!' =%%\n% set "content_ede=!content_ede:""e=^!"%\n% %= restore quotes =%%\n% set ^"content_ede=!content_ede:""q="!"%\n% ) else set "content_ede="%\n% set "retcontent=!retcontent!set "!varname[%%n]!=!content_ede!"!lf!"%\n% )%\n% )%\n% %= return variables retcontent on barrier =%%\n% /f "delims=" %%v in ("!retcontent!") (%\n% %= first line can contain single 1 =%%\n% if "%%v"=="1" (%\n% %= need call endlocal twice, there 1 more setlocal in macro =%%\n% endlocal%\n% endlocal%\n% ) else (%\n% %= true in ede =%%\n% if "!"=="" (%\n% if %%v==%%~v (%\n% %%v !%\n% )%\n% ) else if not %%v==%%~v (%\n% %%~v%\n% )%\n% )%\n% )%\n% ) else set args="
Comments
Post a Comment