Jump to content

Folder check until correct file is found


Go to solution Solved by Melba23,

Recommended Posts

Hey fellas,

I am trying to create a script that would first off prompt you to select a folder which is put into an INI file.

Then a fuction checks for the latest file in specified folder and checks if the first three letters of such file are "Log".

If such file is not found a MsgBox should appear where when pressed NO it would prompt you to reselect the folder and do the file name check again.

And this is where the script fail. In the reselection.

The script works in these scenarios:

1) If the folder has already been specified in the INI file, the script correctly goes right into the main loop - WORKS

2) If there is no folder specified in the INI file, it specifies it and goes into the main loop - WORKS

But if, you select a folder and FileSelect function does not find any "Log" file and you press NO in the msgbox to reselect the correct folder with a "Log" file inside it will just continuously fire the msgbox over and over again.

Anybody?

#include <FileCountOccurrences.au3>
#include <String.au3>
#include <Array.au3>
#include <Date.au3>
#include <FileConstants.au3>
#include <File.au3>

;#RequireAdmin

HotKeySet("{F8}", "Terminate")

Call("FolderCheck")
Call("FileSelect")


While 1
   Local $WinCount = _FileCountOccurrences($sFolder & $newfile, "Victory")
   If $WinCount >= 1 Then
      Call("ShutdownPC")
   EndIf
   Sleep(2000)
WEnd

Func FolderCheck()
   Global $sFolder = IniRead("config.ini", "Path", "Log", "")
   If $sFolder = "" Then
      Call("FolderSelect")
      Global $sFolder = IniRead("config.ini", "Path", "Log", "")
   EndIf
EndFunc

Func FileSelect()
   $last = 0
   $aFiles = _FileListToArray($sFolder , "*" , 1)
   for $i = 1 to $aFiles[0]
   $time = filegettime($sFolder & "\" & $aFiles[$i] , 0 , 1)
   If $time > $last Then
   Global $newfile = $aFiles[$I]
   $last = $time
   EndIf
   next
   Global $NewFileName = StringLeft($newfile, 3)
   If $NewFileName <> "Log" Then
      $MyBox = MsgBox(4, "Cannot find a Log file", "There is no Log file in the folder selected. Is this the correct folder?" & @CRLF & @CRLF & "Press YES, If you are sure the folder is correct to Exit the script. Manually  create a log." & @CRLF & @CRLF & "Press NO to reselect the folder.")
      If $MyBox = 6 Then
         Exit
      ElseIf $MyBox = 7 Then
         Call("FolderSelect")
         Call("FileSelect")
      EndIf
   EndIf
EndFunc

Func FolderSelect()
    Local Const $sMessage = "Select Log folder."
    $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        MsgBox(1, "", "No folder was selected.")
    Else
        IniWrite("config.ini", "Path", "Log", $sFileSelectFolder & "\")
    EndIf
EndFunc

Func ShutdownPC()
   MsgBox(1, "", "shutdown")
EndFunc

Func Terminate()
   Exit
EndFunc
Edited by Seminko
Link to comment
Share on other sites

  • Moderators

Seminko,

I am disappointed. :(>

 

Select HearthCrawler Log folder

Googling tells me:

Hearthcrawler. Bot for Hearthstone

Over to you to convince me why I should not lock this thread. :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Hey Melba,

I know the forum rules:

1. Do not ask for help with AutoIt scripts, post links to, or start discussion topics on the following subjects:
- Launching, automation or script interaction with games or game servers, regardless of the game.
 
Now, I would like to clarify that this is NOT what I am trying to do there. What I'm creating is a script that would go through logs which are not generated by a game, collect the data from such logs and make a summary etc. etc.
 
There is no launching, automation or script interaction with a game of any kind involved.
 
Thank you
Link to comment
Share on other sites

  • Moderators

Seminko,

Fine - I was only asking. ;)

I will look at the script now. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

OMG, just got it, it should call FolderCheck as well so the actual folder variable gets updated for FileSelect to work :)

If $NewFileName <> "Log" Then
      $MyBox = MsgBox(4, "Cannot find a Log file", "There is no Log file in the folder selected. Is this the correct folder?" & @CRLF & @CRLF & "Press YES, If you are sure the folder is correct to Exit the script. Manually  create a log." & @CRLF & @CRLF & "Press NO to reselect the folder.")
      If $MyBox = 6 Then
         Exit
      ElseIf $MyBox = 7 Then
         Call("FolderSelect")
         Call("FolderCheck")
         Call("FileSelect")
      EndIf
   EndIf

Yay, I'm blind sometimes :)

Edited by Seminko
Link to comment
Share on other sites

  • Moderators
  • Solution

Seminko,

That is most definitely NOT the way to do it - you are recalling the FileSelect function from within itself, which is a recipe for disaster unless you are very careful. See the Recursion tutorial in the Wiki to understand why. ;)

