Sign in to follow this  
Followers 0
Peaky

Creating Infinite Loop

7 posts in this topic

#1 ·  Posted

Hi All,

I created a script to check a folder every 5 minutes and if there is a new file, it would convert to word and upload to a translation site.

I have completed that script and tested it a couple time. However, I am having trouble creating an infinite loop.

 

I have tried "While 1 ... WEnd"  and "Do ...Until"  but the script would only run for 0.7 secs and stopped. 

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\Peak Reliance\Desktop\Final_Project_Translate.au3" /UserParams    
+>01:16:17 Starting AutoIt3Wrapper v.17.224.935.0 SciTE v.3.7.3.0   Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Peak Reliance\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Peak Reliance\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\Peak Reliance\Desktop\Final_Project_Translate.au3
+>01:16:17 AU3Check ended.rc:0
>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\Peak Reliance\Desktop\Final_Project_Translate.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
"C:\Users\Peak Reliance\Desktop\Final_Project_Translate.au3" (16) : ==> Subscript used on non-accessible variable.:
For $i = 1 To $FileList[0]
For $i = 1 To $FileList^ ERROR
->01:16:17 AutoIt3.exe ended.rc:1
+>01:16:17 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 0.7095
 

 

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

$sou_path="C:\Mega\1-Service\1-Available\Translation\Chi to Eng"
$FileList = _FileListToArray($sou_path, '*.pdf', 1, True)
Global $oSelect
Global $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
Global $DLDrive = "", $DLDir = "", $DLFileName = "", $DLExtension = ""
Global $Endless = 1         ;beginning of the loop

Do
$search = FileFindFirstFile($sou_path)
    If $search = -1 Then
Else
For $i = 1 To $FileList[0]
    Run("C:\Program Files (x86)\ABBYY FineReader 14\FineReader.exe")
    WinWait("ABBYY FineReader 14 Standard")
    ControlClick('ABBYY FineReader 14 Standard', '', 'Button27')
    WinWait('Select Files to Convert to Microsoft Word')
    ControlSetText("Select Files to Convert to Microsoft Word","","Edit1", $FileList[$i])
    ControlClick('Select Files to Convert to Microsoft Word', '', 'Button1')
    WinWait('ABBYY FineReader 14 Standard')
    ControlClick('ABBYY FineReader 14 Standard', '', 'Button49')
    WinWait('Save document as')
    ControlClick('Save document as', '', 'Edit1')
    ControlSetText('Save document as', '', 'Edit1', _RemoveFileExt($FileList[$i])&".docx")
    ControlClick('Save document as', '', 'Button5')
    WinWait("[CLASS:OpusApp]")
    WinClose("[CLASS:OpusApp]")
    WinClose("[CLASS:AWL:6F6D0000:8:10003:a5100da2:9009a3:1a3040f]")
    WinClose("[CLASS:AWL:6F6D0000:8:10003:3f10145d:5c11bb:5b11283]")
    WinClose("[Class:AWL:6F6D0000:8:10003:9c100bae:1ef0601:3021219]")
    WinClose("[CLASS:AWL:6F6D0000:8b:10003:0:0:0]")
    WinClose('ABBYY FineReader 14 Standard')

$sURL = "https://www.onlinedoctranslator.com/translationform"
$oIE = _IECreate($sURL)
$hIE = _IEPropertyGet($oIE, "hwnd")
Global $oForm = _IEFormGetObjByName($oIE, "translation-form")
$oButton = _IETagNameGetCollection($oIE, "Button", 0)

 If @error = 0 And IsObj($oButton) Then _IEAction($oButton, "focus")
 ControlSend($hIE, "", "", "{ENTER}")


While 1
    If WinWait("[CLASS:#32770]", "", 2) Then
        ConsoleWrite("Debug: Detected update prompt window" & @LF)
        $hWin = WinGetHandle("[CLASS:#32770]","")
        WinActivate($hWin)
        Sleep(1000)
       ControlFocus("Choose File to Upload","","Edit1")
        Sleep(1000)
       ControlSetText("Choose File to Upload","","Edit1",_RemoveFileExt($FileList[$i])&".docx")
        Sleep(1000)
       ControlClick("Choose File to Upload","","Button1")
        Sleep(1000)

