Can I double delay "delayed expansion" ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Can I double delay "delayed expansion" ?

#1 Post by alan_b » 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 :-

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Can I double delay "delayed expansion" ?

#2 Post by aGerman » 27 Jun 2010 13:01

Sorry Alan, but that's too much for me. Think it depends on my bad English knowledge. I understood that you have problems with the expansion of variables, but not if you want it unexpanded or fully expanded.
How ever, have a look at this:-

Code: Select all

@echo off &setlocal
CALL :SET_CD "%%%%ALLUSERSPROFILE%%%%\Some path where unwanted junk resides"
pause
goto :eof

:SET_CD
echo %1
call set yourPath=%1
echo %yourPath%
cd /d %yourPath% 2>nul &&(
  echo CD successful
) || (
  echo CD failed
)
goto :eof



Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Can I double delay "delayed expansion" ?

#3 Post by alan_b » 27 Jun 2010 14:29

Thank you so much - you nailed it ! !

My only language skill is, in French, to :-
Open the door (but I cannot now remember which gender)
and to throw a chair out the Window.
I never got a GCE 'O' level for that - but I think standards have dropped since then !

You on the other hand have a far better understanding and use of English than many for whom it is the first (and only) language.

I now see my problem was that when I changed "%" to "%%" and got no success,
I failed to persevere and try "%%%%".

I should have taken more notice of my granny more than 60 years ago,
when she taught me the perseverance of "Incy Wincy Spider" - now on Internet :-
http://www.nurseryrhymes4u.com/NURSERY_ ... ge_39.html

Regards
Alan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Can I double delay "delayed expansion" ?

#4 Post by aGerman » 27 Jun 2010 15:11

alan_b wrote:I should have taken more notice of my granny more than 60 years ago,
when she taught me the perseverance of "Incy Wincy Spider" - now on Internet :-
http://www.nurseryrhymes4u.com/NURSERY_ ... ge_39.html

lol That's it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Well, as you should know you have to escape a % with a second %
CALL :SET_CD "%%%%ALLUSERSPROFILE%%%%\Some path where unwanted junk resides"

The CALL removes the red ones, so you have
"%%ALLUSERSPROFILE%%\Some path where unwanted junk resides"
in %1.


echo %1

The ECHO command removes the green ones, only the black ones are displayed.


call set yourPath=%1

The CALL command removes the green ones too, so you have
"%ALLUSERSPROFILE%\Some path where unwanted junk resides"
in %yourPath%.


echo %yourPath%

The ECHO command expands %ALLUSERSPROFILE% to the value. CD /D will do the same.

That's all.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Can I double delay "delayed expansion" ?

#5 Post by alan_b » 30 Jun 2010 14:20

Thank you so much for the solution to my problem.
That part of my script is now working fine.

Also thank you for the explanation of how the %%%% are progressively stripped down.

Alan

Post Reply