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.