Sign in to follow this  
Followers 0
Ace08

Exe stops working with scheduled task

4 posts in this topic

hi there i have this script below which gets all the files inside the sms folder then removes positive(+), negative(-), and space( ) on a specific part of the file. the sms folder updates itself every 10 mins so i decided to put the script into the scheduled task, its running nicely for 20 mins so i think it'll be ok with that but after 3 days i've noticed the script is not running i've tried to manualy run the script inside the scheduled task but it wont however my other apps were running perfectly, i tried to restarted my PC and all is working again but after another 2 days the script wont run again. so i was thinking that maybe my script has some issues since all my other apps are working while this one isn't. :unsure:

Local $NewLine,$test

;------------------------------------------------------------------
;Year, Month, Day
;------------------------------------------------------------------
    $DD = @MDAY
    $MM = @MON
    $YYYY = @YEAR
    $TDate = $MM & "-" & $DD & "-" & $YYYY
;------------------------------------------------------------------
;Folder Paths
;------------------------------------------------------------------
    $Output = @ScriptDir & "\Output"
    $Sms = @ScriptDir & "\Sms"
    $Processed = @ScriptDir & "\Processed"
    $ProcessedFolder = @ScriptDir & "\Processed\" & $TDate
    If not FileExists($Output) Then
        DirCreate($Output)
    EndIf
    If not FileExists($Sms) Then
        DirCreate($Sms)
    EndIf
    If not FileExists($Processed) Then
        DirCreate($Processed)
    EndIf
    If not FileExists($ProcessedFolder) Then
        DirCreate($ProcessedFolder)
    EndIf

