I aim to use a Python script that calls a batch script, which in turn calls a cmd file to load environment variables and run commands with them.
In my batch script, the first line loads environment variables by calling a cmd file, and the second line executes a command with the loaded environment and its arguments.
However, my problem is that the first line opens a new shell instance, and the second line is executed in the original shell instance before the environment is loaded.
I need to figure out how to run the commands in the new shell instance with the loaded environment.
cmd /k "C:\Program Files (x86)\Microsoft GSDK\Command Prompts\GamingDesktopVars.cmd" GamingDesktopVS2017
wdapp unregister myAppName
Here is the command file:
@echo off
REM ====================================================================
REM Set path variables
REM ==========================================================================
IF /I "%1"=="GamingDesktopVS2017" (
CALL :SetVS2017
)ELSE (
ECHO You must specify GamingDesktopVS2017 on the command line.
EXIT /B 1
)
IF "%GRDKEDITION%"=="" (
IF NOT "%2"=="" (
SET GRDKEDITION=%2
)ELSE (
REM Get the most recent GXDK edition installed.
CALL :GetLatestGRDK
)
)
IF %DTARGETVSVER% GEQ 15.0 (
SET GRDKVSPRERELEASE=-prerelease
REM Possible values include Microsoft.VisualStudio.Product.Professional,
REM Microsoft.VisualStudio.Product.Community and Microsoft.VisualStudio.Product.Enterprise
REM separated by spaces for multiple products.
IF "%GRDKVSPRODUCTS%"=="" (
IF NOT "%3"=="" (
SET GRDKVSPRODUCTS= -products %3
)
) ELSE (
SET GRDKVSPRODUCTS= -products %GRDKVSPRODUCTS%
)
IF "%GRDKVSVERSION%"=="" (
IF NOT "%4"=="" (
IF NOT "%4"=="NOPRE" (
SET GRDKVSVERSION= -version %4
)
)
) ELSE (
SET GRDKVSVERSION= -version %GRDKVSVERSION%
)
IF "%GRDKVSNOPRERELEASE%"=="" (
IF "%4"=="NOPRE" (
SET GRDKVSPRERELEASE=
)ELSE (
IF "%5"=="NOPRE" (
SET GRDKVSPRERELEASE=
)
)
) ELSE (
SET GRDKVSPRERELEASE=
)
)
IF "%GRDKEDITION%"=="" (
REM If no GXDK edition found then assume Durango.
SET XDKEDITION=000000
)
Echo Setting environment for using Microsoft Desktop %DTARGETVS% Gaming Tools
TITLE Desktop %DTARGETVS% Gaming Command Prompt
IF "%GamingSDK%"=="" (
CALL :GetGSDKInstallPath
)
IF "%GamingSDK%"=="" (
ECHO Microsoft Gaming Development Kit directory is not found on this machine.
EXIT /B 1
)
IF NOT "%GRDKEDITION%"=="000000" (
IF "%GamingGRDKBuild%"=="" (
CALL :GetGRDKBuildInstallPath
)
IF "%GamingGRDKEditionVersionFriendlyName%"=="" (
CALL :GetGRDKEditionVersionFriendlyName
)
IF "%GamingDesktopExtensionSdkBuild%"=="" (
CALL :GetGamingDesktopExtensionSdkPath
)
)
IF NOT "%GRDKEDITION%"=="000000" (
IF "%GamingGRDKBuild%"=="" (
ECHO Microsoft Gaming Development Kit build for %GRDKEDITION% is not found on this machine.
EXIT /B 1
)
IF "%GamingDesktopExtensionSdkBuild%"=="" (
ECHO Microsoft Desktop Development Kit SDK Extensions for %GRDKEDITION% are not found on this machine.
EXIT /B 1
)
IF NOT "%GamingGRDKEditionVersionFriendlyName%"=="" (
TITLE %GamingGRDKEditionVersionFriendlyName% Desktop %DTARGETVS% Gaming Command Prompt
)
)
IF "%WindowsSDKDir%" == "" (
CALL :GetWindowsSDKDir
)
IF "%WindowsSDKDir%"=="" (
ECHO Warning: Windows Software Development Kit directory is not found on this machine.
)
IF NOT "%WindowsSDKDir%" == "" (
if "%WindowsSDKVersion%" == "" (
CALL :GetWindowsSDKVersion
)
)
IF NOT "%WindowsSDKDir%" == "" (
IF "WindowsSDKVersion"=="" (
ECHO Warning: Windows Software Development Kit version not specified.
) else (
Set "WindowsIncludeRoot=%WindowsSDKDir%Include\%WindowsSDKVersion%\"
Set "WindowsLibRoot=%WindowsSDKDir%Lib\%WindowsSDKVersion%\"
)
)
IF "%VSInstallDir%"=="" (
CALL :GetVSInstallDir
)
IF "%VSInstallDir%"=="" (
ECHO Warning: %DTARGETVS% is not found on this machine.
)
IF "%VCInstallDir%"=="" (
if "%VCToolsVersion%"=="" (
CALL :GetVCToolsVersion
)
CALL :GetVCInstallDir
)
IF "%VCInstallDir%"=="" (
ECHO Warning: %DTARGETVS% VC is not found on this machine.
)
IF "%FrameworkDir%"=="" (
CALL :GetFrameworkDir
)
IF "%FrameworkDir%"=="" (
ECHO Warning: .Net Framework is not found on this machine.
)
IF "%FrameworkVersion%"=="" (
CALL :GetFrameworkVer
)
IF "%FrameworkVersion%"=="" (
IF EXIST "%FrameworkDir%v4.0.30319" (
SET "FrameworkVersion=v4.0.30319"
)
)
IF "%MSBuildInstallDir15%"=="" (
CALL :GetMSBuildInstallDir15
)
REM ==========================================================================
REM Set path
REM ==========================================================================
IF EXIST "%VSInstallDir%Team Tools\Performance Tools" (
SET "PATH=%VSInstallDir%Team Tools\Performance Tools\x64;%VSInstallDir%Team Tools\Performance Tools;%PATH%"
)
IF EXIST "%VSInstallDir%" (
SET "PATH=%VSInstallDir%Common7\Tools;%VSInstallDir%Common7\IDE;%PATH%"
)
IF EXIST "%VCInstallDir%" (
SET "PATH=%VSInstallDir%Common7\IDE\VC\vcpackages\;%PATH%"
)
IF EXIST "%FrameworkDir%" (
SET "PATH=%FrameworkDir%%FrameworkVersion%;%PATH%"
)
SET "PATH=%GamingSDK%bin;%PATH%"
IF NOT "%GRDKEDITION%"=="000000" (
SET "PATH=%GamingGRDKBuild%bin;%PATH%"
)
IF EXIST "%PIXPath%" (
SET "PATH=%PIXPath%;%PATH%"
)
REM Normal compiler for VS2017/GXDL.
SET "PATH=%VCInstallDir%bin\Hostx64\x64;%PATH%"
IF EXIST "%WindowsSDKDir%bin\%WindowsSDKVersion\%x64" (
SET "PATH=%WindowsSDKDir%bin\%WindowsSDKVersion%\x64;%PATH%"
)
REM ==========================================================================
REM Set Include
REM ==========================================================================
IF EXIST "%VCInstallDir%" (
SET "INCLUDE=%VCInstallDir%INCLUDE;%INCLUDE%"
)
SET "INCLUDE=%GamingGRDKBuild%gamekit\include;%INCLUDE%"
IF EXIST "%WindowsIncludeRoot%" (
SET "INCLUDE=%WindowsIncludeRoot%um;%WindowsIncludeRoot%shared;%WindowsIncludeRoot%winrt;%WindowsIncludeRoot%cppwinrt;%WindowsIncludeRoot%ucrt;%INCLUDE%"
)
REM ==========================================================================
REM Set Lib
REM ==========================================================================
SET "LIB=%GamingGRDKBuild%gamekit\lib\amd64;%VCInstallDir%\lib\x64;%LIB%"
IF EXIST "%WindowsLibRoot%" (
SET "LIB=%WindowsLibRoot%um\x64;%WindowsLibRoot%ucrt\x64;%LIB%"
)
REM ==========================================================================
REM Set LibPath
REM ==========================================================================
IF EXIST "%VCInstallDir%" (
SET "LIBPATH=%VCInstallDir%LIB\x64;%LIBPATH%"
)
IF EXIST "%FrameworkDir%" (
SET "LIBPATH=%FrameworkDir%%FrameworkVersion%;%LIBPATH%"
)
SET "LIBPATH=%GamingDesktopExtensionSdkBuild%references\CommonConfiguration\Neutral;%LIBPATH%"
SET Platform=Gaming.Desktop.x64
REM ==========================================================================
REM Set the MSBuild path after everything else so that it appears first in the
REM path. VS2017 must not accidentally target msbuild.exe in the .NET
REM Framework since it stopped shipping there in dev12 and later.
REM ==========================================================================
SET "PATH=%MSBuildInstallDir15%;%PATH%"
SET CommandPromptType=Native
SET VisualStudioVersion=%DTARGETVSVER%
CD /D "%GamingSDK%bin"
GOTO :EOF
:GetVSInstallDir
set VSWHERELOCATION="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
if not exist %VSWHERELOCATION% (
Set LegacyVSLocation="true"
)
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\GSDK\%GRDKEDITION%\GRDK" /v "VSIXEditionID" /reg:32 2^>NUL') DO SET GamingGXDKVSIXID=%%c
IF defined LegacyVSLocation (
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\VisualStudio\SxS\VS7" /v "%DTARGETVSVER%" /reg:32 2^>NUL') DO SET VSInstallDir=%%c
) ELSE (
FOR /f "usebackq tokens=1* delims=: " %%i in (`%VSWHERELOCATION% -latest -requires %GamingGXDKVSIXID% %GRDKVSVERSION% %GRDKVSPRODUCTS% %GRDKVSPRERELEASE%`) do (
IF /i "%%i"=="installationPath" set VSInstallDir=%%j
)
)
IF defined VSInstallDir IF not "!VSInstallDir:~-1!"=="\" set VSInstallDir=%VSInstallDir%\
GOTO :EOF
:GetVCToolsVersion
set VCDefaultConfigFile="%VSInstallDir%VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"
if not exist %VCDefaultConfigFile% (
ECHO Warning: Could not find default VC++ tools config file %VCDefaultConfigFile%.
GOTO :EOF
)
for /F %%A in ('type %VCDefaultConfigFile%') do set VCToolsVersion=%%A
if "%VCToolsVersion%"=="" (
ECHO Warning: Could not determine default VC++ tools version.
)
GOTO :EOF
:GetVCInstallDir
if "%VCToolsVersion%" NEQ "" (
SET "VCInstallDir=%VSInstallDir%VC\Tools\MSVC\%VCToolsVersion%\"
)ELSE (
ECHO Warning: Could not set VC Install Directory.
)
GOTO :EOF
:GetLatestGRDK
REM Sets GRDKEDITION to latest GRDK's edition number.
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\GSDK" /v "GRDKLatest" /reg:32 2^>NUL') DO SET GRDKEDITION=%%c
GOTO :EOF
:GetGRDKBuildInstallPath
REM Sets GamingGRDKBuild to location of Gaming GRDK build tools.
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\GSDK\%GRDKEDITION%\GRDK" /v "InstallPath" /reg:32 2^>NUL') DO SET GamingGRDKBuild=%%c
GOTO :EOF
:GetGamingDesktopExtensionSdkPath
REM Sets GamingDesktopExtensionSdkBuild to the location of the current GXDK edition's extension SDK root.
FOR /f "tokens=1,2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Gaming.Desktop.x64.%GRDKEDITION%\v10.0" /v "InstallationFolder" /reg:32 2^>NUL') DO SET GamingDesktopExtensionSdkBuild=%%c
GOTO :EOF
:GetGRDKEditionVersionFriendlyName
REM Sets GamingGRDKEditionVersionFriendlyName to the friendly version name of the selected build tools.
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\GSDK\%GRDKEDITION%\GRDK" /v "EditionVersionFriendlyName" /reg:32 2^>NUL') DO SET GamingGRDKEditionVersionFriendlyName=%%c
GOTO :EOF
:GetGSDKInstallPath
REM Sets GamingXDK to location of GXDK
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\GSDK" /v "GRDKInstallPath" /reg:32 2^>NUL') DO SET GamingSDK=%%c
GOTO :EOF
:GetMSBuildInstallDir15
REM Sets MSBuild15 directory.
IF /I "%DPLATFORM%"=="VS2017" (
SET "MSBuildInstallDir15=%VSInstallDir%MSBuild\15.0\Bin\;%PATH%"
)
GOTO :EOF
:GetFrameworkDir
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\.NETFramework" /v "InstallRoot" /reg:32 2^>NUL') DO SET FrameworkDir=%%c
GOTO :EOF
:GetFrameworkVer
FOR /f "tokens=1,2*" %%a in ('reg query "HKLM\Software\Microsoft\VisualStudio\SxS\VC7" /v "FrameworkVer32" /reg:32 2^>NUL') DO SET FrameworkVersion=%%c
GOTO :EOF
:SetVS2017
SET DPLATFORM=VS2017
SET DTARGETVS=Visual Studio 2017
SET DTARGETVSVER=15.0
GOTO :EOF
:GetWindowsSDKDir
If exist "%GamingSDK%\%GRDKEDITION%\WindowsSDK" (
Set "WindowsSDKDir=%GamingSDK%\%GRDKEDITION%\WindowsSDK\"
) else (
@REM Get Windows 10 SDK installed folder
for /F "tokens=1,2*" %%i in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0" /v "InstallationFolder"') DO (
if "%%i"=="InstallationFolder" (
SET WindowsSdkDir=%%~k
)
)
)
GOTO :EOF
:GetWindowsSDKVersion
REM From VS 2017 winsdk.bat GetWin10SdkDirHelper
@REM get windows 10 sdk version number
setlocal enableDelayedExpansion
@REM Due to the SDK installer changes beginning with the 10.0.15063.0 (RS2 SDK), there is a chance that the
@REM Windows SDK installed may not have the full set of bits required for all application scenarios.
@REM We check for the existence of a file we know to be included in the "App" and "Desktop" portions
@REM of the Windows SDK, depending on the Developer Command Prompt's -app_platform configuration.
@REM If "windows.h" (UWP) or "winsdkver.h" (Desktop) are not found, the directory will be skipped as
@REM a candidate default value for [WindowsSdkDir].
set __check_file=winsdkver.h
if /I "%VSCMD_ARG_APP_PLAT%"=="UWP" set __check_file=Windows.h
if not "%WindowsSdkDir%"=="" for /f %%i IN ('dir "%WindowsSdkDir%include\" /b /ad-h /on') DO (
@REM Skip if Windows.h|winsdkver (based upon -app_platform configuration) is not found in %%i\um.
if EXIST "%WindowsSdkDir%include\%%i\um\%__check_file%" (
set result=%%i
if "!result:~0,3!"=="10." (
set SDK=!result!
if "!result!"=="%VSCMD_ARG_WINSDK%" set findSDK=1
)
)
)
if "%findSDK%"=="1" set SDK=%VSCMD_ARG_WINSDK%
endlocal & set WindowsSDKVersion=%SDK%
if not "%VSCMD_ARG_WINSDK%"=="" (
@REM if the user specified a version of the SDK and it wasn't found, then use the
@REM user-specified version to set environment variables.
if not "%VSCMD_ARG_WINSDK%"=="%WindowsSDKVersion%" (
if "%VSCMD_DEBUG%" GEQ "1" echo [DEBUG:%~nx0] specified /winsdk=%VSCMD_ARG_WINSDK% was not found or was incomplete
set WindowsSDKVersion=%VSCMD_ARG_WINSDK%
set WindowsSDKNotFound=1
)
) else (
@REM if no full Windows 10 SDKs were found, unset WindowsSDKDir and exit with error.
if "%WindowsSDKVersion%"=="" (
set WindowsSDKNotFound=1
set WindowsSDKDir=
)
)
GOTO :EOF
:end
3 Answers
Executing Commands in a New Shell Instance with Loaded Environment
When running a batch script, sometimes it is necessary to load environment variables before executing commands. However, when the script calls a cmd file to load the environment, it opens a new shell instance, and the subsequent commands are executed in the original shell instance before the environment is loaded. This problem can be solved by executing the commands in the new shell instance with the loaded environment. In this post, we will discuss how to achieve this using a Python script that calls a batch script, which in turn calls a cmd file to load environment variables and run commands with them.
Python Script
Let’s start by creating a Python script that calls the batch script. The Python script will use the subprocess module to execute the batch script and capture its output. Here is an example Python script:
python
import subprocess
output = subprocess.check_output(['path/to/batch/script.bat'])
print(output.decode('utf-8'))
This script calls the batch script located at path/to/batch/script.bat
and captures its output. The check_output
function waits for the batch script to finish executing before returning its output. The decode
function is used to convert the output from bytes to a string.
Batch Script
Next, let’s create the batch script that loads environment variables and runs commands with them. Here is an example batch script:
batch
@echo off
call "C:Program Files (x86)Microsoft GSDKCommand PromptsGamingDesktopVars.cmd" GamingDesktopVS2017
start "" cmd /c "wdapp unregister myAppName"
This script loads environment variables by calling the cmd file located at "C:Program Files (x86)Microsoft GSDKCommand PromptsGamingDesktopVars.cmd"
. The call
command is used to execute the cmd file in the current shell instance, so the environment variables are loaded into the current environment.
The start
command is used to open a new shell instance and execute the subsequent command in it. The /c
option is used to execute the command and then exit the new shell instance. The ""
argument is used to specify an empty window title.
Cmd File
Finally, let’s create the cmd file that sets path variables. Here is an example cmd file:
cmd
@echo off
set GRDKEDITION=
if /i "%1"=="GamingDesktopVS2017" (
call :SetVS2017
) else (
echo You must specify GamingDesktopVS2017 on the command line.
exit /b 1
)
if "%GRDKEDITION%"=="" (
if not "%2"=="" (
set GRDKEDITION=%2
) else (
call :GetLatestGRDK
)
)
if %DTARGETVSVER% GEQ 15.0 (
set GRDKVSPRERELEASE=-prerelease
if "%GRDKVSPRODUCTS%"=="" (
if not "%3"=="" (
set GRDKVSPRODUCTS= -products %3
)
) else (
set GRDKVSPRODUCTS= -products %GRDKVSPRODUCTS%
)
if "%GRDKVSVERSION%"=="" (
if not "%4"=="" (
if not "%4"=="NOPRE" (
set GRDKVSVERSION= -version %4
)
)
) else (
set GRDKVSVERSION= -version %GRDKVSVERSION%
)
)
:SetVS2017
set DEVKIT_PATH=C:Program Files (x86)Microsoft Xbox One XDK
set PATH=%DEVKIT_PATH%bin;%DEVKIT_PATH%binwin10;%PATH%
set INCLUDE=%DEVKIT_PATH%vcinclude;%INCLUDE%
set LIB=%DEVKIT_PATH%vclibwin10umx64;%LIB%
set LIBPATH=%DEVKIT_PATH%vclibwin10umx64;%LIBPATH%
set WindowsSdkDir=C:Program Files (x86)Windows Kits10
set WindowsSDKLibVersion=10.0.17763.0
set UCRTVersion=10.0.17763.0
set UniversalCRTSdkDir=C:Program Files (x86)Windows Kits10
set VCIDEInstallDir=C:Program Files (x86)Microsoft Visual Studio2017CommunityVC
set VCToolsInstallDir=C:Program Files (x86)Microsoft Visual Studio2017CommunityVCToolsMSVC14.16.27023
set VS150COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7Tools
set VSINSTALLDIR=C:Program Files (x86)Microsoft Visual Studio2017Community
set VSSDK140Install=C:Program Files (x86)Microsoft Visual Studio2017Community
set VSSDK140InstallRoot=C:Program Files (x86)Microsoft Visual Studio2017Community
exit /b 0
:GetLatestGRDK
echo Getting the most recent GXDK edition installed...
for /f "tokens=2,3" %%a in ('reg query "HKLMSOFTWAREMicrosoftXboxLiveGdkVersions" /v "LatestGDKEdition" ^| findstr "REG_SZ"') do (
set GRDKEDITION=%%b
)
if "%GRDKEDITION%"=="" (
echo Failed to find the most recent GXDK edition installed.
exit /b 1
)
exit /b 0
This cmd file sets path variables based on the specified edition of the Gaming Desktop Kit (GDK). The if
statement checks if the first argument is "GamingDesktopVS2017"
, which is the name of the edition we want to use. If it is, the :SetVS2017
label is called to set the appropriate path variables. If it isn’t, an error message is displayed, and the script exits with an error code of 1.
The :SetVS2017
label sets the path variables for the specified edition of the GDK. The set
command is used to set each variable. The exit /b 0
command is used to exit the script with a success code of 0.
The :GetLatestGRDK
label is called if the GRDKEDITION
variable is not set. This label gets the most recent edition of the GDK installed on the system by querying the registry. The for
loop is used to parse the output of the reg query
command and set the GRDKEDITION
variable to the second token of the second line of output. If the variable is not set, an error message is displayed, and the script exits with an error code of 1.
Conclusion
In this post, we have discussed how to execute commands in a new shell instance with loaded environment variables. We achieved this using a Python script that calls a batch script, which in turn calls a cmd file to load environment variables and run commands with them. We hope this post has been informative and helpful.
To execute the second line of the batch script in the new shell instance that is opened by the first line, you can use the start
command in the batch script.
Change the first line of the batch script to the following:
start cmd /k "C:\Program Files (x86)\Microsoft GSDK\Command Prompts\GamingDesktopVars.cmd" GamingDesktopVS2017
This will open a new instance of the command prompt, run the GamingDesktopVars.cmd
file to set the environment variables, and then return control to the batch script.
Then, add the start
command before the second line of the batch script to execute the wdapp unregister
command in the new shell instance:
start wdapp unregister myAppName
This will execute the wdapp unregister
command in the new shell instance where the environment variables have been set.
The updated batch script should look like this:
start cmd /k "C:\Program Files (x86)\Microsoft GSDK\Command Prompts\GamingDesktopVars.cmd" GamingDesktopVS2017
start wdapp unregister myAppName
Here are a few additional thoughts:
- If you want the batch script to wait for the new shell instance to finish executing before continuing, you can use the
start /wait
command instead of juststart
. - You can also use the
call
command to execute theGamingDesktopVars.cmd
file and return control to the batch script, rather than opening a new shell instance. However, this will not allow you to execute thewdapp unregister
command in the new shell instance where the environment variables have been set. - If you want to pass arguments to the
GamingDesktopVars.cmd
file, you can do so by adding them after the file name in thestart
command, like this:start cmd /k "C:\Program Files (x86)\Microsoft GSDK\Command Prompts\GamingDesktopVars.cmd" GamingDesktopVS2017 argument1 argument2
. - Make sure to use the full path to the
GamingDesktopVars.cmd
file in thestart
command.
I hope this helps! Let me know if you have any other questions.
If you edit your bat (GamingDesktopVS2017.cmd
), and add:
@echo off && cd /d "c:\folder\to\where\the\bat\are\"
SET "DPLATFORM=VS2017
SET "DTARGETVS=Visual Studio 2017"
SET "DTARGETVSVER=15.0"
And run/call this:
echo=exit|cmd /v /k GamingDesktopVS2017.cmd wdapp unregister myAppName
Why you are using cmd /k
?
About “C:\Program Files (x86)\Microsoft GSDK\Command Prompts\GamingDesktopVars.cmd” does:
rem :: the bat only check if your are passing this argument :GamingDesktopVS2017
rem :: if positive, then set same variable that you can add in your bat :
IF /I "%1"=="GamingDesktopVS2017" (
CALL :SetVS2017
=> call label :SetVS2017
)ELSE (
ECHO You must specify GamingDesktopVS2017 on the command line.
EXIT /B 1
)
and the label do this:
:SetVS2017 SET DPLATFORM=VS2017
SET DTARGETVS=Visual Studio 2017
SET DTARGETVSVER=15.0
GOTO :EOF
Just move this settings above to your bat!