Sunday, April 11, 2010

Shell Utilities – The Command Prompt

Have you ever been into a situation, where you had to open the command prompt, and navigate to a directory deep by entering the drive name and after that keying in all those colossal paths into the prompt to get there, pretty tough uh..?  I really get crazy doing this kind of things.



Fig 1.


After Googling, I came out with handful of interesting solutions to this mundane task.

1. Microsoft – “Open Command Window Here”. This was the best solution I came across (and the frequently used), to quickly open a command prompt from the directory of choice. Microsoft “Power Toys for Windows XP” is providing a FREE utility called “Open Command Window Here”. You can grab a copy for yourself from the link below.

Once installed, you will find a new item named “Open Command Window Here” under the “context menu” as in Fig 2. Now this made things get done in a jiffy, I just need to right-click the folder and select “Open Command Window Here” from the context menu, boom!!! the Command Prompt shows up with the folder you had selected. That’s it.







Fig 2.


2. “Tab” key to the rescue. I really found this “Tab” feature quite handy, before I came to know about the “Open Command window here” utility. In the screen (Fig 1), instead of keying in the entire directory name, say “CLRProfilerTest”(The colossal one, for me) you can key in the first few alphabets like CLR and press “Tab” key to auto complete it for you. If you have multiple directories with the same name (say “CLRProfilerDemo” and “CLRProfilerTest”), repeatedly press the “Tab” key to show up each directory name, until the directory you need is displayed. This one is a real time saver, when struggling with huge directory names. Pretty elegant solution.

3. Behind the scenes – I was wondering how the “Open Command window here” was handling this simple but quite handy feature, which made me to dig into the windows DNA (The Windows registry). Eureka!!! Its there, I found out. Fireup(keyin) “regedit”, from Start->Run(WARNING: Don’t create\alter any settings in registry, if you are not sure what that’s for). 
Navigate to “My Computer\HKEY_CLASSES_ROOT\Directory\shell”. You will see like the one in Fig 3.










Fig 3
Voila. Here's the code for this pretty cool utility. You can see the value cmd.exe /k "cd %L" What this code does is, it opens Command Prompt by passing two parameters. First one is "/K" which tells the cmd.exe to interpret\execute the  second parameter. The second parameter "cd %L" is a change directory command (inbuilt command) followed by the "%L", which passes the absolute path of selected directory.  executing the command will look like the following, if the selected directory location is "C:\Windows\System32"



cmd.exe /k "cd c:\windows\system32"

like this you can create custom commands for the directory as you wish.

If you cant get the “Open Command Window Here” utility. Here is pretty simple hack (works only if have admin rights), just copy and paste the below text to a notepad, save the file with a .reg extension (say OpenCommandPromptHere.reg). 

Windows Registry Editor Version 5.00


[HKEY_CLASSES_ROOT\Directory\shell\cmd]
@="Open Command Window Here"

[HKEY_CLASSES_ROOT\Directory\shell\cmd\command]
@="cmd.exe /k \"cd %L\""

Double click the file (the .reg file you just now created), click “Yes” for the confirmation, that’s it. Now the “Open Command Prompt Window here” will get listed in your context-menu while right-clicking directories under windows explorer.

No comments: