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](https://cdn.statically.io/img/i.sstatic.net/FMFkm.png)
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](https://cdn.statically.io/img/i.sstatic.net/K7CHp.png)
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](https://cdn.statically.io/img/i.sstatic.net/DBXPy.png)
By the way
Inspired by @Wasif_Hasan idea:
That was inspired by @spikey_richie idea:
Some further reading:
pause
into your batch file.