Advertisement
Najeebsk

CMD4.ahk

Dec 25th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance Force
  2. SetworkingDir, %A_workingdir%
  3. #warn
  4. Gui,default
  5. Gui,-DPIScale
  6. Gui,color,black,black
  7. Gui,Font,s12 cYellow,Lucida Console
  8. DetectHiddenWindows On
  9. autotrim,off
  10. transform,s,chr,32
  11. transform,q,chr,34
  12.  
  13. wa:=A_screenwidth
  14. ha:=A_screenHeight
  15. xx:=100
  16.  
  17. DllCall("AllocConsole")
  18. ;DllCall("AttachConsole")
  19. ;DllCall("AttachConsole", "UInt", pid)
  20. WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
  21. ;WinMinimize % "ahk_id " DllCall("GetConsoleWindow", "uint")
  22.  
  23.  
  24. objShell := ComObjCreate("WScript.Shell")
  25.  
  26.  
  27.  
  28. ;---------------
  29. gosub,readLB
  30. ;---------------
  31. ;-------- saved at Najeeb 2021-12-12  17:36 UTC --------------
  32. cmdx:=""                            ;- first command for test
  33. ;cmdx:="ping www.google.com -n 4"
  34. #warn
  35. #noenv
  36. name1:="NAJEEB CMD"
  37. global name1
  38. e:=""
  39. CPX:="cp" . DllCall("GetOEMCP", "UInt")        ;- get CODEPAGE e.g. CP850
  40. x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*13)/xx,h:=(ha*2.8)/xx
  41. Gui,Add,Button,x%x%   y%y% w%w% h%h% gTEST01,GOOGLE-PING
  42.  
  43. ;---------------
  44. x:=(wa*1)/xx,y:=(ha*5)/xx,h:=(ha*56)/xx,w:=(wa*13.6)/xx
  45. Gui, Add,Listbox,x%x%   y%y% w%w% h%h%  vLB1 gLB1,%e1x%
  46. x:=(wa*15)/xx,y:=(ha*1)/xx,w:=(wa*60)/xx,h:=(ha*60)/xx
  47. Gui, Add, Edit, x%x%   y%y%  w%w% h%h% vED1               ;- here the output
  48. x:=(wa*1)/xx,y:=(ha*63)/xx,w:=(wa*65)/xx,h:=(ha*6)/xx
  49. Gui, Add, Edit, x%x%   y%y% w%w%  h%h% vED2,%cmdx%           ;- added CMDx command above for test / write command here and then ENTER
  50. x:=(wa*67)/xx,y:=(ha*63)/xx,w:=(wa*6)/xx,h:=(ha*3.2)/xx
  51. Gui, add, Edit, x%x%  y%y% w%w%  h%h% vED3,                          ;- write ENDED when command finished
  52. x:=(wa*67)/xx,y:=(ha*67)/xx,w:=(wa*6)/xx,h:=(ha*3.2)/xx
  53. Gui,add,button, x%x%  y%y% w%w%  h%h% gPrintx, PRINT          ;- write to file last commands and outputs
  54. x:=(wa*2)/xx,y:=(ha*2)/xx,w:=(wa*75)/xx,h:=(ha*75)/xx
  55. Gui, Show,x%x% y%y% w%w% h%h%,%name1%
  56. gosub,a0
  57. GuiControl,1: Focus,ED2
  58. ;send,^{end}
  59. return
  60. ;-------------------------
  61. Guiescape:
  62. Guiclose:
  63. DllCall("FreeConsole")
  64. sleep,200
  65. exitapp
  66. ;---------------------
  67. test01:
  68. gosub,ping
  69. return
  70.  
  71. ;----------------------------------------------
  72. ping:
  73. Gui,1:submit,nohide
  74. objExec := objShell.Exec("cmd /c ping -n 3 -w 1000 www.google.com")
  75. output:=""
  76. while,!objExec.StdOut.AtEndOfStream
  77.   {
  78.   GuiControlGet,ED1
  79.   ;-----------------------------------------
  80.   output.=objExec.StdOut.readline() . "`n"
  81.   ;-----------------------------------------
  82.   GuiControl,1:,ED1,%output%
  83.   }
  84. ;GuiControl,1:,ED1,%output%
  85. output=
  86. return
  87.  
  88. ;-------------------------
  89. ~$enter::
  90. gosub,a0
  91. return
  92. ;=========================================================
  93. AddOutputInGui(output) {
  94.    Control, EditPaste, %output%`r`n, Edit1,%name1%    ;- display output in 1st EDIT
  95. }
  96. ;=========================================================
  97.  
  98. A0:
  99. Gui,1:submit,nohide
  100. ed2a:="cmd /c " . ed2
  101. ;CmdRet(ED1a, Func("AddOutputInGui"),CPX)
  102. xxc:=cmdret(ED2a, Func("AddOutputInGui"),CPX)
  103. e .="`r`n===============================`r`n" . ed1 . " =`r`n-------------------------------`r`n" . xxc . "`r`n===============================`r`n"
  104. e := RegExReplace(e, "((^)|\R)\h*(?=\R|$)(?(2)\R)")              ;- remove empty lines
  105. gosub,clear
  106. return
  107. ;-------------------------
  108. CLEAR:
  109. Gui,1:submit,nohide
  110. Guicontrol,1:,ED2,
  111. Guicontrol,1:,ED3,ENDED
  112. GuiControl,1: Focus,ED2
  113. return
  114. ;-------------------------
  115. printx:
  116. f1=%a_desktop%\%a_now%_DOS_SCREEN_OUTPUT.txt
  117. Gui,1:submit,nohide
  118. if e<>
  119.  {
  120.  fileappend,%e%,%f1%,utf-8
  121.  run,%f1%
  122.  e:=""
  123.  }
  124. return
  125.  
  126. ;=========================================================
  127. CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
  128. {
  129.    static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
  130.         , STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
  131.    hPipeRead:=""
  132.    hPipeWrite:=""
  133.    sOutput:=""
  134.    DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
  135.    DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
  136.    
  137.    VarSetCapacity(STARTUPINFO , siSize :=    A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
  138.    NumPut(siSize              , STARTUPINFO)
  139.    NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
  140.    NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
  141.    NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
  142.    
  143.    VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
  144.    if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
  145.                               , "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
  146.    {
  147.       DllCall("CloseHandle", "Ptr", hPipeRead)
  148.       DllCall("CloseHandle", "Ptr", hPipeWrite)
  149.       throw Exception("CreateProcess is failed")
  150.    }
  151.    DllCall("CloseHandle", "Ptr", hPipeWrite)
  152.    VarSetCapacity(sTemp, 4096), nSize := 0
  153.    while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
  154.       sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
  155.       ;sOutput .= stdOut := StrGet(&sTemp, nSize)
  156.       ;sOutput .= stdOut := StrGet(&sTemp, nSize, CPX)
  157.       ( callBackFuncObj && callBackFuncObj.Call(stdOut) )
  158.    }
  159.    DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
  160.    DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
  161.    DllCall("CloseHandle", "Ptr", hPipeRead)
  162.    Return sOutput
  163. }
  164. ;==================== DOS HELP ================
  165. lb1:
  166. gui,submit,nohide
  167. GuiControl,1:,ED1,
  168. lb1=%lb1%
  169. stringmid,pingx,lb1,1,4
  170.  
  171. if (lb1="ping /?")
  172.  {
  173.  gosub,exec
  174.  return
  175.  }
  176.  
  177. if (pingx="ping")
  178.  {
  179.  ;gosub,ping
  180.  return
  181.  }
  182.  
  183. ;- run#xxx
  184. stringmid,lb1a,lb1,1,1
  185. if (lb1a="#")
  186.   {
  187.   stringtrimleft,lb1,lb1,1
  188.   try {
  189.   run,%lb1%
  190.   } catch {
  191.     erl:= errorlevel
  192.     msgbox, 262208,ERROR,Not found =%lb1%`nERROR=%erl%
  193.   }
  194.  return
  195.  }
  196.  
  197. ;------ change directories and open folder ----
  198. if lb1 contains % "cd "
  199. {
  200. k1:=""
  201. k2:=""
  202. stringsplit,k,lb1,`;,
  203. stringmid,str1,k2,1,2
  204. if (str1="::")
  205.   {
  206.   run, "%k2%"
  207.   return
  208.   }
  209. if k2<>
  210.  {
  211.  run,%k2%
  212.  return
  213.  }
  214. ;gosub,exec
  215. return
  216. }
  217. gosub,exec
  218. return
  219.  
  220.  
  221. exec:
  222. objExec := objShell.Exec(ComSpec " /c  " . lb1 . "`n")
  223. cl:=""
  224. while, !objExec.StdOut.AtEndOfStream
  225.     cl:= objExec.StdOut.ReadAll()
  226. GuiControl,1:,ED1,%Cl%
  227. cl:=""
  228. return
  229. ;-----------------------------------------------
  230.  
  231. ;----------------------------------------------
  232. readLB:
  233. ;--------------------- DOS commands -----
  234. fcdm=%a_scriptdir%\doscommands_new2.txt
  235. ifnotexist,%fcdm%
  236.    runwait,%comspec% /c help >"%fcdm%",,hide,
  237. ;-----------------------------------------
  238. i=0
  239. e1x:=""
  240. e1x =
  241. (Ltrim Join|
  242. HELP /?
  243. TYPE DATA\ALL.ls
  244. TYPE DATA\ATTRIB.ls
  245. TYPE DATA\CMD.ls
  246. TYPE DATA\COPY.ls
  247. TYPE DATA\DEL.ls
  248. TYPE DATA\DIR.ls
  249. TYPE DATA\FIND.ls
  250. TYPE DATA\HELP.ls
  251. TYPE DATA\MD.ls
  252. TYPE DATA\NET.ls
  253. TYPE DATA\NIR.ls
  254. TYPE DATA\REN.ls
  255. TYPE DATA\START.ls
  256. #charmap
  257. #notepad
  258. #calc
  259. #winver
  260. #N.exe
  261. #N.hta
  262. #NOTE.hta
  263. #NOTEREAD.hta
  264. #PCI.hta
  265. #CMD1
  266. #CMD2
  267. #CMD3
  268. #moviemk
  269. cd MyDocuments                 ;%a_mydocuments%
  270. cd Desktop                     ;%A_desktop%
  271. cd MyComputer                  ;::{20d04fe0-3aea-1069-a2d8-08002b30309d}
  272. cd MyNetworkPlaces             ;::{208d2c60-3aea-1069-a2d7-08002b30309d}
  273. cd NetworkConnections          ;::{7007acc7-3202-11d1-aad2-00805fc1270e}
  274. cd Printers                    ;::{2227a280-3aea-1069-a2de-08002b30309d}
  275. cd RecycleBin                  ;::{645ff040-5081-101b-9f08-00aa002f954e}
  276. cd Appdata                     ;%a_appdata%
  277. cd Appdatacommon               ;%a_appdatacommon%
  278. cd Programfiles                ;%a_programfiles%
  279. cd Programs                    ;%a_programs%
  280. cd ProgramsCommon              ;%a_programsCommon%
  281. cd StartMenu                   ;%a_StartMenu%
  282. cd StartMenuCommon             ;%a_StartMenuCommon%
  283. cd Startup                     ;%a_Startup%
  284. cd StartupCommon               ;%a_StartupCommon%
  285. cd Windows                     ;%a_windir%
  286. cd Temp                        ;%a_temp%
  287. cd WorkingDir                  ;%a_workingdir%
  288. cd Script                      ;notepad "%a_scriptfullpath%"
  289. cd ScriptDir                   ;%a_scriptdir%
  290. ping /?
  291. ping -n 4 -w 1000 www.google.com
  292. netstat -an
  293. netstat -e -s
  294. netstat
  295. ipconfig /?
  296. ipconfig
  297. ipconfig /all
  298. dir
  299. dir /P /Q /L /4 /TAWC
  300. dir /?
  301. help
  302. cmd /?
  303. ver
  304. vol
  305. date /t
  306. time /t
  307. path
  308. )
  309.  
  310. fileread,a,%fcdm%                 ;- textfile is in variable A
  311.    loop,parse,a,`n,`r           ;- parse each line
  312.    {
  313.    i++
  314.    if (i<3)                     ;- remove at least the first two lines from help
  315.      continue
  316.    y1=%a_loopfield%
  317.    stringmid,y2,y1,1,3
  318.    if (y1="" or y2="   ")
  319.      {
  320.      y1=
  321.      y2=
  322.      continue
  323.      }
  324.  
  325.    c1=
  326.    stringsplit,c,y1,%s%
  327.    if (c1="dir" or c1="ver" or c1="path" or c1="chcp")
  328.      e1x .= c1 . "|"
  329.    e1x .= c1 . " /?|"
  330.    }
  331. a=
  332. return
  333. ;--------------------- END SCRIPT---------------------------------
  334. #R::Reload
  335. #S::Suspend
  336. #P::Pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement