Activating a Python virtual environment & changing directory in one shortcut (with cmd) - windows

I'm trying to do the following:
Open a CMD prompt
Activate a virtual environment
Change the current directory to my project folder
In essence, I need to execute the following commands sequentially:
C:\Envs\djangorocks\Scripts\activate
cd "D:\GitHub\steelrumors"
I've found this link, but creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
After quite a while of searching I'm still doing it manually, any help is appreciated.

"creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):"
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
Observations:
cmd \k should be cmd /k.
& should be && when using a shortcut.
You dont need all the " characters.
Try the following as the shortcut target:
cmd /k C:\Envs\djangorocks\Scripts\activate && cd D:\GitHub\steelrumors

Consider creating a batch file (e.g. c:\scripts\launchEnv.cmd) that does something like the following:
#echo off
C:\Envs\djangorocks\Scripts\activate
cd /d "D:\GitHub\steelrumors"
Then create a shortcut that invokes cmd /k c:\scripts\launchEnv.cmd .
Some notes:
the #echo off will prevent the commands from showing up in the cmd windows. If you do want to see the commands, then omit that line from your batch file
you'll need the /d param when changing directories to make sure you actually change and navigate there, independent of where the script is currently executing from.

As an extension to the great answer from #DavidPostill I've added an additional step to run a command from the newly created python env.
In my example below, I'm launching a new instance of the awesome data mining program, orange, from an anaconda env called orange. I've also cd'ed to the directory containing my orange data files. Note that I had to use the quotation marks "" to make it work.
C:\Windows\System32\cmd.exe /k "F: && cd \Dropbox\IT\Python\Orange && C:\Users\dreme\Anaconda3\Scripts\activate.bat orange && python -m Orange.canvas"

Related

windows bat file using start cmd /k with doskey not work

I tried to use start cmd with custom doskey file, but failed.
1.doskey
ls=dir /b $1
start "title" cmd.exe /k doskey /macrofile=1.doskey && ls .
Prompted
ls command is not recognized
Two issues:
Issue 1
The && will be interpreted by the shell you run this command from and not the shell in the window, so you are running doskey /macrofile=1.doskey in the new window but ls . in the old one.
The solution is to surround the whole command that should be passed to the new window with doublequotes:
start "title" cmd.exe /k "doskey /macrofile=1.doskey && ls ."
Alternatively you could escape both & characters with ^:
start "title" cmd.exe /k doskey /macrofile=1.doskey ^&^& ls .
...however, this will just uncover issue 2, so keep reading.
Issue 2
As you can deduce (sort of) from the DOSKEY docs, you cannot run a macro from a command that you didn't type in manually (after all, DOSKEY is a tool to process interactive input from a user):
To run a macro, type the macro name at the command prompt, starting at the first position. If the macro was defined with $* or any of the batch parameters $1 through $9, use a space to separate the parameters. You cannot run a doskey macro from a batch program.
(Emphasis mine.)
This talks only about batch files, but it applies to any non-interactive way of entering a command - and passing it to cmd /c or cmd /k is one of them (since you aren't typing the command into the new shell, it's run automatically on startup of the shell instead).
Based on how you described your intent ("use start with custom doskey file"), I assume you added the ls . only to test whether it works. In that case, just remove the whole && ls . part and try ls . manually in the new window, and you'll see that it does work!
In case that's not what you wanted, and you actually intended to run DOSKEY macros from that "cmd /k" command line or a batch file, you are out of luck - instead, you could call a batch file that contains functions and use that, or create custom batch files instead of macros.

use root cmd window to execute commands in new cmd window

