Jump to content

cmd.exe experiment


Alterego
 Share

Recommended Posts

It's not perfect, but it works. You can run multiple commands at once and it sends all output to @HomeDrive \cmd.log.txt. It basically works by checking the log file for updates and then updating the screen. Tried to keep it as short as possible.

A couple things I could use help on

1) Making the hotkey for enter not steal the enter key from all programs even when the window isnt active

2) get it to scroll down automatically.

3) any idea why _ArrayToString won't work with commas? I could only get it to work with a very few characters. Ideally I could use a separator that was longer than one character long. Something funky not likely to end up in the log file..like "$%K#34"..that way running StringReplace on it doesn't replace anything it shouldnt. I think i even tried putting the UDF, only modified, into my code..but it just kept printing 0s

PS, this started from Guidosoft's posting of insolences gui code the other day. none of that code is in here...i was just playing around and this is what i ended up with.

#include <GUIConstants.AU3>
#include <file.au3>
#include <Array.au3>
Global $dir = @HomeDrive & "\cmd.log.txt"
Global $logData = @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "cmd.exe experiment." & _
        @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "Logging to: " & $dir & _ 
        @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "Enter your cmd.exe command." & _
        @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "Right click to copy all text to the clipboard" & _ 
        @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "Type 'help' for a list of commands." & _
        @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & "Type 'exit', 'close', or 'quit' to terminate the program."
Global $linesLastTime = _FileCountLines($dir)
Global $file,$count = 0
Global $guiWidth = 550, $guiHeight = 300
Global $gui = GUICreate("cmd.exe experiment", $guiWidth, $guiHeight, -1, -1, $WS_SYSMENU + $WS_EX_ACCEPTFILES + $WS_EX_OVERLAPPEDWINDOW + $WS_EX_DLGMODALFRAME + $WS_CAPTION + $WS_MAXIMIZEBOX + $WS_MINIMIZEBOX + $WS_SIZEBOX)
    GUISetBkColor(0x00EEFFFA)
    GUISetFont(9, -1, -1, "Verdana")
Global $log = GUICtrlCreateEdit($logData, 0, 0, $guiWidth, $guiHeight - 30, $WS_VSCROLL + $ES_AUTOVSCROLL + + $ES_MULTILINE)
    GUICtrlSetFont($log, 9, 400, "Verdana")
Global $input = GUICtrlCreateInput("", 0, $guiHeight - 30, $guiWidth, 30)
    ControlFocus($gui, "", $input)
    GUICtrlSetFont($input, 9, 400, "Verdana")
GUISetState(@SW_SHOW)
FileOpen($dir, 1)
FileClose($dir)
While 1
    While WinActive("cmd.exe experiment")       
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then Exit
        HotKeySet("{ENTER}", "process")
        If $msg = $GUI_EVENT_PRIMARYUP Then ClipPut($logData)       
    WEnd
    Sleep(250)  
WEnd
Func process()
    Global $command = GUICtrlRead($input)
    Select
        Case $command = "exit" Or $command = "close" Or $command = "quit"
            Exit
    EndSelect
    If $command <> "" Then
        GUICtrlSetData($input, "")
        Global $logData = $logData & @CRLF & @HOUR & ":" & @MIN & ":" & @SEC & ":  " & $command & @CRLF
        GUICtrlSetData($log, $logData)
        Run(@ComSpec & " /c " & $command & ">>" & $dir, @DesktopDir, @SW_HIDE)
        AdlibEnable("readlog")
    EndIf
    ControlFocus($gui, "", $input)
EndFunc  ;==>process
Func readlog()
    _FileReadToArray($dir, $file)
    Global $lines = $file[0]
    $file = _ArrayToString($file, '%', $lines - ($lines - $linesLastTime) + 1, $lines)
    Global $logData = $logData & StringReplace($file, '%', @LF & @HOUR & ":" & @MIN & ":" & @SEC & ":  ", 0, 0)
    If StringLen($logData) > StringLen(GUICtrlRead($log)) Then
        GUICtrlSetData($log, $logData)
        ControlSend("cmd.exe experiment", "", "3", "{PGDN}")
        ControlFocus("", "", $input)
    EndIf
    If Not ProcessExists("cmd.exe") Then AdlibDisable()
    $linesLastTime = $lines
EndFunc  ;==>readlog
Edited by Alterego
Link to comment
Share on other sites

I suggest saving the results to the @TempDir, but that's cool anyway :)

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...