Jump to content


Open command window (with more features)


  • Please log in to reply
19 replies to this topic

#1 Sandeep

Sandeep
  • Guests

Posted 30 September 2004 - 10:06 AM

I used to use Windows PowerToy from MS "Open Command Window Here", although useful but it was too much pain.
I used to wonder what is the best way run DOS commands under command window without closing it immediately after run giving message that I forgot to enter any switch.
But then there was AutoHotKey...amazing! It is like panacea for Windows.

Following code opens command window with following additions:
a) If you are on a folder: changes the current folder of the command window to the folder in the explorer. Just like the so called PowerToy from MS.
B) If you are on file: additionaly sends the filename to the command window so that you can conveniently add any switches if you wish or just run the file as it is.
#c::
WinGetText, text, A  ; This is required to get the full path of the file ftom the address bar
StringSplit, word_array, text, `n	; Get the first string which is usually full path in the address bar
t = %word_array1%	; Take the first element from the array
StringReplace, t, t, `r, , all  ; Just in case
clipboard_prev = %clipboard%
clipboard =
Send, {F2}
Send, ^c
Send, {Esc}
cb = %clipboard%
clipboard = %clipboard_prev%
IfInString t, \
{
	IfInString cb, .
	{
		Run, cmd /K cd "%t%"
		WinWaitActive, cmd.exe,,1
		Send, %cb%
	}
	else
		Run, cmd /K cd "%t%"
}
else
	Run, cmd /K cd "C:\"
return
Although there is no harm in running this anytime you wish but, this is meant to work in Windows Explorer only, as there are many unhandled cases.
As you can see, it works only if you have address bar visible and also "Display full path in the address bar checked" option is checked (which I anyway keep checked).

Not quite sure if this is the only way to do what I wanted.
Any suggestion/feedback is welcome.

#2 Chris

Chris
  • Administrators
  • 10727 posts

Posted 30 September 2004 - 11:03 AM

That's nice. It reminds me of this script, which you may have already seen but if not might be worth a look: http://www.autohotke... ... olders.htm

#3 Sandeep

Sandeep
  • Guests

Posted 30 September 2004 - 11:55 AM

I was working with just that script. It's really a nice piece of work.
But, the purpose is little different.

ahk ROCKS! I can't believe it is so much useful...since I have started using it (which is only few days now)...my mind keeps on rolling for new ideas to make me work faster and smarter!
It has become one of the most important utilities for me.
May I recommend VirtuaWin and Miranda IM, the other two very useful opensource utilities that I use everyday!

Thanks for ahk!
Thanks for the forum!

#4 Rajat

Rajat
  • Members
  • 1886 posts

Posted 30 September 2004 - 12:58 PM

u might like to see this too ...coded this long back and still use it.

http://www.autohotke...topic.php?t=348

#5 Adrian

Adrian
  • Members
  • 1 posts

Posted 03 December 2006 - 11:18 PM

This script is great, I found it very usefull. However, there is a little bug.

Instead of

Run, cmd /K cd "%t%"

It must be

Run, cmd /K cd /D "%t%"

Without the /D switch, the command promt window will open in the active drive, not in the drive you are exploring.

#6 Zed Gecko

Zed Gecko
  • Members
  • 149 posts

Posted 04 December 2006 - 02:30 AM

Nice Script,

i like it, but i don´t want to waste a general shortcut with it.
So i was happy to find this post:
http://www.autohotke.../topic2953.html

It made it easy to modify your code to this:


splitpath, 1, file, dir
 
filegetattrib, attr, %1%
if attr contains D ; launched from a directory
{

	  Run, cmd /K cd /D "%dir%"

}
else ; launched from a file
{

      Run, cmd /K cd /D "%dir%"
      WinWaitActive, cmd.exe,,1
      Send, %file%
  
}
return

The script is not usefull, when run directly, but
if you create a registry key like this:

REGEDIT4

[HKEY_CLASSES_ROOT\AllFileSystemObjects\shell\CMD from here]
@="CMD from here"

