Jump to content

Running a script from command line


juliop
 Share

Recommended Posts

Hello,

I've started using AutoIt for the last few weeks and I think autoit is the king of automation!

This is my first post and I'm hoping there is a simple answer to my question...

I have a script that copies files from a network location into the same location as the script file, performs a few actions with each file (via FileMaker Pro Advanced) and deletes each file. The script works perfectly if I launch it by double clicking on it, but when I try to run it via command line, some of the functions that involve file paths don't seem to work. Here's the syntax I'm using to call the script via command line:

"C:\Program Files\AutoIt3\Autoit3.exe" "C:\Documents and Settings\administrator\Desktop\Test\ScriptFile.au3"

By the way, the script also works fine if I compile it as an executable. It only fails when I call it via command line.

Any help will be appreciated.

Julio

Link to comment
Share on other sites

I would check your code at the points where you used @ScriptDir and/or @WorkingDir. (real used path is different from what you think it is.)

Alternative reason might be some admin-rights conflict. (folder access)

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

For @ScriptDir ... nope, as that one, as far as I know, should be the always the same. Location of the Au3 source-code file or the location of the build executable.

@WorkingDir however can be different on how your executable is called. But how it behaves when starting you script with Autoit3.exe I don't know.

To be sure you need to check it/them. MsgBox() seems best for that in this case.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Can you show us your script. It's a low easier than guessing what you are doing. :mellow:

But something you might like to check...

If you arn't specifying where files are (I.E. using @ScriptDir or @WorkingDir) then the script will use the current directory (ie @WorkingDir) where you enter the command line from. AND if you specifying some and not others you'll end up with a big mess. :)

But this is only a guess without seeing your code.

Good Luck

John Morrison

Link to comment
Share on other sites

Thanks for the quick responses!

Here's the code for the script I'm working with:

FileDelete("Files.txt")
FileDelete("File1.txt")
;######################################################C************************opy files from network
#Include <File.au3>
#Include <Array.au3>
#Include <Folders.au3>
 
$mainPath =   "C:\Documents and Settings\admin\Desktop\Notes\New Folder"
 
$folders =  _GetFileListInVector( $mainPath, "*.fp7")
$fileCount =  $folders[0]    ;Capture file count
_ArrayDelete($folders,0)             ; Delete the first line, it has nothing on it
 
$data =   _ArrayToString($folders,@CRLF)
 
FileWrite("File1.txt",$data)
 
$file = FileOpen("File1.txt", 0)
;################################################################# Check if file opened for reading OK
If $file = -1 Then
   ; MsgBox(0, "Error", "Unable to open file.")
    ;Exit
EndIf
$lineCount = _FileCountLines("File1.txt") - 2
$ErrorCount = 0
; Read in lines of text until the EOF is reached
$number = 0
While 1
    If $fileCount < 1 Then ExitLoop
  $line = FileReadLine($file)
  $number = $number + 1
  $path = @ScriptDir & "\"
  If @error = -1 Then ExitLoop
;Capture the filenames that need to be processed
    Dim $szDrive, $szDir, $szFName, $szExt
    $TestPath = _PathSplit($line, $szDrive, $szDir, $szFName, $szExt)
    $data1 = _ArrayToString($TestPath,@CRLF)
    FileWriteLine("Files.txt",$szFName & $szExt )
  TrayTip("Consistor - Copying File " & $number & " of " & $fileCount, "Copying file " & $line,"",1)
  FileCopy($line,@ScriptDir)
Wend
;Now delete the file that contains the file paths, keeping the file with filenames
FileDelete("File1.txt")
    ;######################################################C************************opy files from network
;#########################################################################
;Close Filemaker if it is open
If WinExists("FileMaker Pro") Then
WinClose("FileMaker Pro")
WinWaitClose("FileMaker Pro","",8000000)
Sleep (5000)
EndIf
 
