1

I'm trying to set some environment variables using 'SET' so they are set locally and not on the system level, which happens when using SETX. However the variables are not appearing to be passed into the final command being executed (.exe) How can i set local env variables and pass the modified env into the exe?

@echo off
setlocal 

:: Assign all Path variables
SET STARTUP="%~dp0startup"

set ADSK_3DSMAX_STARTUPSCRIPTS_ADDON_DIR=%STARTUP%
echo ADSK_3DSMAX_STARTUPSCRIPTS_ADDON_DIR %STARTUP%

start /d "%PROGRAMW6432%\Autodesk\3ds Max 2022\" 3dsmax.exe /i
endlocal
exit
aschipfl
  • 33,626
  • 12
  • 54
  • 99
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

3 Answers3

2

The following batch file works in any use case:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Assign all directory path variables
set "STARTUP=%~dp0startup"
set "ADSK_3DSMAX_STARTUPSCRIPTS_ADDON_DIR=%STARTUP%"

if not exist "%ProgramFiles%\Autodesk\3ds Max 2022\3dsmax.exe" goto Progx86
start "" /D "%ProgramFiles%\Autodesk\3ds Max 2022" 3dsmax.exe /i
exit /B

:Progx86
if not exist "%ProgramFiles(x86)%\Autodesk\3ds Max 2022\3dsmax.exe" goto DisplayError
start "" /D "%ProgramFiles(x86)%\Autodesk\3ds Max 2022" 3dsmax.exe /i
exit /B

:DisplayError
echo ERROR: Failed to find: "Autodesk\3ds Max 2022\3dsmax.exe"
echo(
pause
endlocal

The reason for the second line with using setlocal with the options EnableExtensions and DisableDelayedExpansion is explained by chapter Issue 6: Batch file depends on environment defined outside of this answer.

The reason for the used syntax to define the environment variables is described in full details by my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?

The combination of these changes results in a working batch file, even on directory containing the batch file is for example C:\Temp\Development & Test 100% (!) on which most batch files using %~dp0 fail.

There should be in nearly all cases not used the command exit at end of a batch file. There are some rare cases where exit at bottom of a batch file is really useful, but in most cases it is useless or even counterproductive, for example on debugging a batch file which means running it from within a command prompt window, or when the batch file is in future called by another batch file.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • start /?

See also:

  • Microsoft´s documentation of the Windows commands
  • Where does GOTO :EOF return to?
    It contains information about exit /B not found in the Microsoft documentation.
  • DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
  • Microsoft´s documentation of the Windows kernel library function CreateProcess and the STARTUPINFO structure which are used by cmd.exe on starting an executable without or with usage of its internal command start. The usage of command start changes some parameters passed to the function and values in the structure.
    The explanation of the CreateProcess function parameter lpEnvironment is most interesting in this case. This function parameter is always a null pointer because of cmd.exe does not support the definition of environment variables specific for an executable to start.
    The option /D of command start defines the function parameter lpCurrentDirectory, i.e. no null pointer as by default, but a pointer to the explicitly specified directory path string.
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

The solution could be at first setting the environment variables in the batch file using set and then running the executable directly (without start). If it works, tell me!

Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33
0

Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces.

If you use your syntax, you execute

set ADSK_3DSMAX_STARTUPSCRIPTS_ADDON_DIR=""%~dp0startup""

which is probably not what you expect.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • i can change it to this and it will be fine then right `set ADSK_3DSMAX_STARTUPSCRIPTS_ADDON_DIR=%STARTUP%` – JokerMartini Apr 08 '22 at 16:41
  • 1
    Use `set "var1=data"` for setting *ALL* string values - this avoids problems caused by trailing spaces which are extremely hard to debug. If you omit the quotes, it will *probably* work, but I'd not recommend that approach. If you include quotes in the values set, it becomes hard to control the quote-nesting and you land up having to dequote the variable to use it. If you follow my recommendation, then you can concatenate and substring variables more easily, inserting quotes as required. – Magoo Apr 08 '22 at 16:51