[HKEY_CLASSES_ROOT\AllFileSystemObjects\shell\CMD from here\command]
@="\"C:\\Program Files\\AutoHotkey\\AutoHotkey.exe\" \"x:\\CMD from here.ahk\" \"%1\""

the script will be integrated in the right-click Menu of the Explorer
and perform the same action as your script.

#7 below_average_joe

below_average_joe
  • Members
  • 25 posts

Posted 06 December 2006 - 11:27 PM

Hi,

the script by the OP might be just what I wanted (although I don't understand it, unfortunately), because I already had (rather primitive, but it works):
; COMMANDLINE WITH CTRL + APPS-KEY
^AppsKey::
; Ctrl+Appskey opens command line window in root directory (D:\)
	{
	Run, C:\WINDOWS\system32\cmd.exe
	WinWait, C:\WINDOWS\system32\cmd.exe
	Send, cd {\}{Enter}
	}
return
which just opens a command prompt window by Ctrl-AppsKey.
Now what I was looking for was "Command Prompt here" for a selected folder in Explorer (preferrably selected either in the left or right pane) via keyboard shortcut (here: Ctrl-AppsKey).
I tried
^AppsKey::
Run, cmd.exe /a /k d:\tools\startprompt "%1"
with d:\tools\startprompt.cmd a batch file
@echo off
cd /d %1
Title %~f1
All this works via the folder's context menu, but when trying to add it to my main AHKscript to run it with AHK and keyboard, I get an error message when reading the script:
"This parameter contains a variable name missing its ending percent sign."
So I tried to enclose the "d:\tools\startprompt "%1"" part into additional quotes, to no avail. Actually, this functionality might be enough for me, but I don't know how to pass the "/a /k d:\tools\startprompt "%1"" to cmd.exe.
I might also take the (additional) functionality the OP promised, but I can't get that to work properly, either (even with Adrian's correction, and I don't understand the script, anyway). When I use the shortcut not being in explorer, I get an error "directory not valid", and it jumps to the AHK directory. In Explorer, it works with selected folders in the left pane and files in the right pane, but not with selected folders in the right pane (it always goes to the parent directory, not the selected one).
Can anyone help with modifications to
a) make it choose a specific directory when not in Explorer and
B) make it choose the selected directory of the right-hand pane in the active Explorer window.
BTW, the "parent directory" problem is the same in Total Commander and Directory Opus. I only used Explorer as an example for compatibility.

TIA
Alex

#8 below_average_joe

below_average_joe
  • Members
  • 25 posts

Posted 06 December 2006 - 11:38 PM