$Translation = _IEFormGetObjByName($oIE, "translation-form")
$TranslationFrom = _IEFormElementGetObjByName($Translation, "from")
_IEFormElementOptionSelect($TranslationFrom, "Chinese", 1, "byText")
$TranslationTo = _IEFormElementGetObjByName($Translation, "to")
_IEFormElementOptionSelect($TranslationTo, "English", 1, "byText")

  Sleep(30000)

_IEFormSubmit($oForm)

  Sleep(60000)

Local $hIE2 = WinGetHandle("[Class:IEFrame]")
Local $hCtrl = ControlGetHandle($hIE2, "", "[ClassNN:DirectUIHWND1]")

ControlSend($hIE2, "", $hCtrl, "!S")

 Sleep(10000)

        ExitLoop
    Else
        ConsoleWrite("Debug: Update prompt window not detected yet..." & @LF)
    EndIf
WEnd

Sleep(500)


_IEQuit($oIE)
Sleep(5000)

Global $aPathSplit = _PathSplit(_FindClientName($FileList[$i]), $sDrive, $sDir, $sFileName, $sExtension)
If FileExists("C:\Mega\1-Service\3-Deliverable\"&$sFileName) = 0 Then
    DirCreate("C:\Mega\1-Service\3-Deliverable\"&$sFileName)
EndIf

Global $ClientFolder = "C:\Mega\1-Service\3-Deliverable\"&$sFileName
Global $DLFolder = _PathSplit(_RemoveFileExt($FileList[$i]), $DLDrive, $DLDir, $DLFileName, $DLExtension)

FileMove($FileList[$i], $ClientFolder)
FileMove(_RemoveFileExt($FileList[$i])&".docx", $ClientFolder)
FileMove("C:\Users\Peak Reliance\Downloads\"&$DLFileName&".zh-CN.en.docx", $ClientFolder)

Next
EndIf
Sleep(2000)
Until $Endless = 2          ;Ending the loop


Func _RemoveFileExt($string)
    Return StringLeft($string,StringInStr($string,".",Default,-1)-1)
EndFunc
Func _FindClientName($string)
    Return StringLeft($string,StringInStr($string,"-",Default,-1)-1)
EndFunc

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

You should check to see if _FileListToArray returns an array of files before trying to process it. Plus, you only gather the list of files in the folder once so you can't tell if anything changes in the folder.


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

7 hours ago, BrewManNH said:

You should check to see if _FileListToArray returns an array of files before trying to process it. Plus, you only gather the list of files in the folder once so you can't tell if anything changes in the folder.

Please bear with me if my questions sound silly but:

1. Why do I need to check whether _FileListToArray returns anything? If there's nothing, wouldn't the array just be blank/void, and the "For..Next" function will check again in every loop?

2. Since every file in that particular folder is processed in each loop, wouldn't it only need to check once? Eg. Folder contains File A, & B. Script processes A&B, moved them to another folder, and check again in the next loop and sees C. It would work on C then.

 

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

1 hour ago, Peaky said:

1. Why do I need to check whether _FileListToArray returns anything? If there's nothing, wouldn't the array just be blank/void, and the "For..Next" function will check again in every loop?

No, see the doc

 

1 hour ago, Peaky said:

2. Since every file in that particular folder is processed in each loop, wouldn't it only need to check once? Eg. Folder contains File A, & B. Script processes A&B, moved them to another folder, and check again in the next loop and sees C. It would work on C then.

Again, no.

Your _filelisttoarray should be inside of your loop. 

something like this...(not tested)

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

$sou_path = "C:\Mega\1-Service\1-Available\Translation\Chi to Eng"
;$FileList = _FileListToArray($sou_path, '*.pdf', 1, True)      ; should be inside loop
Global $oSelect
Global $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
Global $DLDrive = "", $DLDir = "", $DLFileName = "", $DLExtension = ""
Global $Endless = 1 ;beginning of the loop

Do
    $FileList = _FileListToArray($sou_path, '*.pdf', 1, True)   ; moved inside loop
    If @ERROR then                                              ; see doc for return values
    Else
        For $i = 1 To $FileList[0]
            Run("C:\Program Files (x86)\ABBYY FineReader 14\FineReader.exe")
            WinWait("ABBYY FineReader 14 Standard")
            ControlClick('ABBYY FineReader 14 Standard', '', 'Button27')
            WinWait('Select Files to Convert to Microsoft Word')
            ControlSetText("Select Files to Convert to Microsoft Word", "", "Edit1", $FileList[$i])
            ControlClick('Select Files to Convert to Microsoft Word', '', 'Button1')
            WinWait('ABBYY FineReader 14 Standard')
            ControlClick('ABBYY FineReader 14 Standard', '', 'Button49')
            WinWait('Save document as')
            ControlClick('Save document as', '', 'Edit1')
            ControlSetText('Save document as', '', 'Edit1', _RemoveFileExt($FileList[$i]) & ".docx")
            ControlClick('Save document as', '', 'Button5')
            WinWait("[CLASS:OpusApp]")
            WinClose("[CLASS:OpusApp]")
            WinClose("[CLASS:AWL:6F6D0000:8:10003:a5100da2:9009a3:1a3040f]")
            WinClose("[CLASS:AWL:6F6D0000:8:10003:3f10145d:5c11bb:5b11283]")
            WinClose("[Class:AWL:6F6D0000:8:10003:9c100bae:1ef0601:3021219]")
            WinClose("[CLASS:AWL:6F6D0000:8b:10003:0:0:0]")
            WinClose('ABBYY FineReader 14 Standard')

            $sURL = "https://www.onlinedoctranslator.com/translationform"
            $oIE = _IECreate($sURL)
            $hIE = _IEPropertyGet($oIE, "hwnd")
            Global $oForm = _IEFormGetObjByName($oIE, "translation-form")
            $oButton = _IETagNameGetCollection($oIE, "Button", 0)

            If @error = 0 And IsObj($oButton) Then _IEAction($oButton, "focus")
            ControlSend($hIE, "", "", "{ENTER}")


            While 1
                If WinWait("[CLASS:#32770]", "", 2) Then
                    ConsoleWrite("Debug: Detected update prompt window" & @LF)
                    $hWin = WinGetHandle("[CLASS:#32770]", "")
                    WinActivate($hWin)
                    Sleep(1000)
                    ControlFocus("Choose File to Upload", "", "Edit1")
                    Sleep(1000)
                    ControlSetText("Choose File to Upload", "", "Edit1", _RemoveFileExt($FileList[$i]) & ".docx")
                    Sleep(1000)
                    ControlClick("Choose File to Upload", "", "Button1")
                    Sleep(1000)

                    $Translation = _IEFormGetObjByName($oIE, "translation-form")
                    $TranslationFrom = _IEFormElementGetObjByName($Translation, "from")
                    _IEFormElementOptionSelect($TranslationFrom, "Chinese", 1, "byText")
                    $TranslationTo = _IEFormElementGetObjByName($Translation, "to")
                    _IEFormElementOptionSelect($TranslationTo, "English", 1, "byText")

                    Sleep(30000)

                    _IEFormSubmit($oForm)

                    Sleep(60000)

                    Local $hIE2 = WinGetHandle("[Class:IEFrame]")
                    Local $hCtrl = ControlGetHandle($hIE2, "", "[ClassNN:DirectUIHWND1]")

                    ControlSend($hIE2, "", $hCtrl, "!S")

                    Sleep(10000)

                    ExitLoop
                Else
                    ConsoleWrite("Debug: Update prompt window not detected yet..." & @LF)
                EndIf
            WEnd

            Sleep(500)


            _IEQuit($oIE)
            Sleep(5000)

            Global $aPathSplit = _PathSplit(_FindClientName($FileList[$i]), $sDrive, $sDir, $sFileName, $sExtension)
            If FileExists("C:\Mega\1-Service\3-Deliverable\" & $sFileName) = 0 Then
                DirCreate("C:\Mega\1-Service\3-Deliverable\" & $sFileName)
            EndIf

            Global $ClientFolder = "C:\Mega\1-Service\3-Deliverable\" & $sFileName
            Global $DLFolder = _PathSplit(_RemoveFileExt($FileList[$i]), $DLDrive, $DLDir, $DLFileName, $DLExtension)

            FileMove($FileList[$i], $ClientFolder)
            FileMove(_RemoveFileExt($FileList[$i]) & ".docx", $ClientFolder)
            FileMove("C:\Users\Peak Reliance\Downloads\" & $DLFileName & ".zh-CN.en.docx", $ClientFolder)

        Next
    EndIf
    Sleep(2000)
Until $Endless = 2 ;Ending the loop


Func _RemoveFileExt($string)
    Return StringLeft($string, StringInStr($string, ".", Default, -1) - 1)
EndFunc   ;==>_RemoveFileExt
Func _FindClientName($string)
    Return StringLeft($string, StringInStr($string, "-", Default, -1) - 1)
EndFunc   ;==>_FindClientName

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#5 ·  Posted

1 hour ago, Peaky said:

1. Why do I need to check whether _FileListToArray returns anything? If there's nothing, wouldn't the array just be blank/void, and the "For..Next" function will check again in every loop?

As @kylomas stated, please read the help file for the the return values for the function. The For/Next loop will only check the returned result the first time it runs, and if $Filelist isn't an array, you'll immediately get an error. Again, read the help file as to how For/Next works.

1 hour ago, Peaky said:

2. Since every file in that particular folder is processed in each loop, wouldn't it only need to check once? Eg. Folder contains File A, & B. Script processes A&B, moved them to another folder, and check again in the next loop and sees C. It would work on C then.

I thought you were looking for new/added files with this? This function will only check the files that were in there the first time you used _FileListToArray, once that's done _FLTA isn't updated ever again, so it's looking for the same files that were there when you first ran it, so if you want it to keep checking the files in the folder, you have to use it every time you finish processing what was already there. So, put it inside the loop. The code is executed from the first line to the last, unless there's a function call in there or a loop of some kind, so having the _FLTA outside the loop, it's only executed once.


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

#6 ·  Posted

10 hours ago, kylomas said:

No, see the doc

 

Again, no.

Your _filelisttoarray should be inside of your loop. 

something like this...(not tested)

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

$sou_path = "C:\Mega\1-Service\1-Available\Translation\Chi to Eng"
;$FileList = _FileListToArray($sou_path, '*.pdf', 1, True)      ; should be inside loop
Global $oSelect
Global $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
Global $DLDrive = "", $DLDir = "", $DLFileName = "", $DLExtension = ""
Global $Endless = 1 ;beginning of the loop

Do
    $FileList = _FileListToArray($sou_path, '*.pdf', 1, True)   ; moved inside loop
    If @ERROR then                                              ; see doc for return values
    Else
        For $i = 1 To $FileList[0]
            Run("C:\Program Files (x86)\ABBYY FineReader 14\FineReader.exe")
            WinWait("ABBYY FineReader 14 Standard")
            ControlClick('ABBYY FineReader 14 Standard', '', 'Button27')
            WinWait('Select Files to Convert to Microsoft Word')
            ControlSetText("Select Files to Convert to Microsoft Word", "", "Edit1", $FileList[$i])
            ControlClick('Select Files to Convert to Microsoft Word', '', 'Button1')
            WinWait('ABBYY FineReader 14 Standard')
            ControlClick('ABBYY FineReader 14 Standard', '', 'Button49')
            WinWait('Save document as')
            ControlClick('Save document as', '', 'Edit1')
            ControlSetText('Save document as', '', 'Edit1', _RemoveFileExt($FileList[$i]) & ".docx")
            ControlClick('Save document as', '', 'Button5')
            WinWait("[CLASS:OpusApp]")
            WinClose("[CLASS:OpusApp]")
            WinClose("[CLASS:AWL:6F6D0000:8:10003:a5100da2:9009a3:1a3040f]")
            WinClose("[CLASS:AWL:6F6D0000:8:10003:3f10145d:5c11bb:5b11283]")
            WinClose("[Class:AWL:6F6D0000:8:10003:9c100bae:1ef0601:3021219]")
            WinClose("[CLASS:AWL:6F6D0000:8b:10003:0:0:0]")
            WinClose('ABBYY FineReader 14 Standard')

            $sURL = "https://www.onlinedoctranslator.com/translationform"
            $oIE = _IECreate($sURL)
            $hIE = _IEPropertyGet($oIE, "hwnd")
            Global $oForm = _IEFormGetObjByName($oIE, "translation-form")
            $oButton = _IETagNameGetCollection($oIE, "Button", 0)

            If @error = 0 And IsObj($oButton) Then _IEAction($oButton, "focus")
            ControlSend($hIE, "", "", "{ENTER}")


            While 1
                If WinWait("[CLASS:#32770]", "", 2) Then
                    ConsoleWrite("Debug: Detected update prompt window" & @LF)
                    $hWin = WinGetHandle("[CLASS:#32770]", "")
                    WinActivate($hWin)
                    Sleep(1000)
                    ControlFocus("Choose File to Upload", "", "Edit1")
                    Sleep(1000)
                    ControlSetText("Choose File to Upload", "", "Edit1", _RemoveFileExt($FileList[$i]) & ".docx")
                    Sleep(1000)
                    ControlClick("Choose File to Upload", "", "Button1")
                    Sleep(1000)

                    $Translation = _IEFormGetObjByName($oIE, "translation-form")
                    $TranslationFrom = _IEFormElementGetObjByName($Translation, "from")
                    _IEFormElementOptionSelect($TranslationFrom, "Chinese", 1, "byText")
                    $TranslationTo = _IEFormElementGetObjByName($Translation, "to")
                    _IEFormElementOptionSelect($TranslationTo, "English", 1, "byText")

                    Sleep(30000)

                    _IEFormSubmit($oForm)

                    Sleep(60000)

                    Local $hIE2 = WinGetHandle("[Class:IEFrame]")
                    Local $hCtrl = ControlGetHandle($hIE2, "", "[ClassNN:DirectUIHWND1]")

                    ControlSend($hIE2, "", $hCtrl, "!S")

                    Sleep(10000)

                    ExitLoop
                Else
                    ConsoleWrite("Debug: Update prompt window not detected yet..." & @LF)
                EndIf
            WEnd

            Sleep(500)


            _IEQuit($oIE)
            Sleep(5000)

            Global $aPathSplit = _PathSplit(_FindClientName($FileList[$i]), $sDrive, $sDir, $sFileName, $sExtension)
            If FileExists("C:\Mega\1-Service\3-Deliverable\" & $sFileName) = 0 Then
                DirCreate("C:\Mega\1-Service\3-Deliverable\" & $sFileName)
            EndIf

            Global $ClientFolder = "C:\Mega\1-Service\3-Deliverable\" & $sFileName
            Global $DLFolder = _PathSplit(_RemoveFileExt($FileList[$i]), $DLDrive, $DLDir, $DLFileName, $DLExtension)

            FileMove($FileList[$i], $ClientFolder)
            FileMove(_RemoveFileExt($FileList[$i]) & ".docx", $ClientFolder)
            FileMove("C:\Users\Peak Reliance\Downloads\" & $DLFileName & ".zh-CN.en.docx", $ClientFolder)

        Next
    EndIf
    Sleep(2000)
Until $Endless = 2 ;Ending the loop


Func _RemoveFileExt($string)
    Return StringLeft($string, StringInStr($string, ".", Default, -1) - 1)
EndFunc   ;==>_RemoveFileExt
Func _FindClientName($string)
    Return StringLeft($string, StringInStr($string, "-", Default, -1) - 1)
EndFunc   ;==>_FindClientName

kylomas

It's hard to understand the logic by words but looking at your fix for my code, it all makes sense. Really appreciate your help!!! It works like a charm now.

Share this post


Link to post
Share on other sites

#7 ·  Posted

9 hours ago, BrewManNH said:

As @kylomas stated, please read the help file for the the return values for the function. The For/Next loop will only check the returned result the first time it runs, and if $Filelist isn't an array, you'll immediately get an error. Again, read the help file as to how For/Next works.

I thought you were looking for new/added files with this? This function will only check the files that were in there the first time you used _FileListToArray, once that's done _FLTA isn't updated ever again, so it's looking for the same files that were there when you first ran it, so if you want it to keep checking the files in the folder, you have to use it every time you finish processing what was already there. So, put it inside the loop. The code is executed from the first line to the last, unless there's a function call in there or a loop of some kind, so having the _FLTA outside the loop, it's only executed once.

Thank you :)

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