home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,885 of 68,980   
   Nathan Phillip Brink to All   
   Re: setlocal does pushd/popd?   
   22 Jun 22 07:45:56   
   
   From: ohnobinki@gmail.com   
      
   An important observation that I have made is that SETLOCAL/ENDLOCAL does not   
   restore the PUSHD/POPD stack. So if you have a script which uses SETLOCAL and   
   PUSHD, the script will still insert the working directory at the time of the   
   PUSHD into the stack.    
   This disturbs the caller’s state, which you are unlikely to want if you are   
   using SETLOCAL. Thus, I recommend to use a pattern where you script begins   
   with SETLOCAL and then you simply use CD in the script. This way, no matter   
   how the script exits, the    
   caller’s working directory will be restored and the caller’s PUSHD/POPD   
   stack will not be altered.   
      
   To see the effects of SETLOCAL followed by PUSHD without any POPD, see this   
   following transcript:   
      
   C:\Users\ohnob>TYPE AppData\Local\Temp\xx1.cmd   
   @ECHO OFF   
   SETLOCAL   
   PUSHD %~dp0 || EXIT /B 1   
      
   C:\Users\ohnob>PUSHD ..   
      
   C:\Users>PUSHD ..   
      
   C:\>"%USERPROFILE%\AppData\Local\Temp\xx1.cmd"   
      
   C:\>CD "%USERPROFILE%"   
      
   C:\Users\ohnob>POPD   
      
   C:\>POPD   
      
   C:\Users>POPD   
      
   C:\Users\ohnob>POPD   
      
   C:\Users\ohnob>POPD   
      
   You can see that the first POPD I run returns me to the root directory even   
   though I never called PUSHD from that directory. The following is the   
   preferred pattern:   
      
   @ECHO OFF   
   SETLOCAL   
   CD %~dp0 || EXIT /B 1   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca