Jump to content

Autoit Service problem!


Recommended Posts

Good evening everyone. Hope you are all fine! I'm facing a problem while trying to run my script as service.

It is a script which every 10 seconds reads a directory splits some text files, analyzes them and then moves

them to a mysql database!

Can you please let me know what maybe wrong?I do not receive any errors and i'm trying to find out what I'm

doing wrong!

Here is the code:

#Include <File.au3>
#Include <Array.au3>
#include "MySQL.au3"

;Retrieve Directories
$ReadMsgDir = IniRead(@ScriptDir&"\Configuration.ini", "Directories", "Read Messages Directory", "NotFound")
$StrdMsgDir = IniRead(@ScriptDir&"\Configuration.ini", "Directories", "Stored Messages Directory", "NotFound")
$timer      = IniRead(@ScriptDir&"\Configuration.ini", "Timers", "Time between checks in ms", "NotFound")
$TrshMsgDir = IniRead(@ScriptDir&"\Configuration.ini", "Directories", "Trash Messages Directory", "NotFound")

;Retrieve Database Credentials and details
$UserName = IniRead(@ScriptDir&"\Configuration.ini", "Database", "Username", "NotFound")
$Password = IniRead(@ScriptDir&"\Configuration.ini", "Database", "Password", "NotFound")
$Database = IniRead(@ScriptDir&"\Configuration.ini", "Database", "Database", "NotFound")
$MySQLServerName = IniRead(@ScriptDir&"\Configuration.ini", "Database", "Server", "NotFound")

;Retrieve other options
$Storeval = IniRead(@ScriptDir&"\Configuration.ini", "Options", "Store mode", "NotFound")

;Application code stars here
While 1

$begin = TimerInit()

sleep($timer)

$dif = TimerDiff($begin)


If FileExists(@Scriptdir&"\Print*" )  Then


;Scan for empty files and delete them before starting any engine

$initfiles=_FileListToArray(@ScriptDir,"Print*")

for $g=1 to $initfiles[0]
    $initsize=filegetsize($initfiles[$g])
    
    If $initsize=0 then
        filedelete($initfiles[$g])
    Endif
Next

If FileExists(@Scriptdir&"\Print*" )  Then

;Start Message Divider Engine

$rawfiles=_FileListToArray(@Scriptdir,"Print*")

$v=0

For $x=1 to $rawfiles[0]

Dim $tpffile
$a=_FileReadToArray($rawfiles[$x],$tpffile)

for $i=1 to $tpffile[0]

    switch $tpffile[$i]
        case ""
        case "**" 
        filewriteline("txf"&$v&".txt",$tpffile[$i])
        $v=$v+1
        case Else
        filewriteline("txf"&$v&".txt",$tpffile[$i])
    Endswitch
    
Next

filecopy($rawfiles[$x],$StrdMsgDir&"*.txt")
filedelete($rawfiles[$x])

Next

;Terminate Message Divider Engine

$inputfiles=_FileListToArray(@Scriptdir,"txf*")

;Start Identification procedure

for $d=1 to $inputfiles[0]
    
    
    $inline=filereadline($inputfiles[$d],1)
    $inlinelen=StringLen($inline)
    
    ;Handle exception of possible wrong message
    if $inlinelen<26 Then
    
    filecopy($inputfiles[$d],$TrshMsgDir&$inline&".txt")
    filedelete($inputfiles[$d])
        
    ;Handle the file if it is a valid format message
    ElseIf $inlinelen=26 Then
    
    $msgdet=StringSplit($inline, " ")
    $sendline=filereadline($inputfiles[$d],2)
    $prio=Stringleft($sendline,2)
    $destination=StringRight($sendline,7)
    

    $orgline=filereadline($inputfiles[$d],3)
    $org1=StringLeft($orgline,8)
    $originator=StringRight($org1,7)
    $msgtypeln=filereadline($inputfiles[$d],4)
    $lenght=StringLen($msgtypeln)
    
    If $lenght = 3 Then
    $msgtype=$msgtypeln

    ElseIf $lenght <> 3 Then
    $msgtype="TST"
    EndIf

    $outfile= $msgdet[4]&" - "&$msgdet[5]&" - "&$originator&" - "&$destination&" - "&$msgtype&" - "&$msgdet[1]&".txt"

