# _CMD()

## Recommended Posts

i hate having to type the full path when i need to run a commandline app

this way you can just browse to the app and hit #c

and you have a dos prompt with the correct dir.

(that is atleast if you have full path names in your titles)

hope it is use to someone:

ty gafrost

#NoTrayIcon

HotKeySet('#c', '_CMD')

While 1
Sleep(1000)
WEnd

Func _CMD()
Run(@comspec & ' /k cls & cd "' & $path) EndFunc Edited by gafrost Don't argue with an idiot; people watching may not be able to tell the difference. #### Share this post ##### Link to post ##### Share on other sites its been done. #include <Process.au3> _RunDOS($sCommand )

and btw, the window stays open if you use /k, using /c closes it after the command is executed so you don't need cls.

Edited by ryeguy

##### Share on other sites

its been done.

#include <Process.au3>

_RunDOS( $sCommand ) and btw, the window stays open if you use /k, using /c closes it after the command is executed so you don't need cls. Think you missed the point of what w0uter is attempting to do, he needs to go to the dos prompt Keeps the program running so that he can do that when he needs to _RunDOS would be a little bulky for that, why run a @ComSpec & " /C " &$sCommand (command would be equal to: (@comspec & ' /k cls & cd "' & $path) so now you have @ComSpec & " /C " & (@comspec & ' /k cls & cd "' &$path

Now if _RunDOS had a parameter for keep open or close then that would be usefull.

Edited by gafrost

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

Doesn't work with UNC paths, of course. What might make it work is to replace the 'cd' command with 'pushd'. Doing that, though, may make it work on only certain versions of Windows.

My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

##### Share on other sites

Doesn't work with UNC paths, of course. What might make it work is to replace the 'cd' command with 'pushd'. Doing that, though, may make it work on only certain versions of Windows.

I never knew about the "pushd" command. Thnks for that. =)

Here's it with that command. I can't take any credit of course, I didn't really do anything...

Func _nCMD()
$path = ControlGetText(Wingettitle(''),"","Edit1") Run(@comspec & ' /k cls & pushd "' &$path & '"')
EndFunc

The fun part is now trying to do a "if" or "case" so you can do it under one hot key.

@w0uter, nice idea. It is nice to write scripts that can get you where you want to be faster then M$way of doing things. It's simplicity wins you four stars.. Cheers.. #### Share this post ##### Link to post ##### Share on other sites And stupid me uses this old bastard [HKEY_CLASSES_ROOT\Directory\shell\cmd\command] cmd.exe /k "cd %L" @w0uter: That idea's just way too cool Edited by dabus #### Share this post ##### Link to post ##### Share on other sites kept forgetting if i had the program running or not, so added to it a lil bit #NoTrayIcon$G_SZVERSION = "_CMD Center ver 1.2" & @LF & @LF & "Made with AutoIt ver " & @AutoItVersion
If WinExists($G_SZVERSION) Then Exit; It's already running AutoItWinSetTitle($G_SZVERSION)

opt("TrayOnEventMode", 1)
HotKeySet("!e", "_Exit")
HotKeySet('!c', '_CMD')

$cmditem = TrayCreateItem("Open CMD Window") TrayItemSetOnEvent(-1, "_CMD")$aboutitem = TrayCreateItem("About")

$exititem = TrayCreateItem("Exit") TrayItemSetOnEvent(-1, "ExitScript")$dll = DllOpen("psapi.dll")
TraySetIcon(@SystemDir & "\cmd.exe", 0)

TraySetState()

$Current_Window = "" While 1 If WinGetTitle('') <>$G_SZVERSION Then $Current_Window = ControlGetText(WinGetTitle(''), "", "Edit1") _ReduceMemory($dll)
Sleep(50)
WEnd
DllClose($dll) Func OnAutoItExit() DllClose($dll)
EndFunc  ;==>OnAutoItExit

Func ExitScript()
Exit
EndFunc  ;==>ExitScript

Func _CMD()
Run(@ComSpec & ' /k pushd "' & $Current_Window & '"') EndFunc ;==>_CMD Func About() MsgBox(64, "About:",$G_SZVERSION)

Func _Exit()
Exit
EndFunc  ;==>_Exit

Func _ReduceMemory($dll = "psapi.dll") Local$ai_Return = DllCall($dll, 'int', 'EmptyWorkingSet', 'long', -1) Return$ai_Return[0]
EndFunc  ;==>_ReduceMemory
Edited by gafrost

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

