Execute a batch file before executing in a shortcut (.lnk)

2.3k views Asked by At

I have multiple versions of a program called Siemens NX. NX uses environmental variables for configuration. I need NX 10.0 to use a different set of environmental variables than my NX 7.5 which uses the system environmental variables. Therefore, I have written a batch file that setups the environmental variables that I need. However, There is a lot of different programs that go with NX 10.0. I don't want to have to create a batch file for each program. Instead, I just want to ammend the shortcuts (.lnk) to execute a batch file before starting. For instance, this is easily done by

C:\Siemens\NX10\UGII\setup_NX10_environment.bat && C:\Siemens\NX10\UGII\ugraf.exe -nx

However, the command window is left open. How can I call the batch script and it closes and then calls my program?

2

There are 2 answers

0
JosefZ On BEST ANSWER

Supply program with parameters to your batch script as follows

C:\Siemens\NX10\UGII\setup_NX10_environment.bat "C:\Siemens\NX10\UGII\ugraf.exe" -nx

and improve that batch as follows:

rem all the original setup_NX10_environment.bat stuff here
%*
exit

or

rem all the original setup_NX10_environment.bat stuff here
call %*
exit

or

rem all the original setup_NX10_environment.bat stuff here
start "" %*
exit
1
Zenadix On

The console window may remain open if you call the executable like this:

executable.exe

However, prepending start to the executable will detach it from the console. Thus the console will not remain open if you call the executable like this:

start executable.exe

In conclusion, rewrite your command as follows:

C:\Siemens\NX10\UGII\setup_NX10_environment.bat && start C:\Siemens\NX10\UGII\ugraf.exe -nx