Click here to Skip to main content
969 members (268 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.2K views
Posted 30 Mar 2015

Batch files to your PATH to access two executables with the same name without the full file path

, 30 Mar 2015
Rate this:
Please Sign up or sign in to vote.
This blog describes how to use Batch files to add to your PATH if you have two executables with the same name you'd like to access without having to type the full path.

I use both Python 2.7 and Python 3.3 frequently from the Windows Command Prompt. If I would only use one version, I’d add the folder containing the executable to my PATH environment variable so I did not always have to type out the full path, but only python. However, because there are two folders with a python.exe executable, adding both folders to the PATH wouldn’t be useful: only one of the two executables would be called when executing python, and for the other one, I’d still have to type out the full path.

That’s why I went to look for a solution to be able to call Python 2.7 by executing the command python2, and Python 3.3 by executing the command python3, without renaming the executables to python2.exe and python3.exe — I wanted to keep their original file names. The solution in this post can be applied to any similar situation, I just take Python as example here.

The first step is to create a folder at any location. In this folder, create two new files: python2.bat and python3.bat. These files will contain the commands to call the right Python version.

My python2.bat contains this:

@echo off
C:\Python27\python %*

@echo off makes sure that commands executed later are not displayed on the screen. Their output still is, but the command itself is not. If you remove that line, you’ll see this line printed when executing the file:

C:\your_folder_path>C:\Python27\python

C:\Python27\python %* executes Python 2.7. %* holds the arguments given to the Batch file, and these get passed to Python.

python3.bat looks very similar, only Python27 is replaced with Python33.

Now, we have our Batch files. But we are not ready yet. Now you’d have to use the full file path to these Batch files to execute them, and the goal was to be able to call python2 and python3 without having to enter a full path. To do this, you have to add the folder containing the Batch files to your PATH. Go to Control Panel > System and Security > System and you’ll see an “Advanced System Settings” link. Click on this one, go to the “Advanced” tab, click “Environment Variables”, select “Path”, and add your Batch folder there. Make sure it’s separated from the previous folder by a semicolon.

And now, you can call python2 and python3 without having to write a full file path!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ProgramFOX
Belgium Belgium
I'm a hobbyist programmer. I work mostly in C#, JavaScript, and Python. I also like chess; you can find me on Lichess.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
RA-Web1 | 2.8.171013.1 | Last Updated 30 Mar 2015
Article Copyright 2015 by ProgramFOX
Everything else Copyright © RootAdmin, 1999-2017
Layout: fixed | fluid