@wOuter

On Windows 2000 and above you can use a '/d' with the CMD instead of the old way of 'D:' followed by 'cd D:\foo\bar'.

Run(@comspec & ' /k cd /d "' & $path & '" & cls', @SystemDir) @Klaatu Pushd doesn't allow for authentication, but is a very handy way of cd'ing to a UNC. Also, for those who didn't know, pushd and popd use a stack, so you can use multiple pushd's (it will create one drive letter for each call, going down from Z: or the last available letter), and popd to climb back up the chain. You should use a popd after you're done because on some OS's (XP I think, but not Windows 2000), the drive letter will stay mapped after you exit the command line window. You could have your program watch for drive letters create while CMD windows are open and unmap them when no CMD windows exist. Just a thought. If you need authentication, you can use something like this first, which will get you access to all shares on the remote machine to which you have access... Run(@comspec & ' /c net use \\remotemachine\IPC$ ' & $password & ' /user:' &$domain & "\" & $username & ' /persistent:no') and close the connection if you want with a... Run(@comspec & ' /c net use \\remotemachine\IPC$ /del /y')

Of course, you'd have to check for a '\\' in the $path to know it's a UNC, then split the remote machine name from that UNC for the net use command. Also, the '/y' only works on Windows 2000 and above. Edited by c0deWorm My UDFs: ExitCodes #### Share this post ##### Link to post ##### Share on other sites You should use a popd after you're done because on some OS's (XP I think, but not Windows 2000), the drive letter will stay mapped after you exit the command line window. XP Pro SP2 un-maps drive when closing command window Don't argue with an idiot; people watching may not be able to tell the difference. #### Share this post ##### Link to post ##### Share on other sites XP Pro SP2 un-maps drive when closing command window I just checked and Windows 2000 SP4 unmaps also. I don't remember which OS/SP-level didn't unmap, but there is at least one, so it's best IMO to plan for it. My UDFs: ExitCodes #### Share this post ##### Link to post ##### Share on other sites Good idea! A CMD hotkey should be mandatory! AutoIt hotkeys can interfere with other programs so I just use start menu hotkeys. This script puts a hotkey onto the default shortcut for these apps. It only runs once with no need to leave an AutoIt app running. ; ; Hot keys on shortcuts ; dim$a_Lnks[5][2]
; DOS Ctrl+Alt+D
$a_Lnks[0][0] = @StartMenuDir & "\Programs\Accessories\Command Prompt.lnk"$a_Lnks[0][1] = "^!d"

$a_Lnks[1][0] = @StartMenuDir & "\Programs\Accessories\Notepad.lnk"$a_Lnks[1][1] = "^!n"

$a_Lnks[2][0] = @StartMenuCommonDir & "\Programs\Accessories\WordPad.lnk"$a_Lnks[2][1] = "^!w"

; Calulator Ctrl+Alt+Shift+3
$a_Lnks[3][0] = @StartMenuCommonDir & "\Programs\Accessories\Calculator.lnk"$a_Lnks[3][1] = "^!+3"

; Paint Ctrl+Alt+P
$a_Lnks[4][0] = @StartMenuCommonDir & "\Programs\Accessories\Paint.lnk"$a_Lnks[4][1] = "^!p"

For $i = 0 to ubound($a_Lnks, 1) - 1
If FileExists($a_Lnks[$i][0]) == 1 then
$det = FileGetShortcut($a_Lnks[$i][0]) If StringInStr($det[3],"-HK") == 0 then; Skip shortcuts that have already been modified
FIleCopy($a_Lnks[$i][0], $a_Lnks[$i][0] & ".bak", 0)
FileSetAttrib($a_Lnks[$i][0] & ".bak", "+H")
FileCreateShortcut($det[0],$a_Lnks[$i][0],$det[1], $det[2],$det[3] & "-HK", $det[4],$a_Lnks[$i][1],$det[5], \$det[6])
EndIf
EndIf
Next

I also have a context menu for folders. This will open a DOS prompt, even to UNC paths using pushd.

; Open a Command Prompt to current folder
RegWrite("HKEY_CLASSES_ROOT\Directory\shell\cmd", "", "REG_SZ", "Open CMD Here")
; This will open the command prompt on UNC shares
RegWrite("HKEY_CLASSES_ROOT\Directory\shell\cmd\command", "", "REG_SZ", 'cmd.exe /k "pushd %L"')

## Create an account

Register a new account