;Launch FileMaker
TrayTip("Consistor - Launching Filemaker","Launching",1)
Run("C:\Program Files\FileMaker\FileMaker Pro 11 Advanced\FileMaker Pro Advanced.exe")
WinWait("Open New or Existing File", "", 1000000000)
WinClose("Open New or Existing File", "")
;Delete the existing recover.txt file
FileDelete("Recover.log")
$file = FileOpen("Files.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
   ; MsgBox(0, "Error", "Unable to open file.")
   ; Exit
EndIf
$ErrorCount = 0
;######################################################RUN CONSISTENCY CHECK IN ALL FILES
$number = 0
While 1
  If $fileCount < 1 Then ExitLoop
  $line = FileReadLine($file)
  $number = $number + 1
  $path = @ScriptDir & "\"
  If @error = -1 Then ExitLoop
  ;MsgBox(0, "Line read:", $line)
If StringInStr($line, ".") Then
  TrayTip("Consistor - Processing file " & $number & " of " & $fileCount, "Processing " & $line,"",1)
  WinActivate("FileMaker Pro")
  Send ("!fv")             ;Open the select damaged file window
  WinWaitActive("Damaged File", "", 1)       ;Now activate the damaged file window
  Sleep(50) ;Change to 5000 when moving to the server
  ControlSetText("Select Damaged File", "", "Edit1", $path & $line )  ;Insert the current file path
  ControlClick("Select Damaged File", "", "Button5")      ;Click on the Check consistency button
  Sleep (500)
;Trap for the access privileges error and close the window when it comes up
  If WinExists("FileMaker Pro","The access privileges") Then
   WinClose("FileMaker Pro", "The access privileges")
   WinWaitClose("FileMaker Pro","The access privileges",8000000)
   WinClose("Select Damaged File", "")  ;Close the Select damaged file window
   ;$ErrorCount = $ErrorCount + 1 ;Increase the error count
  Else
   WinWait("","Summary of the",23333333333)
   Sleep (500)
   WinClose("FileMaker Pro", "")
   WinClose("Select Damaged File", "")  ;Close the Select damaged file window
  EndIf
    ;TrayTip("Consistor - Processing file " & $number & " of " & $fileCount, "Processing " & $line,"",1)
EndIf
;Delete the file after
FileDelete($line)
Wend
FileClose($file)
;################################################################################################## COUNT THE NUMBER OF ERRORS ON RECOVER.LOG FILE
$file = FileOpen("Recover.log", 0)
;################################################################# Check if file opened for reading OK
If $file = -1 Then
   ; MsgBox(0, "Error", "Unable to open file.")
    ;Exit
EndIf
 
$ErrorCount = 0
$number = 0
While 1
  $line = FileReadLine($file)
  If @error = -1 Then ExitLoop
  If StringInStr($line, "***") = 0 Then
  $ErrorCount = $ErrorCount + 1
  EndIf
WEnd
;Close the file
FileClose ( $file)
;###################################################################################################################### SEND EMAIL
 
$htmlheader = '<table border="1" cellpadding="5" cellspacing="0" style="font-family: helvetica, arial, sans-serif; border-collapse: collapse; font-size: 14px; border: 1px solid #ccc;">'
$htmlfooter = "</table>"
$file = FileOpen("Recover.log", 0)
$chars = FileRead($file)
$chars = StringReplace($chars, @CR,  '<tr style="background-color: #fff; color: #333;">')
$chars = StringReplace($chars,@TAB,"</td><td>")
$tableHeader =  '<tr style="background-color: #005cab; color: white;"> <td>Date</td> <td>Filename</td><td>Error#</td><td>Comments</td></tr><tr>'
FileClose($file)
 
TrayTip("Sending E-mail","Sending...",1)
#include <INet.au3>;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "example.smtp.com"                            ; address for the smtp-server to use - REQUIRED
$FromName = "Consistor@example.com"                       ; name from who the email was sent
$FromAddress = "testacc@example.com"                           ; address from where the mail should come
$ToAddress = "john@example.com"                         ; destination address of the email - REQUIRED
$Subject =  "Consistency Check - " & $ErrorCount & " errors found" & " (" & $fileCount & " files processed)"                      ; subject from the email - can be anything you want it to be
If $fileCount < 1 Then
$Subject = "FATAL ERROR"
EndIf
$Body = $htmlheader & $tableHeader & $chars  & $htmlfooter                ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                          ; the file you want to attach- leave blank if not needed
$CcAddress = ""                          ; address for cc - leave blank if not needed
$BccAddress = ""                            ; address for bcc - leave blank if not needed
$Importance = "Normal"                    ; Send message priority: "High", "Normal", "Low"
If $fileCount < 1 Then
$Importance= "High"
EndIf
$Username = ""                            ; username for the account used from where the mail gets sent - REQUIRED
$Password = ""                            ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 25                               ; port used for sending the mail
$ssl = 0                                    ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465                            ; GMAIL port used for sending the mail
;~ $ssl=1                                  ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
    MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf
;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                $objEmail.AddAttachment ($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 then $IPPort = 25
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
EndFunc   ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc
 
; Run Chronos
Sleep(5000)
TrayTip("Deleting files from source path ","Deleting...",1)
Sleep(1000)
;Delete the source folder
DirRemove ( $mainPath,1)
;Create the folder again
Sleep(6000)
DirCreate ($mainPath)
TrayTip("Launching Chronos ","Launching...",1)
Sleep(500)
ShellExecute("C:\Documents and Settings\admin\Desktop\Start Chronos.fp7", "", "", "open")
TrayTip("","","")

I was able to get this to work by hardcoding the path of the script file into a variable at the beginning of the script and replacing the @ScriptDir variable with this variable. I'm sure there's gotta be a better solution thought. The script above does not include this change.

Thanks,

Julio

Edited by juliop
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...