How I automated some CMD Commands with Bash
What was the problem?
I recently started using Django on Windows and one of the things that quickly stood out for me was having to move through the same folders to get to where my project code sits. Such a routine task!
Then, having to activate my virtual environment every time I need to work on the current project. Thankfully, using the virtual environments is made easy by virtualenvwrapper-win for Windows. But, I still needed to start the virtual environment anytime the PC comes on.
I will show you how I used a batch file to automate these Command Prompt actions.
How did I fix the Problem?
It felt like moving through the directories and activating the virtual environment was more burdensome than the actual coding. So, I had to find out how to combine all the commands for changing directories and starting the virtual environments so I only have to use a single command or take an action.
That’s where Windows batch files come in!
Batch files are script files that hold commands and execute them serially without your input. I’ve used them before but here I was, having to start enjoying them again.
Steps to Fixing the Problem
I created a file in a text editor and saved it as kickstart.bat. `bat` is the batch file extension.
Then, I added the following command to it.
cd "C:\Users\User\Folder\project"
The cd
command above is for changing the directories to the level where I worked. I went to the project directory with File Explorer and copied the address from the address bar.
Next, I added the command for starting the virtual environment.
call workon env
In the above code:
- The
workon
is the virtualenvwrapper-win command for starting virtual environments andenv
is the name of the virtual environment that I created for the project. - I prepended the environment activation command with the
call
command to enable thebat
file access an external utility that is not native to the Windows command prompt.
But, we are not finished yet. Running the batch file on the CMD this way would run and close the CMD application after running. Why run batch operation if the CMD window won’t be available for me to do my Django command-line operations?
Consequently, I added the next command to the .bat
file.
cmd /k
The above command keeps the command prompt open after executing the batch file so you can have access to the Command Prompt window.
The full batch file is the following code:
cd "C:\Users\User\Folder\project"
call workon env
cmd /k
Henceforth, anytime I need to work, I could just go and double-click the kickstart.bat
file where it is located and I will have my project directory open in a Command Prompt window with the virtual environment activated.
Using the Batch file globally
Come to think of it, why do I have to go about finding the kickstart.bat
file when I need it? That is why I added the address of the folder containing the batch file to the Windows PATH
in the System Environment Variables. This allows me to use the batch file globally like a CLI tool by typing and running only the name of the batch file in Command Prompt:
kickstart
And the Command Prompt obeys and gets ready for my Django operations within a split second:
(env) "C:\Users\User\Folder\project>
Goal achieved! I can then keep using the Command Prompt window
In conclusion
Since that day, I’ve only had to open the Command Prompt and run the kickstart
command to change directories to the project folder and activate the virtual environment, all at once.
Hopefully, you learned a thing that could make your Windows usage easier. You can share with me in the comment section other ways you’ve tried automating routine or boring tasks before. I’ve also decided to start documenting my automation thrills across my daily development and deployment workflow. You can surely expect more.
Finally, if you enjoyed this article, you can give it some claps 👏👏👏 and say `hi` on Twitter @JKYNLW.
Cheers!