1

My goal is to have a python script calling a batch script calling a cmd file to load environment variables and executing some commands with those.

The first line of my batch script is to call a cmd file to load environment variables. The second line is a command using this environment and passing arguments.

My issue is that the first line opens a new shell instance and the second line of the script is executed in the first shell instance where my environment is not yet loaded.

How should I proceed to execute the commands using my environment in the new instance?

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
1
  • This is an environment for using Microsoft Desktop Visual Studio 2017 Gaming Tools, I figured I could maybe append my wdapp unregister command in this cmd file, but I don't know where. I tried at the end with no success. I updated the question with the cmd file
    – jmlesfrite
    Commented Dec 30, 2019 at 12:27

1 Answer 1

0

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!

Sorry my limited English

3
  • @jmlesfrite thank you, so sorry my limited English, good to know that!
    – Io-oI
    Commented Jan 28, 2020 at 22:59
  • @jmlesfrite Please publish the source code
    – Io-oI
    Commented Jan 28, 2020 at 23:13
  • This can be helpfully to someone....
    – Io-oI
    Commented Jan 28, 2020 at 23:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .