Hello everyone,
In this post, I would like to write on some general topic (in order to target more audience :) ). If you have been a MS-DOS user, you would still love to work on the command prompt. Even now, besides all these graphics stuff, we need to have this command prompt to achieve things quickly, for example, mass deletion of files with some pattern, mass renaming etc. The only problem is, it is hard to navigate to a specific folder through command prompt. And, if the folder is a special folder like My Documents, My Pictures, Desktop etc, then it will be even more tough.
Just imagine, if there is an option in the context menu (menu which appears when you right click on any of the file or folder in Windows Explorer) for each of the folder, which let you to open it through command prompt. There are more software in the market that provide you this facility (and of course, with many others). In this post, I am going to show adding that functionality just by using Windows Registry.
Important Note : Changing Windows Registry values, adding or deleting keys may affect the stability of Windows. So exercise additional care while doing such things.
Here, I have linked one .reg file with which, you can bring the above said functionality to context menu of every folder in the system.
https://sites.google.com/site/lazyprogrammers/openwithcmd
You have to import the contents of the registry file into your system registry as shown below.
1. Execute the .reg file. Either double click or right click and select Merge option.
2. Click Yes for the below option.

3. Click OK on the next acknowledgement screen.

That’s it. You have installed this utility. Now go and check any folder in the system by right-clicking it. It should have an option in the context menu as given below.

Just click on this, the folder will be opened in the command prompt folder. If you are just a computer user, then you can stop reading this post with this. If you are a programmer or if you really want to know what is happening behind the scenes, let’s go further.
In order to understand what this .reg file is really doing to bring this functionality, let’s have a closer look on the below points.
Now, we have this setup ready. All we have to do is to hook it to the commands available in the context menu of every folder. For this, we have to understand how Windows Registry and how it is organized. I will, in future, write a separate post on this subject (as far as I know). For the time being, let’s concentrate on only one Key set called HKEY_CLASSES_ROOT.
This key set contains all known file extensions of a machine. The “known file extensions” here mean, that Windows knows, how to handle files with those extensions. For example, .txt, .bmp, .jpg are few of them. It has some special sub keys for Directory and Drive. We have to modify the Directory sub key in order to achieve this.
Execute regedit from Run window to get the registry editor.Goto HKEY_CLASSES_ROOT\ Directory in the left side tree view. Expand all the sub keys under this key.
The screen should look like this.

Now let us have a closer look on the shell sub key. This key contains the commands available in the context menu of a folder. I have the VLC player installed and hence you can see the Add to Playlist, Play with VLC command related keys under the Shell key.
Now, we have to add a new key under this shell and let’s name it as OpenWithCMD. Below picture shows, how to do this.

This key has a default value, which you can see in the right side of the Registry editor screen. This tells Windows, what would be the caption of this menu item in the screen. So let us give Open with Command Prompt as the value here.

