Jump to content

Autoit and process


ludo10
 Share

Recommended Posts

hello,

i have to check somes folder to see if file arrived.

I does it with a batch that i scheduled every 5 minutes.

so my question : can i make an .exe with autoit to stay as a process to check every time my folder?

i used autoit to do the rest of the job (compare file, and sending it to SAP)

Thank You

Link to comment
Share on other sites

ok, you give me the sleep that my exe run in a loop.

but i'd like an "eternal" loop in this case no?

i dont want anymore (if it is possible) to schedule my batch (or autoit script) but to run it as a process in memory (or as a service)

Edited by ludo10
Link to comment
Share on other sites

ok, you give me the sleep that my exe run in a loop.

but i'd like an "eternal" loop in this case no?

i dont want anymore (if it is possible) to schedule my batch (or autoit script) but to run it as a process in memory (or as a service)

Here is something to get you started. Read also the comments at the end !!

#include <File.au3>
#include <Array.au3>

Opt("TrayIconHide", 1)


; Eventlogging globals

Global Const $EVT_SUCCESS = 0
Global Const $EVT_ERROR = 1
Global Const $EVT_WARNING = 2
Global Const $EVT_INFORMATION = 4
Global Const $EVT_AUDIT_SUCCESS = 8
Global Const $EVT_AUDIT_FAILURE = 16

Global $EVT_HANDLE = _EventLogInit()

_EventLogWrite($EVT_INFORMATION, "Filewatcher: Script Started...")


;============== MAIN ===========================================
$watchdir = "c:\test"

If Not FileExists($watchdir) Then
    _EventLogWrite($EVT_INFORMATION, "Filewatcher: Watch dir does not exist...")
    Exit (1)
EndIf

While 1
    Sleep(20000)
    $newfiles = _CheckForNewFiles($watchdir)
    _ProcessFiles($watchdir, $newfiles)
WEnd

Func _CheckForNewFiles($path)
    Return _FileListToArray($path)
EndFunc   ;==>_CheckForNewFiles

Func _ProcessFiles($path, ByRef $newfiles)
    
    Local $backupdir = $path & "\backup"
    DirCreate($backupdir)
    For $n = 1 To $newfiles[0]
        FileMove($path & "\" & $newfiles[$n], $backupdir)
        _EventLogWrite($EVT_INFORMATION, "Filewatcher: Moved file " & $path & "\" & $newfiles[$n])
    Next
    
EndFunc   ;==>_ProcessFiles


Func _EventLogInit()
    Local $WshShell = ObjCreate("WScript.Shell")
    If @error Or $WshShell = 0 Then $glb_eventlog_problem = 1
    Return $WshShell
EndFunc   ;==>_EventLogInit

Func _EventLogWrite($type, $msg)
    $EVT_HANDLE.LogEvent ($type, $msg)
EndFunc   ;==>_EventLogWrite


; HOWTO install the file as Windows service
; 1.) Donwload and install Windows Server 2003 Resource Kit Tools
;     http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en
; 2.) Compile your AU3 script into an exe and place it in this folder
;     c:\program files\filewatcher\filewatcher.exe
; 3.) Install srvany.exe as a service, give the service the name: filewatcher
;
;     C:\Program Files\Windows Resource Kits\Tools\instsrv.exe filewatcher "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
; 4.) Configure the registry for srvany.exe
;     add this to the registry
;
;       REGEDIT4
;
;       [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\filewatcher\Parameters]
;       "Application"="c:\\Program Files\\filewatcher\\filewatcher.exe"
;       "AppDirectory"=""
;       "AppParameters"=""
; 5.) Start your service and watch how the files in c:\test disappear every 20 seconds :-))
;
;     net start filewatcher
;
; ******  !!!  WARNING !!!  *********
;
; DO NOT USE ANY GUI FUNCTIONS IN THE CODE WHEN IS RUNNING AS A SERVICE. 
; (EVEN NO MsgBox(), InputBox(), etc...). THAT WILL SIMPLY BLOCK THE
; PROCESS AND IT WILL HANG FOREVER (or until you stop the service).
;
;

EDIT: Added Event logging

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

thank you, i'll try this week end and tell you if it work :whistle:

it works, otherwise I would not have posted the code!

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

_CheckForNewFiles($path)
Kind of a redundant function isn't it?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

_CheckForNewFiles($path)
Kind of a redundant function isn't it?
only in the sample. It's just a template for him to start with. Maybe he want's to do some filtering later on, so it's better to have a function in the first place, otherwise he might start cluttering the while loop....

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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