Reputation: 6147
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
Upvotes: 1
Views: 3554
Reputation: 49086
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:
exit /B
not found in the Microsoft documentation.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.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./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.Upvotes: 3
Reputation: 79983
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.
Upvotes: 0
Reputation: 2609
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!
Upvotes: 0