Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
vin97
- Posts: 35
- Joined: 17 Apr 2020 08:30
#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:
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#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
#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: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#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
Steffen
-
vin97
- Posts: 35
- Joined: 17 Apr 2020 08:30
#5
Post
by vin97 » 15 Jun 2022 03:20
Thanks. Was looking in the wrong place (cmd switches).
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#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
).
Steffen
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#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