AHK获取CMD命令结果三种方法【RunAnyCtrl】

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效
User avatar
Zz_
Posts: 5
Joined: 28 Jun 2016, 22:48

AHK获取CMD命令结果三种方法【RunAnyCtrl】

28 Apr 2018, 03:41

【返回cmd命令的结果值 @hui-Zz】(闪动命令框)

Code: Select all

cmdReturn(command){ ; WshShell 对象: http://msdn.microsoft.com/en-us/library/aew9yb99 shell := ComObjCreate("WScript.Shell") ; 通过 cmd.exe 执行单条命令 exec := shell.Exec(ComSpec " /C " command) ; 读取并返回命令的输出 return exec.StdOut.ReadAll() }
【后台静默运行cmd命令缓存文本取值 @hui-Zz】

Code: Select all

cmdSilenceReturn(command){ CMDReturn:="" cmdFN:="RunAnyCtrlCMD" try{ RunWait,% ComSpec " /C " command " > ""%Temp%\" cmdFN ".log""",, Hide FileRead, CMDReturn, %A_Temp%\%cmdFN%.log FileDelete,%A_Temp%\%cmdFN%.log }catch{} return CMDReturn }
【隐藏运行cmd命令并将结果存入剪贴板后取回 @hui-Zz】

Code: Select all

cmdClipReturn(command){ cmdInfo:="" Clip_Saved:=ClipboardAll try{ Clipboard:="" Run,% ComSpec " /C " command " | CLIP", , Hide ClipWait,2 cmdInfo:=Clipboard }catch{} Clipboard:=Clip_Saved return cmdInfo }
【StdoutToVar取命令结果(第三方)】

Code: Select all

StdoutToVar_CreateProcess(sCmd, sEncoding:="CP0", sDir:="", ByRef nExitCode:=0) { DllCall( "CreatePipe", PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 ) DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1 ) VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24, 0 ) siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 ) NumPut( siSz, si, 0, "UInt" ) NumPut( 0x100, si, (A_PtrSize == 4) ? 44 : 60, "UInt" ) NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 60 : 88, "Ptr" ) NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 64 : 96, "Ptr" ) If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000 , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) ) Return "" , DllCall( "CloseHandle", Ptr,hStdOutWr ) , DllCall( "CloseHandle", Ptr,hStdOutRd ) DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout. While ( 1 ) { ; Before reading, we check if the pipe has been written to, so we avoid freezings. If ( !DllCall( "PeekNamedPipe", Ptr,hStdOutRd, Ptr,0, UInt,0, Ptr,0, UIntP,nTot, Ptr,0 ) ) Break If ( !nTot ) { ; If the pipe buffer is empty, sleep and continue checking. Sleep, 100 Continue } ; Pipe buffer is not empty, so we can read it. VarSetCapacity(sTemp, nTot+1) DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,nTot, PtrP,nSize, Ptr,0 ) sOutput .= StrGet(&sTemp, nSize, sEncoding) } ; * SKAN has managed the exit code through SetLastError. DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode ) DllCall( "CloseHandle", Ptr,NumGet(pi,0) ) DllCall( "CloseHandle", Ptr,NumGet(pi,A_PtrSize) ) DllCall( "CloseHandle", Ptr,hStdOutRd ) Return sOutput }
Last edited by Zz_ on 29 Apr 2018, 08:44, edited 1 time in total.
tmplinshi
Posts: 1278
Joined: 01 Oct 2013, 14:57

Re: AHK获取CMD命令结果三种方法【RunAnyCtrl】

28 Apr 2018, 07:55

建议用 StdoutToVar,避免弹出 CMD 窗口、产生临时文件等缺点。
User avatar
Zz_
Posts: 5
Joined: 28 Jun 2016, 22:48

Re: AHK获取CMD命令结果三种方法【RunAnyCtrl】

29 Apr 2018, 09:02

建议用 StdoutToVar,避免弹出 CMD 窗口、产生临时文件等缺点。
刚了解到StdoutToVar,不过我做了测试,发现StdoutToVar对findstr不能支持,比如ahk只获取当前Wifi名的一行,而StdoutToVar返回了所有结果

Code: Select all

StdoutToVar_CreateProcess("netsh wlan show interface | findstr ""`\<SSID""")
tmplinshi
Posts: 1278
Joined: 01 Oct 2013, 14:57

Re: AHK获取CMD命令结果三种方法【RunAnyCtrl】

29 Apr 2018, 09:18

管道符 | 是 CMD 的语法。

Code: Select all

StdoutToVar_CreateProcess("cmd /c netsh wlan show interface | findstr ""\<SSID""")

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 4 guests