Tuesday, 18 December 2012

How to run an application from any path in Windows Command prompt

The problem:

If you need to open an application or software such as Microsoft Notepad, you need to go to the path where the application is installed, usually the default location is

C:\Windows\system32\

and then type

notepad

to open a new Notepad window. To search for the path where the application is installed, type the following on the prompt:

C:\>for %i in (notepad.exe) do @echo: %~$PATH:i
C:\WINDOWS\system32\notepad


Note the blank spaces, you need to specify it properly to work.

Replace

notepad

by the application name you want to search for.
This can be very irritating if you need to open an application often through the prompt and have to go to the path every time where the program is installed. If you try to type the name of the application anywhere other than the path where the application is installed, you will get an error which looks like this:





The solution:



To reduce this effort, you can specify Windows to search for the term you type in the prompt in certain fixed locations, called as class paths. You can specify the path to search as follows:

Start -> Settings -> Control Panel



Double-click on System, open "Advanced" tab, click on the "Environment Variables" button below. In the "System variables" frame below, scroll if there are many variables and click on "Path", click "Edit" to add or delete a new path. You can enter the path where your desired application is stored. If there are multiple paths, separate them by semi-colons(;).

For example, if you need to specify the class path for notepad++, installed at  

C:\Program files\Notepad++\


Click on "Path", then "Edit" button, if there already is a path specified, add a semi-colon after that, and type

C:\Program files\Notepad++\


after the already present path.

Now, you can type notepad++ at any path at the command prompt and a new "Notepad++" window will be opened.

Even more shorter:

In order to shorter the command notepad++, you can rename the file at the installed path to, say, np, and then type np at the prompt.
Caution: Renaming the executable file may cause the files linked to it, like a shortcut file on the Desktop, to stop working, so you will have to change the filename at respective locations too. However, this is one of the easiest methods to shorten the command. Please share if you find better methods to do so.