Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Side effect of SETLOCAL

37 views
Skip to first unread message

Paul Emmons

unread,
Apr 11, 2018, 7:19:11 PM4/11/18
to
Consider this short demonstration batch file (running under Windows
7):
______________________________________
setlocal
Z:
cd \TEMP
dir
______________________________________

The dir command lists the files of Z:\temp, proving that this is
indeed the current directory of Z: at the time.

However, after termination, the current directory of Z: reverts
to whatever it was before execution. Furthermore, if before
execution the current drive and directory per the command prompt
are for another drive, e.g. C:\, this situation is also restored.

Without setlocal, the current directory will remain Z:\TEMP.

Maybe this behavior is common knowledge to everyone here, but the
help for SETLOCAL does not mention it.








B00ze

unread,
Apr 11, 2018, 10:48:09 PM4/11/18
to
On 2018-04-11 19:19, Paul Emmons <pemm...@comcast.net> wrote:

> Consider this short demonstration batch file (running under Win7):
> setlocal
> Z:
> cd \TEMP
> dir
>
> The dir command lists the files of Z:\temp, proving that this is
> indeed the current directory of Z: at the time.
>
> However, after termination, the current directory of Z: reverts
> to whatever it was before execution. Furthermore, if before
> execution the current drive and directory per the command prompt
> are for another drive, e.g. C:\, this situation is also restored.
>
> Without setlocal, the current directory will remain Z:\TEMP.
>
> Maybe this behavior is common knowledge to everyone here, but the
> help for SETLOCAL does not mention it.

Ah! I didn't know this! And me going PUSHD/POPD to be nice to people,
there is no need! :-)

Thanks!
Best Regards,

--
! _\|/_ Sylvain / B00...@hotmail.com
! (o o) Member:David-Suzuki-Fdn/EFF/Red+Cross/SPCA/Planetary-Society
oO-( )-Oo WORK HARDER!... Millions on welfare depend on you!

Herbert Kleebauer

unread,
Apr 12, 2018, 3:20:38 AM4/12/18
to
On 12.04.2018 01:19, Paul Emmons wrote:
> Consider this short demonstration batch file (running under Windows
> 7):
> ______________________________________
> setlocal
> Z:
> cd \TEMP
> dir
> ______________________________________
>
> The dir command lists the files of Z:\temp, proving that this is
> indeed the current directory of Z: at the time.
>
> However, after termination, the current directory of Z: reverts
> to whatever it was before execution. Furthermore, if before
> execution the current drive and directory per the command prompt
> are for another drive, e.g. C:\, this situation is also restored.

Isn't this the only purpose of setlocal?

setlocal /?

Begins localization of environment changes in a batch file. Environment
changes made after SETLOCAL has been issued are local to the batch file.
ENDLOCAL must be issued to restore the previous settings. When the end
of a batch script is reached, an implied ENDLOCAL is executed for any
outstanding SETLOCAL commands issued by that batch script.


JJ

unread,
Apr 12, 2018, 11:52:46 AM4/12/18
to
I'm aware that SETLOCAL does affect the working directory, but I concur with
Paul Emmons.

SETLOCAL's help only mention "environment". For those who are new to CMD,
would think that it means the environment variables. For those who are
already familiar with CMD, may have already know this and the "environment"
in that context, means the command prompt (or terminal) environment.

However, this is not true (at least on Windows 7 version of CMD). SETLOCAL
does not affect the states of: ECHO, DPATH, VERIFY, color, and title of the
console window. So IMO, SETLOCAL's help is not being clear enough.

npocmaka

unread,
Apr 12, 2018, 12:29:35 PM4/12/18
to

npocmaka

unread,
Apr 12, 2018, 12:41:29 PM4/12/18
to
DPATH and PATH are affected.KEYS is not.MODE is not.

JJ

unread,
Apr 13, 2018, 10:36:22 AM4/13/18
to
MODE is not even an internal command.
But it doesn't really matter.
The point is that SETLOCAL's help is not being clear.

npocmaka

unread,
Apr 13, 2018, 10:47:04 AM4/13/18
to
I think it is. It restores only the command states that are relying on variables - by restoring the variables. In the case of the directory i think it is the %__CD__% variable which cannot be changed directly by the user - but looks like ENDLOCAL has no restrictions over it.(%CD% can be overwritten by the user but it does not change the current directory)
0 new messages