Shortcut that opens a command prompt and runs an application

I know how to make a shortcut to cmd.exe and set the start-location to my target directory. But I would like the shortcut to run an application in the console window... basically my application run with no commands will print a welcome message, instructing the user how to launch it. he user then interacts with this command-prompt as normal.

So I want a shortcut that will:

  • open the command prompt ina given folder
  • run a specified program in that folder
  • keep the command prompt open afterwards for me to interact with

Is this possible?

2

3 Answers

You can use this:

cmd.exe /k "cd /d "Filepath" & start program.exe"
  • /k switch tells to keep command prompt open after command execution.
  • cd changes directory to the file path, /d switch makes it safer because without this switch over-drive paths cannot be changed.
  • Then start the program.

You can create a shortcut. Right click on desktop > New > Shortcut. Then put the code there and give it a name. You can also put it in a batch file.

By the way

Inspired by @spikey_richie idea:

@echo off & cd /d "Filepath" & start program.exe & pause >nul

If you don't want to keep command prompt open, but wait termination or a key press.

3

I think what you're asking for is something like this in a .bat batch file

Create a new file called shortcut.bat. In the file create the following:

cd c:\path to file\

Start program.exe

pause

When you run shortcut.bat it'll fire up a command window, start your program and leave the command window open.

Assuming your executable is a console application, you don't need start:

%comspec% /k "cd /d "c:\Python27" && python.exe"
rem :: or..
%comspec% /k "pushd "c:\Python27" && python.exe"
rem :: or, with arguments
%comspec% /k "pushd "c:\Python27" && "python.exe" args[0] args[1] ... args[n]"

enter image description here


1. Start cmd.exe interpreter using one system variable:

%comspec% ...
rem :: is the same command ::
C:\Windows\system32\cmd.exe ...

2. When invoking the command interpreter, instruct it to remain by /keep with the cmd.exe opened, without "auto quit/exit" when finalizing the "command passed".

%comspec% /k command...
rem :: is the same command ::
C:\Windows\system32\cmd.exe /k command...

3. Use the target directory as a working directory with check directory command cd directory.

... "cd /d "c:\Python27"

4. By default, the command-line interpreter (cmd.exe) will assuming the %windir%\system32\ as a working directory, by adding cd or pushd, you can define your working directory as you wish/need

cd /D "c:\Python27"...

5. Use the /D switch in your cd traget_folder to ensure that it also changes the drive when invoked.

...cd /D "c:\Python27" .... 

6. Use " quotation marks " when passing the path to your folder to the cd command, to potentially prevent any error event related to any folder that contains any special character or even spaces in its name.

...cd /D "c:\Python27" .... 

7. Use the && operator, this will respond for any correction that may be necessary in case the path is wrong, has been changed, etc..., and also avoid the execution of the subsequent command where it would also return in error, thus, it will only return an error and proceed without execution, facilitating any future correction of the path if necessary or changed

...cd /D "c:\Python27" && python.exe"

8. If the previous execution was successful (without error, the same as return 0). The operator will proceed with the next command, starting python.exe, and since this is a console application, the interface will remain in the same window, and using the directory assumed as work directory for the folder previously signaled until this execution is finished by the user and if this happens, python interface console ends and the interpreter cmd.exe remains open (/keep) and in the same work folder.

cmd /K Carries out the command specified by string but remains%comspec% /K "cd /D "c:\Python27" && "python.exe"" 

9. As we are invoking the command interpreter and passing multiple commands, it is recommended to inform/define that this will be restricted to a block, and we do this by delimiting/defining between double-quotes, since we are using a single line:

interpreter "command operator command"%comspec% /K "cd /D "c:\Python27" &&  "python.exe"" 

10. Assuming you want/need to make use of this shortcut by pointing a UNC folder path in your command, you will need to use some compatible way to enter and remain in your pointed drive/folder, in this case, use pushd, but also works with regular drive/path.

%comspec% /K "pushd "c:\Python27" ..."
%comspec% /K "pushd "\\Installler\Python27" ..."

enter image description here

Obs.: If you use pushd, use also popd command to return a folder previously assumed to be the work directory at the time you want and already removing the drive temporarily created in the initial pushd execution.

enter image description here


By the way

Inspired by @Wasif_Hasan idea:

That was inspired by @spikey_richie idea:


Some further reading:

0

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

You Might Also Like