Here is what I managed to cobble together from your existing code - as I would have done something very different altogether had I started from scratch:

;#include <FileCountOccurrences.au3>
#include <String.au3>
#include <Array.au3>
#include <Date.au3>
#include <FileConstants.au3>
#include <File.au3>

;#RequireAdmin

Global $newfile = ""

HotKeySet("{F8}", "Terminate")

While $newfile = "" ; Keep looking until a file is found or the script exits
    $sFolder = FolderCheck()
    $newfile = FileSelect($sFolder)
WEnd

While 1
    Local $WinCount = 0 ; _FileCountOccurrences($sFolder & $newfile, "Victory")
    If $WinCount >= 1 Then
        ShutdownPC()
    EndIf
    Sleep(2000)
WEnd

Func FolderCheck()
    Global $sFolder = IniRead("config.ini", "Path", "Log", "")
    If $sFolder = "" Then
        $sFolder = FolderSelect()
    EndIf
EndFunc   ;==>FolderCheck

Func FileSelect($sFolder)
    $last = 0
    $newfile = ""
    $aFiles = _FileListToArray($sFolder, "*", 1)
    ; Check if there are any files returned
    If IsArray($aFiles) Then
        For $i = 1 To $aFiles[0]
            $time = FileGetTime($sFolder & "\" & $aFiles[$i], 0, 1)
            If $time > $last Then
                Global $newfile = $aFiles[$i]
                $last = $time
            EndIf
        Next
    EndIf
    $newfile = StringLeft($newfile, 3)
    If $newfile = "Log" Then
        ; Return the file to end the loop
        Return $newfile
    Else
        Switch MsgBox(4, "Cannot find a Log file", "There is no Log file in the folder selected. Is this the correct folder?" & @CRLF & @CRLF & "Press YES, If you are sure the folder is correct to Exit the script. Manually  create a log." & @CRLF & @CRLF & "Press NO to reselect the folder.")
            Case 6
                Exit
            Case 7
                ; Clear selected folder to force new search
                IniWrite("config.ini", "Path", "Log", "")
                ; Return empty string to force a new loop
                Return ""
        EndSwitch
    EndIf
EndFunc   ;==>FileSelect

Func FolderSelect()
    Local Const $sMessage = "Select HearthCrawler Log folder."
    $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        MsgBox(1, "", "No folder was selected.")
        Exit
    Else
        IniWrite("config.ini", "Path", "Log", $sFileSelectFolder & "\")
        Return $sFileSelectFolder
    EndIf
EndFunc   ;==>FolderSelect

Func ShutdownPC()
    MsgBox(1, "", "shutdown")
EndFunc   ;==>ShutdownPC

Func Terminate()
    Exit
EndFunc   ;==>Terminate
See if that works for you - it does for me within the limits of my testing (no real log file). :)

M23

P.S. And PLEASE stop using Call! :mad:

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Unfortunately, that does not work for me. Having the same issue I created this thread for.

When I select a wrong folder, and press NO in the MsgBox to reselect the folder, there is an endless loop of msgboxes even though the correct folder has been selected.

But the original thing works for me. I'll check on recursion.

 

Why is 'Call' bad?

Edited by Seminko
Link to comment
Share on other sites

  • Moderators

Seminko,

How very strange - evidently the script works perfectly for me or I would not have posted it. :wacko:

As to Call: why make AutoIt work harder then it has to? By using Call you force the string name to be parsed and then compared to existing functions - using the function name directly allows the interpreter to go straight there. Call should only really be used when the function name has to be in string format - when compiled from other elements for example. Of course the script will still work if you use Call - but it is not considered good practice to use it when it is not necessary. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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