;Prepare data for mySQL export

$explines=_FileCountLines($inputfiles[$d])

$html2=""

For $p=1 to $explines
    
$htmlline1=filereadline($inputfiles[$d],$p)

$html2=$html2&$htmlline1&"<br />"

Next

$html="<code>"&$html2&"</code>"

;Edit date format

$date1 = Stringleft($msgdet[4],5)
$day = Stringleft($date1,2)
$date3 = StringRight($date1,3)

switch $date3
    case "JAN"
    $month="01"
    case "FEB"
    $month="02"
    case "MAR"
    $month="03"
    case "APR"
    $month="04"
    case "MAY"
    $month="05"
    case "JUN"
    $month="06"
    case "JUL"
    $month="07"
    case "AUG"
    $month="08"
    case "SEP"
    $month="09"
    case "OCT"
    $month="10"
    case "NOV"
    $month="11"
    case "DEC"
    $month="12"
EndSwitch

$sqldate = @YEAR & "-" & $month & "-" & $day

;Edit time format

$sqltime1=StringLeft($msgdet[5],4)
$sqltime2=StringLeft($sqltime1,2)
$sqltime3=StringRight($sqltime1,2)
$sqltime=$sqltime2&":"&$sqltime3

$sqldatetime=$sqldate&" "&$sqltime

$SQLInstance = _MySQLConnect($UserName,$Password,$Database,$MySQLServerName)

$uentry= "INSERT INTO master.main (`MID` ,`MRD` ,`MRT` ,`MRDT`,`ORG`,`DES` ,`TYPE` ,`PRIO` ,`MSG` ,`READ`) VALUES ( '"&$msgdet[1]&"', '"&$sqldate&"', '"&$sqltime&"', '"&$sqldatetime&"', '"&$originator&"', '"&$destination&"', '"&$msgtype&"', '"&$prio&"', '"&$html&"', 'NOP' );"

_Query($sqlinstance,$uentry)

_MySQLEnd($SQLInstance)


Switch $Storeval
    case 0
    consolewrite("File proccessed and deleted."&@LF)
    case 1
    filecopy($inputfiles[$d],$ReadMsgDir&$outfile)
    consolewrite("File proccessed and stored in a new format."&@LF)
EndSwitch

filedelete($inputfiles[$d])
        
    EndIf
    
Next


EndIf
Endif

WEnd

Any help is really appreciated!

Link to comment
Share on other sites

Can you please let me know what maybe wrong?I do not receive any errors and i'm trying to find out what I'm

doing wrong!

Hello,

Sorry, I've no time to check your script so I don't really know what's wrong but I can see you are using FileReadLine & FileWriteLine functions but you never open the file in the right mode (read or write mode) nor close it then. Try opening and closing the files correctly with FileOpen and FileClose functions... take a look to help file to see the examples and the available modes.

Edit: I know if you use filenames instead filehandles you don't need to open/close the file because of the function do it itself but it is slower and maybe 10 seconds are not enough for the script to complete. Try to debug it so you can see where the script hangs.

Cheers,

sahsanu

Edited by sahsanu
Link to comment
Share on other sites

I know if you use filenames instead filehandles you don't need to open/close the file because of the function do it itself but it is slower and maybe 10 seconds are not enough for the script to complete. Try to debug it so you can see where the script hangs.

Well, I've never thought that this could be a problem! I'll do the appropriate changes and let you know! Thanks!

Have you read this?

http://www.autoitscript.com/wiki/FAQ#How_can_I_run_my_script_as_a_service.3F

Yes, I've read it and it works fine for me, but I prefer to have a "direct" service and not a service generated by a third party app! Thanks for

your reply. In case nothing works I will keep working with this app!

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...