$hFilesFolders = _FileListToArrayEx(@ScriptDir & "\Sms", '*.TXT')
For $i = 1 to UBound($hFilesFolders)-1
    $file = FileOpen($hFilesFolders[$i], 0)
    If $file = -1 Then
        Exit
    EndIf
    While 1
        $strLine = FileReadLine($file)
        If @error = -1 Then ExitLoop
        if $strLine = "" then ExitLoop
        if $strLine = " " then ExitLoop
        $Field = StringSplit($strLine,",",1)
        $Field[1] = StringReplace($Field[1], " ", "")
        $Field[1] = StringReplace($Field[1], "+", "")
        $Field[1] = StringReplace($Field[1], "-", "")
        For $a = 1 to UBound($Field)-1
            If $a = UBound($Field)-1 Then
                $NewLine = $NewLine & $Field[$a] & @CRLF
            Else
                $NewLine = $NewLine & $Field[$a] & ","
            EndIf
        Next
    WEnd
    $filename = StringSplit($hFilesFolders[$i], "\",1)
    $txtfile = $filename[UBound($filename)-1]

    $NewFile = FileOpen($Output & "\" & $txtfile, 10)
        FileWrite($NewFile, $NewLine)
    FileClose($NewFile)
    FileMove($hFilesFolders[$i],$ProcessedFolder & "\" & $txtfile)
    while FileExists($ProcessedFolder & "\" & $txtfile) = 0
        Sleep(80)
    WEnd
    $NewLine = ""
Next
Func _FileListToArrayEx($s_path, $s_mask = "*.*", $i_flag = 0, $s_exclude = -1, $f_recurse = True, $f_full_path = True)

    If FileExists($s_path) = 0 Then Return SetError(1, 1, 0)

    ; Strip trailing backslash, and add one after to make sure there's only one
    $s_path = StringRegExpReplace($s_path, "[\\/]+\z", "") & "\"

    ; Set all defaults
    If $s_mask = -1 Or $s_mask = Default Then $s_mask = "*.*"
    If $i_flag = -1 Or $i_flag = Default Then $i_flag = 0
    If $s_exclude = -1 Or $s_exclude = Default Then $s_exclude = ""

    ; Look for bad chars
    If StringRegExp($s_mask, "[/:><\|]") Or StringRegExp($s_exclude, "[/:><\|]") Then
        Return SetError(2, 2, 0)
    EndIf

    ; Strip leading spaces between semi colon delimiter
    $s_mask = StringRegExpReplace($s_mask, "\s*;\s*", ";")
    If $s_exclude Then $s_exclude = StringRegExpReplace($s_exclude, "\s*;\s*", ";")

    ; Confirm mask has something in it
    If StringStripWS($s_mask, 8) = "" Then Return SetError(2, 2, 0)
    If $i_flag < 0 Or $i_flag > 2 Then Return SetError(3, 3, 0)

    ; Validate and create path + mask params
    Local $a_split = StringSplit($s_mask, ";"), $s_hold_split = ""
    For $i = 1 To $a_split[0]
        If StringStripWS($a_split[$i], 8) = "" Then ContinueLoop
        If StringRegExp($a_split[$i], "^\..*?\..*?\z") Then
            $a_split[$i] &= "*" & $a_split[$i]
        EndIf
        $s_hold_split &= '"' & $s_path & $a_split[$i] & '" '
    Next
    $s_hold_split = StringTrimRight($s_hold_split, 1)
    If $s_hold_split = "" Then $s_hold_split = '"' & $s_path & '*.*"'

    Local $i_pid, $s_stdout, $s_hold_out, $s_dir_file_only = "", $s_recurse = "/s "
    If $i_flag = 1 Then $s_dir_file_only = ":-d"
    If $i_flag = 2 Then $s_dir_file_only = ":D"
    If Not $f_recurse Then $s_recurse = ""

    $i_pid = Run(@ComSpec & " /c dir /b " & $s_recurse & "/a" & $s_dir_file_only & " " & $s_hold_split, "", @SW_HIDE, 4 + 2)

    While 1
        $s_stdout = StdoutRead($i_pid)
        If @error Then ExitLoop
        $s_hold_out &= $s_stdout
    WEnd

    $s_hold_out = StringRegExpReplace($s_hold_out, "\v+\z", "")
    If Not $s_hold_out Then Return SetError(4, 4, 0)

    ; Parse data and find matches based on flags
    Local $a_fsplit = StringSplit(StringStripCR($s_hold_out), @LF), $s_hold_ret
    $s_hold_out = ""

    If $s_exclude Then $s_exclude = StringReplace(StringReplace($s_exclude, "*", ".*?"), ";", "|")

    For $i = 1 To $a_fsplit[0]
        If $s_exclude And StringRegExp(StringRegExpReplace( _
            $a_fsplit[$i], "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\Q" & $s_exclude & "\E") Then ContinueLoop
        If StringRegExp($a_fsplit[$i], "^\w:[\\/]+") = 0 Then $a_fsplit[$i] = $s_path & $a_fsplit[$i]
        If $f_full_path Then
            $s_hold_ret &= $a_fsplit[$i] & Chr(1)
        Else
            $s_hold_ret &= StringRegExpReplace($a_fsplit[$i], "((?:.*?[\\/]+)*)(.*?\z)", "$2") & Chr(1)
        EndIf
    Next

    $s_hold_ret = StringTrimRight($s_hold_ret, 1)
    If $s_hold_ret = "" Then Return SetError(5, 5, 0)

    Return StringSplit($s_hold_ret, Chr(1))
EndFunc

Work smarter not harder.My First Posted Script: DataBase

Share this post


Link to post
Share on other sites



I'd only be guessing here, but I'd guess that it would be the section below that's killing your script.

If $file = -1 Then
        Exit
    EndIf

If it ever encounters an error with the FileOpen command, it exits the whole script. You could easily encounter an error if a file is in the process of being copied into the folder at the same time that your script is trying to open it, this will cause it to error out, and your script will exit at that point.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'd only be guessing here, but I'd guess that it would be the section below that's killing your script.

If $file = -1 Then
        Exit
    EndIf

If it ever encounters an error with the FileOpen command, it exits the whole script. You could easily encounter an error if a file is in the process of being copied into the folder at the same time that your script is trying to open it, this will cause it to error out, and your script will exit at that point.

hmmm i think you're right will check on this one.... one more question if I run the same exe twice will it affect the other? because i was also thinking that if the script isn't finished running until the next schedule then scheduled task will try to run the same script again can it cause an error? Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Share this post


Link to post
Share on other sites

hmmm i think you're right will check on this one.... one more question if I run the same exe twice will it affect the other? because i was also thinking that if the script isn't finished running until the next schedule then scheduled task will try to run the same script again can it cause an error?

For only 1 instance of your script, add this :

#Include <Misc.au3>

If Not _Singleton ( @ScriptName, 1 ) Then Exit

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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