1

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
  • Add a pause into your batch file. Jul 28, 2020 at 14:59
  • 2
    that doesn't let me continue using the command-window, it just stops it closing until I hit a key @spikey_richie I've clarified the question a bit
    – Mr. Boy
    Jul 28, 2020 at 15:28

3 Answers 3

2

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
  • could the path still be set as the "start location" of the shortcut or would that mess up? BTW the .exe is in the path I want the command prompt to be open in, if that makes anything simpler
    – Mr. Boy
    Jul 28, 2020 at 15:41
  • Yes, the path is the start location the program and you can place the shortcut or batch file anywhere Jul 28, 2020 at 15:55
  • I tried this and it opens the command prompt but nothing else appears to happen, nothing is printed in the command window
    – Mr. Boy
    Jul 28, 2020 at 22:03
1

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.

1

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.