PUSHP is to the PATH as PUSHD is to the Current Directory. POPP works like POPD. Once popped, it's gone. Keep Ii Simple.
Edit: marketing paragraph: PUSHD fails if you enter PUSHD garbage when garbage is not valid. If you enter PATH garbage, then that is what you get and you can't find anything that used to be on the PATH. A simple typing error on PATH MyFolder;%PATH%; appears to work but in effect might as well have been ;;%PATH%; and not what was expected. PUSHP validates any folder before adding it to the PATH and even saves the existing path, anyway.
So here they are. Most of the code is for documentation. Edit: POPP has been streamlined (including fixes ):
Code: Select all
@Echo Off
:PopP
Rem first run of POPP.bat will save the current PATH as baseline level 1.
If NOT DEFINED pop0 (Set "pop0=1"
set "pop1=%PATH%;"
)
set "pop1=%pop1:;;=;%"
Rem get saved path for current POPP level, inform and prepare level housekeeping
Set "popName=pop%pop0%"
Set "popLast=%pop0%"
Call Set "popValue=%%%%%%popName%%%%%%"
If "%pop0%" GTR "1" (
Set "popDel=%popName%"
Set /A "pop0-=1"
Call Echo.Popping PATH from level %%popLast%% to %%pop0%%.
) Else (
Set "popDel=popDel"
Call Echo.Refreshing PATH to baseline level %%pop0%%.
)
Call PATH %popValue%
Rem purge POPPed variable and prevent leaked variables
Set "%popDel%="
Set "popDel="
Set "popValue="
Set "popName="
Set "popLast="
Rem display HELP if reuqested
If /I "%~1." NEQ "." (
:: Create temporary help file
Call :TempFile "Help.txt"
Type "%Temp%\~Scripts~\%~n0_Help.txt"
)
Exit /B
:TempFile NameExtValue[In]
@echo Off & SetLocal EnableDelayedExpansion
For %%E In ("%~1") DO (
Set "fName=%%~nxE"
Set "fExt=%%~xE"
)
If Not Exist "%Temp%\~Scripts~" MkDir "%Temp%\~Scripts~"
If Exist "%Temp%\~Scripts~\%~n0_%fName%" DEL "%Temp%\~Scripts~\%~n0_%fName%"
For /f "delims=:" %%i in (
'findstr /nir /c:"^goto[ ]*\:EndOf%fName%" /c:"^\:EndOf%fName%" "%~fs0"'
) Do Set "DataRange=!DataRange! %%i"
For /f "tokens=1,2" %%i in ("%DataRange%") Do (Set /A "BeginData=%%i+1" & Set /A "EndData=%%j-1")
(For /L %%i In (2 1 %BeginData%) Do Set /P "="
For /L %%i In (!BeginData! 1 %EndData%) Do (
Set "line=" &Set /P "line="
Echo(!line!
)
) < "%~f0" >"%Temp%\~Scripts~\%~n0_%fName%"
EndLocal
Exit /B
GoTo :EndOfHelp.txtFile
POPP ["flag" | /?]
This script restores the PATH to the PATH last saved by either PUSHP or POPP.
"flag" is used to specify a request for help.
It may have any value, but whether specified or not
the POPP function is executed.
USAGE Notes:
If there is no saved PATH because neither PUSHP nor POPP has run, the
first run of POPP will save the current PATH as baseline level 1.
This stays available for the next and any subsequent POPP commands.
POPP can be used to restore the PATH after a successful PUSHP
or POPP even if the active PATH becomes corrupted by other means.
Saved POPP levels are easily examined using the command: SET pop
:EndOfHelp.txtFile
Code: Select all
@Echo Off&SetLOCAL EnableDelayedExpansion &Rem.and keep environment uncluttered.
Set "VarName=%~1"
If NOT DEFINED VarName (
:: Create temporary help file
Call :TempFile "Help.txt"
Type "%Temp%\~Scripts~\%~n0_Help.txt"
Exit /B
) Else (
Call Set "VarValue=%%!VarName!%%"
rem Echo !VarName!=!VarValue!
If NOT DEFINED VarValue (
rem Echo.Working with !VarName!
Set "input=!VarName!"
) Else (
rem Echo.Working with !VarValue!
Set "input=!VarValue!"
)
)
For /F "usebackq tokens=*" %%A In ('"%input%"') Do (
If Exist "%%A\" (
Set "Arg1=%%~fA"
) Else (
If Exist "%%A" (
Rem. Save the containing folder whan argument is a file
Set "Arg1=%%~dpA"
Set "Arg1=!Arg1:~,-1!"
) Else (
Set "Arg1="
)
)
)
If NOT DEFINED Arg1 (
Echo.The file defined by '%input%' could not be found.
:: Create temporary help file
Call :TempFile "Help.txt"
Type "%Temp%\~Scripts~\%~n0_Help.txt"
Exit /B
) Else (Endlocal&Set "push=%Arg1%")
If NOT DEFINED pop0 (Set "pop0=1"
set "pop1=%PATH%;"
)
set "pop1=%pop1:;;=;%"
Set /A "pop0+=1"
Echo.Pushing PATH to level %pop0%.
Set "pop%pop0%=%PATH%"
Set "VarName=%~2"
If NOT DEFINED VarName GOTO :NoVar
PATH %PATH%;%Push%;
Echo.'%push%' appended to PATH.
GOTO :Var
:NoVar
PATH %push%;%PATH%;
Echo.'%push%' now precedes PATH.
:Var
Set "PATH=%PATH:;;=;%"
Exit /B
:TempFile NameExtValue[In]
@echo Off & SetLocal EnableDelayedExpansion
For %%E In ("%~1") DO (
Set "fName=%%~nxE"
Set "fExt=%%~xE"
)
If Not Exist "%Temp%\~Scripts~" MkDir "%Temp%\~Scripts~"
If Exist "%Temp%\~Scripts~\%~n0_%fName%" DEL "%Temp%\~Scripts~\%~n0_%fName%"
For /f "delims=:" %%i in (
'findstr /nir /c:"^goto[ ]*\:EndOf%fName%" /c:"^\:EndOf%fName%" "%~fs0"'
) Do Set "DataRange=!DataRange! %%i"
For /f "tokens=1,2" %%i in ("%DataRange%") Do (Set /A "BeginData=%%i+1" & Set /A "EndData=%%j-1")
(For /L %%i In (2 1 %BeginData%) Do Set /P "="
For /L %%i In (!BeginData! 1 %EndData%) Do (
Set "line=" &Set /P "line="
Echo(!line!
)
) < "%~f0" >"%Temp%\~Scripts~\%~n0_%fName%"
EndLocal
Exit /B
GoTo :EndOfHelp.txtFile
PUSHP ["string"[ "flag"]]
This script adds a folder to the current PATH and saves previous PATH
information so that it can be restored using the POPP command.
"string" is an argument used to specify the folder to be added:
It may be a variable name or a value that can refer to a relative or
absolute file or folder. The containing folder is used in the
case of a file.
If neither is found this help is displayed. (PUSHP /? also works.)
Absence of a first argument is treated as a request for help.
"flag" is used to specify that folder is to be added after the Path:
It may have any value, but if none is specified
the folder will be added before the PATH.
USAGE Notes:
POPP can be used to restore the PATH after a successful PUSHP
or POPP even if the active PATH becomes corrupted by other means.
Saved POPP levels are easily examined using the command: SET pop
:EndOfHelp.txtFile
My question is whether there is a disadvantage in using
Code: Select all
Set "PATH=Routines;%PATH%;"
Code: Select all
PATH Routines;%PATH%;
John A.