To add even more blurb to an already convoluted message:
Why "Command Prompt here" via keyboard is better than via context menu (registry):
Mainly because you can take it along with you on your thumbdrive :)
Well, can you (regarding the OP's script)?

#9 Zed Gecko

Zed Gecko
  • Members
  • 149 posts

Posted 08 December 2006 - 04:03 AM

@ below average
i don´t really undestand what you are trying to do, but
i assume that the problem:

"This parameter contains a variable name missing its ending percent sign."


is either because you really need to add the ending percent, and then you don´t need the quotes
Run, cmd.exe /a /k d:\tools\startprompt %1%
or you need to escape the percent sign, to make ahk pass it correctly.

Run, cmd.exe /a /k d:\tools\startprompt "`%1"

to your second post i just want to state that with regwrite and regdelete you can make the context menu version also portable!

#10 below_average_joe

below_average_joe
  • Members
  • 25 posts

Posted 09 December 2006 - 09:28 PM

i don´t really undestand what you are trying to do

I want to have "CMD from here" (also known as "DOS here", "Command Prompt here" etc.) via keyboard, not context menu, so I don't have to grab for a mouse. Plus, I want it portable. I specifically do not want to touch the registry. And I want it to work with the folder selected in the right explorer pane, not the parent folder. I think I already wrote all that.

I tried all of those:
Run, cmd.exe /a /k d:\tools\startprompt %1%
Run, cmd.exe /a /k d:\tools\startprompt "%1%"
Run, cmd.exe /a /k d:\tools\startprompt.cmd %1%
Run, cmd.exe /a /k d:\tools\startprompt "`%1"
None of them works, almost all send me to the AHK program directory, the last one also gives me an error that the specified path could not be found.
I also tried from the post above:
Run, cmd /K cd /D "%dir%"
which also gives me a prompt in the AHK directory.

#11 Zed Gecko

Zed Gecko
  • Members
  • 149 posts

Posted 10 December 2006 - 05:31 AM

Ok.
I modified sandeeps script a little, try it out.

^!c::
WinGetText, text, A  ; This is required to get the full path of the file from the address bar
; because the path is not always in the first line
; we search for the first line containing ":\" and take this
Loop, parse, text, `n
{
	IfInString A_Loopfield, :\
	{
		t := A_Loopfield
		break
	}
}

clipboard_prev = %clipboard%
clipboard =
Send, {F2}
Send, ^c
Send, {Esc}
cb = %clipboard%
clipboard = %clipboard_prev%
IfInString t, \
{
   IfInString cb, .
   {
      Run, cmd /K cd /D "%t%"
      WinWaitActive, cmd.exe,,1
      Send, %cb%
   }
   else
   	{
		fullt := t . "\" . cb
		Run, cmd /K cd /D "%fullt%"
	}
}
else
  	{
		fullt := t . "\" . cb
		Run, cmd /K cd /D "%fullt%"
	}
return


#12 below_average_joe

below_average_joe
  • Members
  • 25 posts

Posted 10 December 2006 - 04:35 PM

Thanks, but now in Explorer this version only works with folders in the right hand pane. If I select anything in the left pane, I get the "could not find the specified path" message and a prompt in the AHK directory.
In Total Commander it still goes to the parent directory of the selected file or I get a "wrong syntax for file or folder name" (very strange) or "invalid path" message.
For all these messages, I don't know the exact words in the English version, as I had to translate them for this post.

#13 Zed Gecko

Zed Gecko
  • Members
  • 149 posts

Posted 11 December 2006 - 05:26 AM

try this:

^!c::
WinGetText, text, A  ; This is required to get the full path of the file ftom the address bar
Loop, parse, text, `n
{
	IfInString A_Loopfield, :\
	{
		t := A_Loopfield
		break
	}
}
StringSplit, partt, t , \
checkLR := partt%partt0%

clipboard_prev = %clipboard%
clipboard =
Send, {F2}
Send, ^c
Send, {Esc}
cb = %clipboard%
clipboard = %clipboard_prev%

IfInString t, \
{
	IfInString cb, .
	{
		Run, cmd /K cd /D "%t%"
		WinWaitActive, cmd.exe,,1
		Send, %cb%
	}
	else
	{
	IfInString checkLR, %cb%
			fullt := t
		else
			fullt := t . "\" . cb
		Run, cmd /K cd /D "%fullt%"
	
	}
}
else
{
	IfInString checkLR, %cb%
		fullt := t
	else
		fullt := t . "\" . cb
	Run, cmd /K cd /D "%fullt%"

}
return


#14 toralf

toralf
  • Fellows
  • 3948 posts

Posted 11 December 2006 - 08:47 AM

I hope this shorted code helps
F1:: 
  ;get selected file/folder
  ClipSaved := ClipboardAll   
  Clipboard =
  Send, ^c
  ClipWait, 0.5
  File = %Clipboard% 
  Clipboard := ClipSaved 

  ;get only first one
  Loop, Parse, File, `n, `r
    {
      File := A_LoopField
      Break
    }

  ;check attributes
  FileGetAttrib, Attrib, %File%

  ;if folder
  If InStr(Attrib,"D")
      Run, %ComSpec% /K , %File%
  ;if file
  Else {    
      SplitPath, File, FileName, Dir
      Run, %ComSpec% /K , %Dir% ,,PID
      WinWaitActive, ahk_pid %PID% 
      Send, %FileName% 
    }
return
*tested*

Doesn't work for UNC paths.

#15 below_average_joe

below_average_joe
  • Members
  • 25 posts

Posted 11 December 2006 - 12:33 PM

toralf, your version works nicely!
Tested it in Explorer (left and right pane), TC, DOpus (left and right pane).
Great, thanks!