Jump to content
Sign in to follow this  
Guest Guidosoft

Shell and RunEx

Recommended Posts

Guest Guidosoft

I know it's simple but it's cool so I just post it cause its not worth putting in an include or anywhere else.

Func Shell($ShellCommand);Runs a MS-DOS command.
   Run(@ComSpec & " /C " & $ShellCommand,"",@SW_HIDE)
EndFunc

Func RunEx($File);Runs a file.
   Run(@ComSpec & " /C start " & $File,"",@SW_HIDE)
EndFunc

I just learned what @ComSpec is for so I got excited, don't spoil my fun.

Share this post


Link to post
Share on other sites
Guest brammeleman

I just made a function to run a shell command and grab the output. It comes with a modified version of _TempFile:

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:         B. Daams <brammeleman@brammeleman.org>
;
; Script Function:
;                Execute a single DOS command and return stdout
;
; Example:
;                Msgbox(4096, "Output of 'ls' on a remote host:", _ShellExec("c:\progra~1\putty\plink.exe host ls"))
; ----------------------------------------------------------------------------

Func _ShellExec($cmd)

    $batchtemp = _TempFile2("bat")
    $resulttemp = _TempFile2("txt")

; add " > $resulttemp" to $cmd
    $cmd = $cmd & "> " & $resulttemp

; write $cmd to $batchtemp
    $fh = FileOpen ($batchtemp,2)
    If $fh = -1 Then
        MsgBox(0, "Error", "Unable to open temporary batch file for writing.")
        Exit
    EndIf
    FileWrite($fh, $cmd)
    FileClose($fh)

; run $batchtemp
    RunWait($batchtemp, @TempDir, @SW_HIDE)
    
; read $resulttemp to $returnval
    $fh = FileOpen($resulttemp, 0)

; Check if file opened for reading OK
    If $fh = -1 Then
        MsgBox(0, "Error", "Unable to open result file.")
        Exit
    EndIf

; Read in lines of text until the EOF is reached
    $returnval = ""
    While 1
        $returnval = $returnval & @LF & FileReadLine($fh)
        If @error = -1 Then ExitLoop
    Wend

; clean up the mess
    FileDelete($batchtemp)
    FileDelete($resulttemp)
    
    return $returnval
EndFunc

Exit

Func _TempFile2($ext = "tmp")
; slightly modified version of UDF _TempFile
; this function accepts an optional argument $ext which
; allows you to specify the file type for the temporary
; file
    Local $s_TempName
    
    Do
        $s_TempName = "~"
        While StringLen($s_TempName) < 8
         $s_TempName = $s_TempName & Chr(Round(Random(97, 122), 0))
        Wend
        $s_TempName = @TempDir & "\" & $s_TempName & "." & $ext
    Until Not FileExists($s_TempName)
    Return ($s_TempName)
EndFunc
Edited by brammeleman

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...