error pushd setlocal popd endlocal

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

error pushd setlocal popd endlocal

#1 Post by Ed Dyreen » 17 Jan 2015 16:45

This batch runs from drive Z:\..., which is permanently mapped to "\\ed-serv-0\ed-serv-0"

Code: Select all

@echo off

echo.test0
cd
pushd "\\ed-serv-0\ed-serv-0"
cd

setlocal
popd

echo.
echo.test1
cd
endlocal
cd

pause
exit
The system can't find the specified path??

Code: Select all

test0
Z:\ED\VIP\PROJ\DEV\doskit\doskitXP32x86 v20150111\Hello world !
Y:\

test1
Z:\ED\VIP\PROJ\DEV\doskit\doskitXP32x86 v20150111\Hello world !
Het systeem kan het opgegeven pad niet vinden.
Z:\ED\VIP\PROJ\DEV\doskit\doskitXP32x86 v20150111\Hello world !
Druk op een toets om door te gaan. . .
I don't want to change "\\ed-serv-0\ed-serv-0"
I don't want endlocal before popd in which case the above error does not occur.

:evil: Darn, my evil plan is foiled !

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: error pushd setlocal popd endlocal

#2 Post by Aacini » 17 Jan 2015 20:33

Yes. SETLOCAL command save the current directory in an area "parallel" to the environment itself that ENDLOCAL restores; this is an undocumented behavior discussed at this post. When your SETLOCAL command executes, the current directory is Y:\ created by PUSHD command, so the Y:\ is saved. Then the POPD is executed and Y:\ disapppear. After that, when the ENDLOCAL executes, the undocumented behavior try to restore the saved directory, that is Y:\ and, of course, it does not exists...

I think there is no way to avoid this error, unless you modify the order of commands like this: PUSHD SETLOCAL ENDLOCAL POPD or SETLOCAL PUSHD POPD ENDLOCAL.

Antonio

Post Reply