i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file: is executed in the new window(I got the new window by using the start keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start
I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "#set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd process with the start command. We use /V:ON to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%) will be expanded on the fly with ! (so !file! instead of %file%). We use /K to tell to this cmd process to not close when provided commands end. To this cmd process, we provide the following commands :
#set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The # means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar parameter, filename build from the previous prompt and the .jar extension. The ^ here escapes the ., it seems not useful here but maybe your script/env requires it.
We link the both commands with && which means : _if left command from && is successful (it exits with ERRORLEVEL 0) then execute the right command from &&.

Running File from Notepad Plus Plus and Current Directory

There are a number of examples on the web of how to run a file from the Notepad Plus Plus (NPP). But they all fail to account for the fact that the current working directory is the location of the NPP's executable, and not the location of the file.
Usually they go something like this:
cmd /K "$(FULL_CURRENT_PATH)"
Consider the following Python script:
with open('somefile.txt', 'a') as file:
file.write('Hello there.\n')
This file will be created in the NPP folder, which is not at all what most people would expect. Most people would want it in the same location as the Python file.
You could also do something like this, and it works as expected, but this limits you to Python files only:
<Command name="Run This Python File" Ctrl="no" Alt="no" Shift="yes" Key="116">cmd /K python "$(FULL_CURRENT_PATH)"</Command>
I would not want to add extra code to the Python script to change the current working directory, as normally this would not be needed.
I have been trying to solve this and came up with the following. This line goes in "shortcuts.xml" in the NPP folder.
<Command name="Run This File" Ctrl="yes" Alt="no" Shift="no" Key="116">cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""</Command>
So you shut down the NPP, edit the "shortcuts.xml" by adding this line, using another editor, then launch the NPP.
To run the file, use Ctrl+F5 key combination.
This works in Windows 10, but fails in Windows XP.
How can I tweak it to work in Windows XP?
Try this:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
My guess would be that the problem is the improperly nested quotes in the command; I'm not sure exactly why it would work on later Windows' while failing on XP.
The command
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
represents
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
Even from the syntax highlighting you can see that the quotes are not quoting what you expect.
To get the desired effect, you can use this command:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
:: XML-ified:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
I run several Windows .bat files from Notepad++. To achieve this one of the entries in the <UserDefinedCommands> section of the file C:\Users\AdrianHHH\AppData\Roaming\Notepad++\shortcuts.xml is:
<Command name="CD and run file" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C "cd /d $(CURRENT_DIRECTORY) && $(FULL_CURRENT_PATH)"</Command>
With the .bat as the current file I then use menu => Run => CD and run file.
The command line shown in the question appears to have too many " symbols. The current directory includes the drive specifier and so the CD needs the \D option.
The command I use starts cmd \C ... (rather than the \K in the question) so the command window closes automatically. My .bat files normally finish with choice /t 60 /C Y /d Y /n so I can see the command's output.
Notepad++ > F5 (Run) > then type following command
cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)"
assuming you have setup the path, or you may use C:\Python27\python.exe or the path of your python binary, and you will run the python at the folder where the python resides in. You can also save this command to shortcut by clicking button Save....
Afterwards, you also can modify the command in toolbar > Run > Modify shortcut/delete command.

How do I launch a Git Bash window with particular working directory using a script?

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?
My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.
I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.
Another option is to create a shortcut with the following properties:
Target should be:
"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
Start in is the folder you wish your Git Bash prompt to launch into.
Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:
"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"
If used inside registry key, folder parameter can be provided with %1:
"C:\Program Files\Git\git-bash.exe" --cd="%1"
Git Bash uses cmd.exe for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exe wrapper. In Windows you launch a new terminal using the start command.
Thus a shell script which launches a new Git Bash terminal with a specific working directory is:
(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &
An equivalent Windows batch script is:
C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).
Let yet add up to the answer from #Drew Noakes:
Target:
"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo
The cd param should be one of the options how to specify the working directory.
Also notice, that I have not any --login param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).
Start in:
C:\GitRepo
The same possible way, as #Drew Noakes mentioned/shown here sooner, I use it too.
Shortcut key:
Ctrl + Alt + B
Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.
If you do not want it visible, yet still activatable, place this .lnk file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355
"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.
Windows 10
This is basically #lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.
cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:
Create a new key called Bash in the shell key at HKEY_CLASSES_ROOT\Directory\Background\shell
Add a string value to Icon (not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
Edit the default value of Bash to the text you want to use in the context menu
Add a sub-key to Bash called command
Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC is not a real directory.
This is the command which can be executed directly in Run dialog box (shortcut is win+R) and also works well saved as a .bat script:
cmd /c (start /d "/path/to/dir" bash --login) && exit
I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshell and your favorite terminal program is called `myterm' you can script the following:
(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...
Note that the parameter -e for execution may be named differently with your favorite terminal program.
Using Windows Explorer, navigate to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.
Along the same lines, if you have the git directory in your path, you can type "git-bash" in the address bar and a Git Shell will open in that directory.
If using Windows OS :
Right click on git terminal > Properties
Properties>Under shortcut tab>Start in:
add your folder target path like below image

How can I make a shortcut start in a different directory when running it as an administrator on Windows Vista?

I have a shortcut on my desktop which opens a command prompt with many arguments that I need. I set the 'start in' field to d:\ and it works as expected (the prompt starts in d:). When I choose Advanced -> run as administrator and then open the shortcut, it starts in C:\Windows\System32, even though I have not changed the 'start in' field. How can I get it to start in d:\?
If you use the /k argument, you can add a single line to execute a change drive and change directory. For instance:
C:\Windows\System32\cmd.exe /k "d: & cd d:\storage"
Using & you can string together many commands on one line.
Edit: You can also change drive with the cd command alone "cd /d d:\storage". Thanks to Adam Mitz for the comment.

Resources