Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to create a link to an executable which must be run in its own folder?

Writer Andrew Henderson

I have a program inside a folder like below. The objective is to have a link in the utilities folder so I can call it from any command line.

C:\Utilities\Program\ program.exe some.important.dll some.important.ini

Is it possible to create a link, symbolic link or hard link in the Utilities folder that can run the file as if its inside the Program folder? When I create the symlink, the program does not locate its dll/ini files. When I create a .lnk, I can't run it from command line by just typing "program", e.g. in PowerShell, cmd or Run.

C:\Utilities\ program <--------- should be the link

I need the link because my environment PATH will include only the Utilities folder, so I don't want to append a new folder to the PATH every time I add a new Program folder, I would like just to create a new [hard/sym]link.

4

4 Answers

Unfortunately I cant comment to answers. So I have to write a new one.
If you want to use the comand to the program native in the commandline then dont use the command @Start.

@"pathtofile/program.exe" %*

With this all parameters are piped to the original program and all outputs are piped to the calling commandline. With an entry of the utility Directory in the path you can use it in cmd, powershell and run-dialog.
In this answere here, the technique is described, too.

PS: Just use .cmd instead of .bat to differentiate optically the shortcut from an normal batch file. The usage is the same.

EDIT: After testing I realized that the called programm does not recognizes needed files in the same folder (as wanted from the question opener). So a more complex script (all in batch) is needed to change to the programms folder, then execute und than changes dir back.

The only thing you have to change in the script is the progpath variable to the wanted program. Of course you can skip all the REM-commands too (only commenting the code).
Heres the script:

@ECHO OFF
REM enable local variables
SETLOCAL
REM fullpath of program
SET progpath=C:\Dokumente und Einstellungen\Lukas Seidler\Desktop\notepad
REM save current direcory
SET currpath=%CD%
REM change to drive of prog
FOR %%I IN ("%progpath%") DO (%%~dI)
REM change to path of prog
FOR %%I IN ("%progpath%") DO (CD "%%~pI")
REM execute prog with optional parameters
FOR %%I IN ("%progpath%") DO ("%%~nxI" %*)
REM change to calling drive
FOR %%I IN ("%currpath%") DO (%%~dI)
REM change to calling path
FOR %%I IN ("%currpath%") DO (CD "%%~pnI\")
REM disable local variables
ENDLOCAL
REM EOF

For easily creating the file in the utilities dir use another script and place it in the utilities dir (for easy access). I named it makeshortcut.bat. Usage is: makeshortcut "full\path\to\file". Just change the variable utilitypath to your folder.

@ECHO OFF
SETLOCAL
SET utilitypath=D:\Apps\CommonFiles
SET outfile="%utilitypath%\%~n1.cmd"
ECHO @ECHO OFF > %outfile%
ECHO SETLOCAL >> %outfile%
ECHO SET progpath=%~f1 >> %outfile%
ECHO SET currpath=^%%CD^%% >> %outfile%
ECHO FOR ^%%^%%I IN ("^%%progpath^%%") DO (^%%^%%~dI) >> %outfile%
ECHO FOR ^%%^%%I IN ("^%%progpath^%%") DO (CD "^%%^%%~pI") >> %outfile%
ECHO FOR ^%%^%%I IN ("^%%progpath^%%") DO ("^%%^%%~nxI" ^%%*) >> %outfile%
ECHO FOR ^%%^%%I IN ("^%%currpath^%%") DO (^%%^%%~dI) >> %outfile%
ECHO FOR ^%%^%%I IN ("^%%currpath^%%") DO (CD "^%%^%%~pnI\") >> %outfile%
ECHO ENDLOCAL >> %outfile%
ECHO done.

WARNING: No checks of arguments are made. Existing files are overwritten.

If you create a shortcut there, you would then use the "start in" field in the shortcut to indicate the folder it should run in.

Then, just try start program.lnk and see if that works.

3

You could try an MS-Dos batch file, with the same filename (except for the extension) as the program you want to execute.

For example, C:\Utilites\Program.bat could contain something like:

C:\Utilities\Program\program.exe

or

@start "" /D "%~dp0Program\" program.exe %*

Then, when you CD to C:\Utilities, you simply type Program, and the original file you wanted to launch will be launched.

Or, you could program the exe file to be launched from anywhere using the DosKey feature. If you choose to do so, perhaps This article can help you learn to do so.

There's also the option of creating hard/symbolic links to all the other files in the original directory, in addition to the exe file.

You can also copy (or link) the program, and it's files, to your System32 directory. Like DosKey, the latter will allow you to launch the program from anywhere, even in CMD/Run/PowerShell (but not Explorer).

3

This worked for me when I installed 7z program and wanted to use it in command prompt:

  • I installed 7z, then I created a link to the exe file and copied it to windows\system32 folder.
  • I created a 7z.bat file and put one line inside it : 7z.exe.lnk, and copied that file into the windows\system32 folder.

It then works as you wanted. Here is an example (sorry for the Danish windows):

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy