Sign in to follow this  
Followers 0

Batch execute commands

1 post in this topic

Posted

Use this to execute batch commands.

; --------------------------------------------------------------------------------------------------
; AutoIt Version:   3.0 
; Language:         English
;
; Script Version:   1.0
; Script Creation:  4/25/2005
; Author:           Yann Bouan for InhouseIT
;
; Script Function:
;   Run Single or Multiple Batch Commands
; Usage:
;   ITBatchRunner [-h][-p <directory>] [-f <file> [-s]] [command]
;           
;       -h Display help
;       -p path using * as wildcard (defaults to working directory)
;       -f file containing commands
;       -s run a batch for each command
;
;   Available variables $name, $prefix, $suffix
;
; Notes:
;   Directories must be followed by a backslash
;   Wildcards must all be on the same directory level
;   To prevent lock ups all message boxes timeout
;
; Examples: (simple but of limited practical use)
;   ITBatchRunner -p c:\myapplication\*\logs\ del $name.log
;   ITBatchRunner -p c:\myapplication\logs\*.log copy $prefix.log $prefix.txt
;   ITBatchRunner -p c:\myapplication\logs\*.log copy -f batch.bat
; --------------------------------------------------------------------------------------------------

; --------------------------------------------------------------------------------------------------
; Parse command line and launch appropriate batch(s)
; --------------------------------------------------------------------------------------------------
; Returns: nothing
Func Main ()
    Dim $path = @WorkingDir
    Dim $command =  ''
    For $i = 1 to $CmdLine[0] 
        If $command = '' And $i+1 <= $CmdLine[0] Then
            Select 
        ; Get path
            Case $CmdLine[$i] == '-p'
                    If StringLeft($CmdLine[$i+1],1) = '-' Then
                        MsgBox(0, 'Error', 'Bad path', 10)
                    Else
                        $path = $CmdLine[$i+1]
                        $i = $i+1
                        ContinueLoop
                    EndIf
        ; Get batch file
            Case $CmdLine[$i] == '-f'
                If FileExists ( $CmdLine[$i+1] ) Then
                    If $i+2 <= $CmdLine[0] And $CmdLine[$i+2] = '-s' Then
                    ; Execute multiple commands as individual batches
                        ExecMulti ( $path, $CmdLine[$i+1] )
                    Else
                    ; Execute multiple commands as single batch
                        ExecBatch ( $path, $CmdLine[$i+1], 1 )
                    EndIf
                    Exit
                Else
                    MsgBox(0, 'Error', 'Command File does not exist', 10)
                    Exit
                EndIf
            EndSelect
        EndIf
    ; Display help
        If StringLeft($CmdLine[$i],1) = '-' Then
            $helpMsg = ' Script Function:' & @CR
            $helpMsg =  $helpMsg &  '   Run Single or Multiple Batch Commands' & @CR
            $helpMsg =  $helpMsg &  'Usage:' & @CR
            $helpMsg =  $helpMsg &  '   ITBatchRunner [-p <directory>] [-f <file> [-s]] [command]' & @CR & @CR
            $helpMsg =  $helpMsg &  '       -h display help' & @CR
            $helpMsg =  $helpMsg &  '       -p path using * as wildcard (defaults to working directory)' & @CR
            $helpMsg =  $helpMsg &  '       -f batch file containing commands' & @CR
            $helpMsg =  $helpMsg &  '       -s run a new batch for each command line' & @CR & @CR
            $helpMsg =  $helpMsg &  '   Available variables $name, $prefix, $suffix' & @CR & @CR
            $helpMsg =  $helpMsg &  'Notes:' & @CR
            $helpMsg =  $helpMsg &  '   Directories must be followed by a backslash' & @CR
            $helpMsg =  $helpMsg &  '   Wildcards must all be on the same directory level' & @CR
            $helpMsg =  $helpMsg &  '   To prevent lock ups all message boxes timeout' & @CR & @CR
            $helpMsg =  $helpMsg & ' Examples: (simple but of limited practical use)' & @CR
            $helpMsg =  $helpMsg & '    ITBatchRunner -p c:\myapplication\*\logs\ del $name.log' & @CR
            $helpMsg =  $helpMsg & '    ITBatchRunner -p c:\myapplication\logs\*.log copy $prefix.log $prefix.txt' & @CR & @CR
            $helpMsg =  $helpMsg & '    ITBatchRunner -p c:\myapplication\logs\*.log copy -f batch.bat' & @CR & @CR         
            $helpMsg =  $helpMsg & '               ***** v1.0, made by InhouseIT (4/25/2005) *****             '
            MsgBox(0, 'ITBatchRunner Help', $helpMsg , 15)
            Exit
        EndIf
    ; Build command
        $command = $command & ' ' & $CmdLine[$i]
    Next
; Execute single command as batch
    ExecBatch ( $path, $command )
EndFunc 

; --------------------------------------------------------------------------------------------------
; Cycle through commands - Execute multiple commands on one match or all matches
; --------------------------------------------------------------------------------------------------
; Returns: nothing
Func ExecMulti ($path, $commandfile, $matchName = '' )
    $file = FileOpen( $commandfile, 0)
; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
; Read in lines of command until the EOF is reached
    Else
        While 1
            $command = FileReadLine($file)
            If @error = -1 Then ExitLoop
            Select
            Case $matchName = ''
            ; Execute Command on all matches
                ExecBatch ( $path, $command )
            Case Else
            ; Execute command on single match
                ExecCommand ($path, $command, $matchName)
            EndSelect
        Wend
        FileClose($file)
    EndIf
EndFunc

; --------------------------------------------------------------------------------------------------
; Cycle through matches - Execute one or more command on every match
; --------------------------------------------------------------------------------------------------
; Returns: nothing
Func ExecBatch ( $path, $command, $multi = 0 )
    $searchItems = StringSplit ( $path, '*')
    If $searchItems[0] > 1 And StringInStr( $searchItems[$searchItems[0]], "\") Then
        $searchPath = StringLeft( $path, StringLen($path) - StringLen( $searchItems[$searchItems[0]] ) + StringInStr( $searchItems[$searchItems[0]], "\")-1  )
    Else
        $searchPath = $path
    EndIf
; Initiate directory scan
    $search = FileFindFirstFile ( $searchPath )
; Exit if directory empty
    If $search = -1 Then
        MsgBox(0, 'Error', 'No files/directories matched the search pattern: ' & @CR & $searchPath, 10)
        Exit
    EndIf
; Cycle through directories
    While 1
        $matchName = FileFindNextFile ( $search )
        If @error Then ExitLoop
        If $matchName = '.' Or $matchName = '..' Then
           ContinueLoop
        EndIf
        $fullPath = $searchItems[1] & $matchName
        If $searchItems[0] > 1 And StringInStr($searchItems[$searchItems[0]],'\') Then
            $fullPath = $fullPath & $searchItems[$searchItems[0]]
        EndIf
        If FileExists( $fullPath ) Then
        ; Run command
            Select
            Case $multi = 1
            ; Execute multiple commands on match
                ExecMulti ($fullPath, $command, $matchName)
            Case Else
            ; Execute single command on match
                ExecCommand ($fullPath, $command, $matchName)
            EndSelect
        EndIf
    WEnd
EndFunc

; --------------------------------------------------------------------------------------------------
; Prepare and run command
; --------------------------------------------------------------------------------------------------
; Returns: nothing
Func ExecCommand ( $fullPath, $command, $matchName )
    $fullPath = StringLeft($fullPath, StringInStr($fullPath,'\',0,-1)-1)
    If FileExists( $fullPath ) Then
        $command = StringReplace ( $command, '$name', $matchName)
        $matchInfo = StringSplit($matchName, '.')
        $command = StringReplace ( $command, '$prefix', $matchInfo[1])
        If $matchInfo[0] = 2 Then
            $command = StringReplace ( $command, '$suffix', $matchInfo[2])
        Else
            $command = StringReplace ( $command, '$suffix', '')
        EndIf
    ; Run command
        RunWait( @ComSpec & ' /c ' & $command, $fullPath, @SW_HIDE)
    EndIf
EndFunc

Main()

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  
Followers 0