Can I double delay "delayed expansion" ?
Posted: 27 Jun 2010 11:54
I need to do something like
CALL :SET_CD "%ALLUSERSPROFILE%\The rest of the path"
with the SPECIAL feature that the code held in :SET_CD must :-
expand the environmental variable and change the current directory accordingly ;
set a flag to denote success or failure, and upon failure set CD to a safe default.
All that works,
BUT I now wish :SET_CD to also display for DEBUG the UNexpanded text ALLUSERSPROFILE,
and yet still be able to expand that variable to actually aim the Current Directory.
When CMD.EXE processes the line CALL :SET_CD "%ALLUSERSPROFILE%
it immediately expands it so that :SET_CD never knows about ALLUSERSPROFILE.
I have tried replacing "%ALLUSERSPROFILE% with :-
"%%ALLUSERSPROFILE%%" - not got it fully working yet;
"^%ALLUSERSPROFILE^%" - not got it fully working yet;
"!ALLUSERSPROFILE!" - so near - yet so far ! !
I was able to use CALL :SET_CD "!ALLUSERSPROFILE!\The rest of the path"
and if the script commenced with SETLOCAL enabledelayedexpansion,
then CMD.EXE fully expanded CALL :SET_CD "!ALLUSERSPROFILE!
and all the code worked properly, but my debug text ALLUSERSPROFILE was still missing.
If the script did NOT commence with SETLOCAL enabledelayedexpansion,
then the :SET_CD code worked beautifully, and was able to show me the original text
"!ALLUSERSPROFILE!\The rest of the path"
After which the SET_CD code used SETLOCAL enabledelayedexpansion
and then it worked perfectly as before,
testing the validity of "...The rest of the path",
and suitably changing the current directory and setting the error/success flag.
UNFORTUNATELY when the :SET_CD code terminated, there was an implied ENDLOCAL,
with the result that the Current Drive and Directory, and also the error/success flag,
were exactly as they had been before :SET_CD had been executed,
hence subsequent code to delete known (possible) junk was aiming at the wrong place.
Is there some alternative trick that can be suggested.
All I can think of is to add a second argument, e.g.
CALL :SET_CD "%ALLUSERSPROFILE%\The rest of the path" ALLUSERSPROFILE
but there are many such lines with many different environmental variables,
and the original scripts I have inherited had several typos which I identified by rigorous testing,
and I do not wish to "climb the greasy pole" by adding my own individual specific extra typos.
My top level code is many sets of the following sort of thing :-
Regards
Alan
CALL :SET_CD "%ALLUSERSPROFILE%\The rest of the path"
with the SPECIAL feature that the code held in :SET_CD must :-
expand the environmental variable and change the current directory accordingly ;
set a flag to denote success or failure, and upon failure set CD to a safe default.
All that works,
BUT I now wish :SET_CD to also display for DEBUG the UNexpanded text ALLUSERSPROFILE,
and yet still be able to expand that variable to actually aim the Current Directory.
When CMD.EXE processes the line CALL :SET_CD "%ALLUSERSPROFILE%
it immediately expands it so that :SET_CD never knows about ALLUSERSPROFILE.
I have tried replacing "%ALLUSERSPROFILE% with :-
"%%ALLUSERSPROFILE%%" - not got it fully working yet;
"^%ALLUSERSPROFILE^%" - not got it fully working yet;
"!ALLUSERSPROFILE!" - so near - yet so far ! !
I was able to use CALL :SET_CD "!ALLUSERSPROFILE!\The rest of the path"
and if the script commenced with SETLOCAL enabledelayedexpansion,
then CMD.EXE fully expanded CALL :SET_CD "!ALLUSERSPROFILE!
and all the code worked properly, but my debug text ALLUSERSPROFILE was still missing.
If the script did NOT commence with SETLOCAL enabledelayedexpansion,
then the :SET_CD code worked beautifully, and was able to show me the original text
"!ALLUSERSPROFILE!\The rest of the path"
After which the SET_CD code used SETLOCAL enabledelayedexpansion
and then it worked perfectly as before,
testing the validity of "...The rest of the path",
and suitably changing the current directory and setting the error/success flag.
UNFORTUNATELY when the :SET_CD code terminated, there was an implied ENDLOCAL,
with the result that the Current Drive and Directory, and also the error/success flag,
were exactly as they had been before :SET_CD had been executed,
hence subsequent code to delete known (possible) junk was aiming at the wrong place.
Is there some alternative trick that can be suggested.
All I can think of is to add a second argument, e.g.
CALL :SET_CD "%ALLUSERSPROFILE%\The rest of the path" ALLUSERSPROFILE
but there are many such lines with many different environmental variables,
and the original scripts I have inherited had several typos which I identified by rigorous testing,
and I do not wish to "climb the greasy pole" by adding my own individual specific extra typos.
My top level code is many sets of the following sort of thing :-
Code: Select all
CALL :SET_CD "%ALLUSERSPROFILE%\Some path where unwanted junk resides"
CALL :ZAP "First bit of Junk"
CALL :ZAP "Next bit of Junk"
CALL :SET_CD "%USERPROFILE%\Another dwelling place for junk"
CALL :ZAP "More stuff to go"
... etc etc ...
Regards
Alan