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

10,168

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.

Share:
10,168

Related videos on Youtube

natenho
Author by

natenho

Updated on September 18, 2022

Comments

  • natenho
    natenho almost 2 years

    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.

    • Vlastimil Ovčáčík
      Vlastimil Ovčáčík almost 11 years
      As you said, the problem is the program won't be able to find its config files or dependencies. Probably the best approach here is to use batch. For example C:\Utilities\program.bat with content @start "" /D "%~dp0Program\" program.exe %*.
    • natenho
      natenho almost 11 years
      I'll keep the batch approach for the while, but one of the drawbacks is for example: I can't have the program output in the current terminal (cmd/Powershell). Suppose I want to call program --help to print its usage info - it will only flash a cmd window and will not display the usage.
    • Vlastimil Ovčáčík
      Vlastimil Ovčáčík almost 11 years
      how about @start "" /B /D "%~dp0Program\" program.exe %*?
    • natenho
      natenho almost 11 years
      /B switch helps the output but still batch can't solve seamlessly, you know? I'm still believing that it must have a simple way to get it working.
  • natenho
    natenho almost 11 years
    Thanks, but when I create a .lnk, I can't run it from command line by just typing "program", the idea is to run it just like I run "notepad" or "calc" for instance, easy as that.
  • natenho
    natenho almost 11 years
    Actually I do the batch alternative, but its not seamless as a simple link. And its not very confortable to have a fast cmd window popping up or flashing in taskbar. Regarding DOSKEY, I can't use it to run outside cmd, like in powershell or start->run, can I? You mentioned anywhere, but it seems doskey is a very old exclusive cmd/ms-dos feature.
  • TSJNachos117
    TSJNachos117 almost 11 years
    Sorry, I didn't realize you mentioned using either the Run command, or PowerShell. I thought you were only doing this with the Command Prompt. I've expanded my original answer, providing two more options for you to choose from.
  • natenho
    natenho almost 11 years
    I appreciate your comments, but it seems worst than the batch approach to copy or to link several files - some programs has too many folders/files. I don't need system32 because Utilities is already in PATH environment variable.
  • Jack
    Jack almost 11 years
    How about putting it into a batch file? Would that work?
  • natenho
    natenho almost 11 years
    You can notice the drawbacks of batch files approach in the other comments along the question.