slyfox1186

open-cmd-here.ahk

Jun 3rd, 2021 (edited)
2,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     OpenCMDHere
  3.     Required: AutoHotkey.exe
  4.  
  5.     Opens 'cmd.exe' using the active explorer.exe window's folder path
  6.     as the working directory. If explorer is not the active window
  7.     when triggered it will open 'cmd.exe' using "C:\Windows\System32"
  8.     as the working directory.
  9.  
  10.     The '_pwd' VAR may require character changes in the file path
  11.     that is retrieved because of the way ComObject stores characters.
  12.     RegEx is used to change the special 'URL' encoding to 'ASCII'
  13.     format so 'cmd.exe' can read it without throwing errors.
  14.     https://www.w3schools.com/tags/ref_urlencode.ASP
  15. */
  16.  
  17. #SingleInstance, Force
  18. DetectHiddenText, On
  19. DetectHiddenWindows, On
  20. SetTitleMatchMode, 2
  21.  
  22. !c::_OpenCMDHere()
  23.  
  24. _OpenCMDHere()
  25. {
  26.     If WinExist("ahk_class CabinetWClass ahk_exe explorer.exe")
  27.         _winHWND := WinActive()
  28.     For win in ComObjCreate("Shell.Application").Windows
  29.         If (win.HWND = _winHWND)
  30.         {
  31.             _pwd := SubStr(win.LocationURL, 9)
  32.             _pwd := RegExReplace(_pwd, "%20", " ")
  33.             _pwd := RegExReplace(_pwd, "%21", "!")
  34.             _pwd := RegExReplace(_pwd, "%24", "$")
  35.             _pwd := RegExReplace(_pwd, "%25", "%")
  36.             _pwd := RegExReplace(_pwd, "%26", "&")
  37.             _pwd := RegExReplace(_pwd, "%27", "'")
  38.             _pwd := RegExReplace(_pwd, "%28", "(")
  39.             _pwd := RegExReplace(_pwd, "%29", ")")
  40.             _pwd := RegExReplace(_pwd, "%2D", "-")
  41.             _pwd := RegExReplace(_pwd, "%2E", ".")
  42.             _pwd := RegExReplace(_pwd, "%30", "*")
  43.             _pwd := RegExReplace(_pwd, "%3A", ":")
  44.             _pwd := RegExReplace(_pwd, "%3F", "?")
  45.             _pwd := RegExReplace(_pwd, "%40", "@")
  46.             _pwd := RegExReplace(_pwd, "%5B", "[")
  47.             _pwd := RegExReplace(_pwd, "%5D", "]")
  48.             _pwd := RegExReplace(_pwd, "%5E", "^")
  49.             _pwd := RegExReplace(_pwd, "%5F", "_")
  50.             _pwd := RegExReplace(_pwd, "%60", "``")
  51.             _pwd := RegExReplace(_pwd, "%7B", "{")
  52.             _pwd := RegExReplace(_pwd, "%7C", "|")
  53.             _pwd := RegExReplace(_pwd, "%7D", "}")
  54.             _pwd := RegExReplace(_pwd, "%7E", "~")
  55.             _pwd := RegExReplace(_pwd, "/", "\")
  56.         }
  57.     Run, "C:\Windows\System32\cmd.exe" /T:0A /K PROMPT $P$G$_$G, % _pwd ? _pwd : "C:\Windows\System32\",, _winPID
  58.     WinWaitActive, ahk_pid %_winPID%
  59.     WinGetPos,,, _Width, _Height, ahk_pid %_winPID%
  60.     WinMove, ahk_pid %_winPID%,, (A_ScreenWidth/2)-(_Width/2), (A_ScreenHeight/2)-(_Height/2)
  61. }
  62. Return
Add Comment
Please, Sign In to add comment