Jump to content

Trouble with While and Loops :(


 Share

Recommended Posts

Hi, im pretty new to autoit but seem to be getting the hang of it. however, ive run into some problems with a code im writing at the moment and cant find the solution. Ive been googling for days to find an answer but im stumped. Wonder if anyone here could help. I think this is easy but i just cant get a logical solution in my head lol.

What I am trying to do is write a script that monitors a folder for new txt files (txt file will be named after a variable). If there are no new txt files, then it will sleep for 20 seconds and then check again in a loop until there is a text file in the folder. When there is a txt file in the folder, it will continue on with the script (Which ive already completed),move the txt file and process it. Once the processing is complete, and the script has reached the end, it will return to the beginning of the script to check if there is another text file in the folder. This will be going in a continuous loop so that it processes every text file that enters the folder thats being monitored.

So far ive got this:

$Path = "D:\NewFiles\"
$MovePath = "C:\NewFiles\"
$NewFileLocation = FileFindFirstFile($Path & "*.txt")
$NewFile = $NewFileLocation
$NewFilePath = $Path & $NewFile

While not FileExists($NewFilePath)
sleep(10000)
WEND

If FileExists($NewFilePath) then
FileMove($NewFilePath,$MovePath)
endif
;########## SCRIPT NEEDS TO GO BACK TO THE BEGINNING ######

Im having some trouble with the variables here as FileFindFirstFile is not providing a proper name for the first txt file it finds. I think i can sort this part out though with some more research, but the looping issue is a nightmare. Anyone have any ideas? Thanks to those that help me :idea: Seems like a really helpful place here from what ive read in the other posts :)

Link to comment
Share on other sites

Welcome,

Im having some trouble with the variables here as FileFindFirstFile is not providing a proper name for the first txt file it finds.

The return value of FileFindFirstFile() is a file find/search handle. If the handle is valid, then you can use it in FileFindNextFile() which is the function that returns the filenames.

Hopefully enough comments for you to learn from

$Path = "D:\NewFiles\"
$MovePath = "C:\NewFiles\"
While 1
    $handle_find = FileFindFirstFile($Path & "*.txt")
    ; check the find handle
    If $handle_find <> -1 Then
        ; if handle is valid then loop using FileFindNextFile() to get filenames
        While 2
            $NewFile = FileFindNextFile($handle_find)

            ; next line seems useless so commented it
;~          $NewFile = $NewFileLocation

            $NewFilePath = $Path & $NewFile

            ; the file was just found so this seems useless
;~          While Not FileExists($NewFilePath)
;~              Sleep(10000)
;~          WEnd

            ; the file was just found so it should exist
;~          If FileExists($NewFilePath) Then

            If Not FileMove($NewFilePath, $MovePath) Then
                ; optional alert of failed FileMove()
                MsgBox(0x40000, @ScriptName, _
                        'Unable to move ' & @CRLF & _
                        $NewFilePath & @CRLF & _
                        'to' & @CRLF & _
                        $MovePath _
                )
            EndIf

;~          EndIf

        WEnd
        ; close the find handle
        FileClose($handle_find)
    EndIf
    ; sleep for 20 seconds
    Sleep(20 * 1000)
WEnd

I left some of your suspect lines in as close resemblance to your original code so I could comment on them.

:idea:

Link to comment
Share on other sites

Thanks so much for your help. It would have taken me ages to figure that out :idea: With a little adjustment and removing the error msgbox (it was showing the error message even though it completed the FileMove lol) it works great

$Path = "D:\NewFiles\"
$MovePath = "C:\NewFiles\"
While 1
    $handle_find = FileFindFirstFile($Path & "*.txt")
    ; check the find handle
    If $handle_find <> -1 Then
        ; if handle is valid then loop using FileFindNextFile() to get filenames
        While 2
            $NewFile = FileFindNextFile($handle_find)

            $NewFilePath = $Path & $NewFile

            FileMove($NewFilePath, $MovePath & $NewFile)
           Sleep(10000)

        WEnd
        ; close the find handle
        FileClose($handle_find)
    EndIf
    ; sleep for 20 seconds
    Sleep(20 * 1000)
WEnd

Again, thanks for your help. it now works great :( I knew it was a good idea to post here. Thank you for your help Autoit Ninja :)

Link to comment
Share on other sites

Hi, I seem to be having trouble with the script. The one i did where i took out the msgbox merely disguised that it wasnt working correctly. When i put the msgbox back in to check to see if the loop was working, it sems to not be. The problem is that it keeps on checking the folder for new txt files and rather than sleeping 20 seconds if it doesnt find a txt file, it instead shows the empty msgbox. I can only assume that the script doesnt realise that it is supposed to do nothing when there isnt a txt file and just loop till there is. It seems to want to process something, even if there is no txt file.

Do you have any idea as to why? Thanks for your help :idea:

Link to comment
Share on other sites

Try this. It's modified code from the example here, Function FileFindFirstFile. Change the $Path variable to suit, and uncomment the While and WEnd statements to run this more than once. Uncomment the Sleep statement to wait the extra 19 seconds. The ConsoleWrite line is where it processes what's been found, change that to suit.

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

Global $Path = @ScriptDir
Global $MovePath = "C:\NewFiles\"
Global $NewFileLocation

;While 1
    Search()
;WEnd

Func Search()
    $NewFileLocation = FileFindFirstFile($Path & "\*.txt")
    If $NewFileLocation = -1 Then
        MsgBox(4096, "Error", "No files/directories matched the search pattern", 1)
        ;Sleep(19000)
        FileClose($NewFileLocation)
        Search()
    EndIf
    While 1
        $file = FileFindNextFile($NewFileLocation)
        If @error Then ExitLoop
        ConsoleWrite("FileMove(" & $Path & "\" & $file & ", " & $MovePath & ")" & @LF)
    WEnd
EndFunc

Func Quit()
    FileClose($NewFileLocation)
    Exit
EndFunc

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

Do you have any idea as to why? Thanks for your help :)

Yeah, my fault. I missed the @error check to exit the loop. :idea:

Updated with the added line of "If @error Then ExitLoop" minus the Msgbox() code you seem to not want.

$Path = "D:\NewFiles\"
$MovePath = "C:\NewFiles\"
While 1
    ; get a file find/search handle
    $handle_find = FileFindFirstFile($Path & "*.txt")
    ; check the find handle is valid
    If $handle_find <> -1 Then
        ; loop using FileFindNextFile() to get filenames
        While 1
            $NewFile = FileFindNextFile($handle_find)
            ; exit loop on error (no more txt files)
            If @error Then ExitLoop
            ; build a fullpath
            $NewFilePath = $Path & $NewFile
            ; move the file found
            FileMove($NewFilePath, $MovePath)
        WEnd
        ; close the find handle
        FileClose($handle_find)
    EndIf
    ; sleep for 20 seconds
    Sleep(20 * 1000)
WEnd
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...