If you use & key before any of the character in this default value, that will be shown as underlined in the screen and will be used as the accelerator key for that option (as in Visual Basic.NET). For example, if you enter “O&pen with Command Prompt”, it will be shown in the menu as “Open with Command Prompt”. So after you opened the context menu, if you press character “p”, it will highlight this command immediately. If you don’t have any other command in the menu with p underlined, then this command will be immediately executed. In this way, you can accelerate the command.
Now let us create a subkey under this OpenWithCMD key. This is similar to how we create the OpenWithCMD key. We have to name it as “command” as it contains the command definition of this menu option.
After creating command key, goto the default value of that key (in the right side window). Give the command that we prepared in the above section, as the default value.
cmd.exe /K cd /D "%1"
Click on OK. Now it’s ready to work. Without even refresh, just go to any Windows explorer, and check the context menu of any folder. Our newly created option should be available.
In Registry editor, we have an option to export or import any key with values. So I exported the above created key into the OpenWithCMD.reg file and attched here. This will make the installation job of this utility (!?) easier :)
In this post, I would like to write on some general topic (in order to target more audience :) ). If you have been a MS-DOS user, you would still love to work on the command prompt. Even now, besides all these graphics stuff, we need to have this command prompt to achieve things quickly, for example, mass deletion of files with some pattern, mass renaming etc. The only problem is, it is hard to navigate to a specific folder through command prompt. And, if the folder is a special folder like My Documents, My Pictures, Desktop etc, then it will be even more tough.
Just imagine, if there is an option in the context menu (menu which appears when you right click on any of the file or folder in Windows Explorer) for each of the folder, which let you to open it through command prompt. There are more software in the market that provide you this facility (and of course, with many others). In this post, I am going to show adding that functionality just by using Windows Registry.
Important Note : Changing Windows Registry values, adding or deleting keys may affect the stability of Windows. So exercise additional care while doing such things.
Here, I have linked one .reg file with which, you can bring the above said functionality to context menu of every folder in the system.
https://sites.google.com/site/lazyprogrammers/openwithcmd
You have to import the contents of the registry file into your system registry as shown below.
1. Execute the .reg file. Either double click or right click and select Merge option.
2. Click Yes for the below option.
3. Click OK on the next acknowledgement screen.
That’s it. You have installed this utility. Now go and check any folder in the system by right-clicking it. It should have an option in the context menu as given below.
Just click on this, the folder will be opened in the command prompt folder. If you are just a computer user, then you can stop reading this post with this. If you are a programmer or if you really want to know what is happening behind the scenes, let’s go further.
In order to understand what this .reg file is really doing to bring this functionality, let’s have a closer look on the below points.
- Let us first target on, how command prompt is opened with the specified path first. Then we will see how to bring this option into the context menu of every folder.
- Whenever, a command, available in the context menu, is invoked, the full path of the folder will be passed as a command line argument to that particular application.
- We have to invoke the cmd.exe in order to bring the command prompt.
- Just invoking the cmd.exe is not enough. We have to browse to the mentioned folder. So we use the “cd” MS-DOS command for this.
- cmd.exe has an option
- to execute a given command and close itself
- to execute a given command and remains open
- For the later, we have to invoke cmd with /K option.
- So, let us invoke cmd.exe as below.
- cmd.exe /K cd /D “%1”
- cd “%1” will instruct the cmd.exe to browse itself to the invoked folder. %1 here refers the first command line argument, passed to cmd.exe (the full path of the folder in this case).
- We are surrounding %1 with a pair of double quotes in order to avoid issues when the full path of the folder contains any space in it.
- /K option will keep the command window execute the command and keep open.
- /D option will instruct the cd command to change the drive path if required. i.e by default the command prompt will be invoked with C: as the working drive. If we open a folder which resides in other foler, then the cd command will change the directory and browse to the specified path.
Now, we have this setup ready. All we have to do is to hook it to the commands available in the context menu of every folder. For this, we have to understand how Windows Registry and how it is organized. I will, in future, write a separate post on this subject (as far as I know). For the time being, let’s concentrate on only one Key set called HKEY_CLASSES_ROOT.
This key set contains all known file extensions of a machine. The “known file extensions” here mean, that Windows knows, how to handle files with those extensions. For example, .txt, .bmp, .jpg are few of them. It has some special sub keys for Directory and Drive. We have to modify the Directory sub key in order to achieve this.
Execute regedit from Run window to get the registry editor.Goto HKEY_CLASSES_ROOT\ Directory in the left side tree view. Expand all the sub keys under this key.
The screen should look like this.
Now let us have a closer look on the shell sub key. This key contains the commands available in the context menu of a folder. I have the VLC player installed and hence you can see the Add to Playlist, Play with VLC command related keys under the Shell key.
Now, we have to add a new key under this shell and let’s name it as OpenWithCMD. Below picture shows, how to do this.
This key has a default value, which you can see in the right side of the Registry editor screen. This tells Windows, what would be the caption of this menu item in the screen. So let us give Open with Command Prompt as the value here.
If you use & key before any of the character in this default value, that will be shown as underlined in the screen and will be used as the accelerator key for that option (as in Visual Basic.NET). For example, if you enter “O&pen with Command Prompt”, it will be shown in the menu as “Open with Command Prompt”. So after you opened the context menu, if you press character “p”, it will highlight this command immediately. If you don’t have any other command in the menu with p underlined, then this command will be immediately executed. In this way, you can accelerate the command.
Now let us create a subkey under this OpenWithCMD key. This is similar to how we create the OpenWithCMD key. We have to name it as “command” as it contains the command definition of this menu option.
After creating command key, goto the default value of that key (in the right side window). Give the command that we prepared in the above section, as the default value.
cmd.exe /K cd /D "%1"
Click on OK. Now it’s ready to work. Without even refresh, just go to any Windows explorer, and check the context menu of any folder. Our newly created option should be available.
In Registry editor, we have an option to export or import any key with values. So I exported the above created key into the OpenWithCMD.reg file and attched here. This will make the installation job of this utility (!?) easier :)

No comments:
Post a Comment