Sign in to follow this  
Followers 0
Andy2k14

Continuously search for file

4 posts in this topic

Hi all,

Still learning AutoIt, can anyone recommend how i can do the following:

Continuously search for a .txt file in a designated directory

The search will continue indefinatley until the user closes the program or the file is found

If file found open file and read 1st line of txt file

close file

end program

Ive been looking at using the "read first line" function but having difficulty with the syntax format. The example is not (in my opinion) noob friendly.

Any help appreciated

 

Share this post


Link to post
Share on other sites



Andy2k14,

 

Ive been looking at using the "read first line" function

Are you speaking about FileReadLine? :huh:

If so, then the default is to read the first line, so a simple:

FileReadLine("filename")
will do what you want. ;)

As to detecting the file in the first place, I suggest FileExists in an infinite loop with a suitable Sleep to prevent the CPU overheating - ExitLoop will get you out of the loop once the file has been found and read. See how you get on with that and come back if you run into problems. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Andy2k14

I'm in a great mood today so here is some of my code but use it to learn and modify it as you wish.

Dim $file, $match, $openfile, $readline ;declaration

$file = "c:\temp\file.txt"; file path and name

While 1
    $match = FileFindFirstFile($file);check if file exists
    if $match = -1 Then exiterror();ops... is not there
    $openfile = FileOpen($file, 0) ; opening in read mode
    If $openfile = -1 Then MsgBox(16, "Error", "Error opening file");what is happening with this file... i'll have to check it!
    $readline = FileReadLine($file, 1) ; read 1st line
    Select
        Case @error = 1;check help in autoit
            MsgBox(16, "Error", "File not open in read mode");File read failed due open file
        Case @error = -1;check help in autoit
            MsgBox(48, "Error", "File is empty");File read failed due no data in file
    EndSelect
    exitnoerror();everything went well let's exit with sucess
sleep (50)
WEnd


Func exiterror()
    MsgBox(16, "Search File", "file " & $file & " not found"); file not found, here you can choose to continue loop or not
    Exit;exiting script
EndFunc   ;==>exiterror

Func exitnoerror()
    MsgBox(64, "Search File", "file " & $file & " found" & @CRLF & "Text : " & $readline) ; text of the 1st line here
    Exit
EndFunc   ;==>exitnoerror

Cheers

Edited by November

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

you simply could do something like this:

Global $fExit= False

HotKeySet("{ESC}", "_Exit")


Local $sFileData=""
Do
    Sleep(30)
    FileReadLine("File.exe",1)
Until Not @error or $fExit



Func _Exit()
    $fExit=True
EndFunc

Saludos

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