How to clear global variables when launching cmd from within a batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vin97
Posts: 35
Joined: 17 Apr 2020 08:30

How to clear global variables when launching cmd from within a batch?

#1 Post by vin97 » 14 Jun 2022 17:42

What is the syntax for the start command to launch cmd.exe in a way where only specific variables are retained?

Example:

Code: Select all

set "VariableThatsSupposedToBePassedOn=X"
set "VariableThatsNotSupposedToBePassedOn=Y"
start "" cmd.exe "set "var=%VariableThatsSupposedToBePassedon%" & echo "%var%" & echo "%VariableThatsNotSupposedToBePassedOn%""
Result I want from the second cmd instance:

Code: Select all

"X"
""

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to clear global variables when launching cmd from within a batch?

#2 Post by ShadowThief » 14 Jun 2022 18:14

All child instances inherit the environment of their parents. If you don't want a variable to exist inside of a child instance, you'll have to unset the variable before you spawn the child process.

vin97
Posts: 35
Joined: 17 Apr 2020 08:30

Re: How to clear global variables when launching cmd from within a batch?

#3 Post by vin97 » 14 Jun 2022 18:55

Is there no way to spawn a completely fresh cmd instance?
I wouldn't have a problem utilizing temp files to pass on the few select variables that have to be kept.

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

Re: How to clear global variables when launching cmd from within a batch?

#4 Post by aGerman » 15 Jun 2022 01:02

START /I will spawn a process that inherits only from explorer.

caller.bat

Code: Select all

@echo off
set "foo=bar"
start "without /I" cmd /k "new.bat"
start "with /I" /I cmd /k "new.bat"
new.bat

Code: Select all

@echo off
set foo
Steffen

vin97
Posts: 35
Joined: 17 Apr 2020 08:30

Re: How to clear global variables when launching cmd from within a batch?

#5 Post by vin97 » 15 Jun 2022 03:20

Thanks. Was looking in the wrong place (cmd switches).

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

Re: How to clear global variables when launching cmd from within a batch?

#6 Post by aGerman » 15 Jun 2022 09:42

Haha, don't worry. The description for option /I is confunsing anyway (and the German help text is even completely meaningless here :roll:).

Steffen

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

Re: How to clear global variables when launching cmd from within a batch?

#7 Post by Aacini » 15 Jun 2022 11:06

You may use this method in any batch file:

Code: Select all

@echo off
setlocal

rem Remove all environment variables
rem but preserve certain ones

(
for /F "delims==" %%v in ('set') do set "%%v="
set "comspec=%comspec%"
set "path=%path%"
set "VariableThatsSupposedToBePassedOn=%VariableThatsSupposedToBePassedOn%"
)
Antonio

Post Reply