Jump to content

Make a function (_function_name) run in the background


Recommended Posts

Hi

I am wanting to run _FileListToArrayRec in the background and check its progress (mainly I just want to know its progress). I am using _FileListToArrayRec to search for files of a given type and upon searching the entire C:\ drive I see that the process takes quite some time. I was inspired by this project as windows 11's files search function is currently broken for me (06/11/2021).

Any ideas how to run this function in the background and get the progress? 

I was thinking something along the lines of the InetGet ( "URL", "filename" [, options = 0 [, background = 0]] ) Background function, but I see in the help file the _FileListToArrayRec function does not have such a thing, does this mean it is not possible? 

Many thanks in advance,

Jacob

 

Global $version = "1.0.0.0"
;A script that counts how many .au3 files are in a folder and sub-folders

#include <File.au3>

Global $FileType = InputBox("File Type","Input the file type you would like to count."&@CRLF&"* = All types","*")
If @error=1 Then Exit
If $FileType="" Then $FileType = "*"

Global $FolderStart = FileSelectFolder("Select a folder to cound files in...",@HomeDrive)
If @error=1 Then Exit


;$Files = _FileListToArray($FolderStart,"*.au3",1)
$Files = _FileListToArrayRec($FolderStart,"*."&$FileType,1,1,1)
$BoolFiles = True
If @error <> 0 Then $BoolFiles = False

$output="Files:"
For $i = 1 to $Files[0] step 1
    $output=$output&@CRLF&$Files[$i]
Next

MsgBox(0,$Files[0]&" FIle have been found.",$output)

 

Edited by Thelaughedking
more details
Link to comment
Share on other sites

Not possible with _FileListToArrayRec.  You would need to use/create the code to add some checkpoints inside it.  For example you could count the number of folders from the starting folder and then calculate the number of folders searched to have a progress percentage.

Link to comment
Share on other sites

19 hours ago, Nine said:

Not possible with _FileListToArrayRec.  You would need to use/create the code to add some checkpoints inside it.  For example you could count the number of folders from the starting folder and then calculate the number of folders searched to have a progress percentage.

Thank you, this is a very good suggestion. I might look into this, we will see if I get around to it, I'm working on a tcp server so that takes priority at the moment :)

Link to comment
Share on other sites

It really wouldn't be all that difficult... Here's the original.

Func _FileListToArray($sFilePath, $sFilter = "*", $iFlag = $FLTA_FILESFOLDERS, $bReturnPath = False)
    Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = ""

    ; Check parameters for the Default keyword or they meet a certain criteria
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
    If $iFlag = Default Then $iFlag = $FLTA_FILESFOLDERS
    If $bReturnPath Then $sFullPath = $sFilePath
    If $sFilter = Default Then $sFilter = "*"

    ; Check if the directory exists
    If Not FileExists($sFilePath) Then Return SetError(1, 0, 0)
    If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0)
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 0, 0)

    Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
    If @error Then Return SetError(4, 0, 0)
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If ($iFlag + @extended = 2) Then ContinueLoop
        $sFileList &= $sDelimiter & $sFullPath & $sFileName
    WEnd
    FileClose($hSearch)

    If $sFileList = "" Then Return SetError(4, 0, 0)
    Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter)
EndFunc   ;==>_FileListToArray

So... You'll need to grab a total count of files from the directory that you're indexing, and then count them off...

Something like DirGetSize, maybe? This is dirty, but it gives you a good starting point. Take note of lines 15 & 21.

Func _FileListToArray($sFilePath, $sFilter = "*", $iFlag = $FLTA_FILESFOLDERS, $bReturnPath = False)
    Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = ""

    ; Check parameters for the Default keyword or they meet a certain criteria
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
    If $iFlag = Default Then $iFlag = $FLTA_FILESFOLDERS
    If $bReturnPath Then $sFullPath = $sFilePath
    If $sFilter = Default Then $sFilter = "*"

    ; Check if the directory exists
    If Not FileExists($sFilePath) Then Return SetError(1, 0, 0)
    If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0)
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 0, 0)
    
    $Return = DirGetSize($sFilePath, 1)
    $TotalFiles = $Return[1]
    Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
    If @error Then Return SetError(4, 0, 0)
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        $TotalFiles -= 1
        TrayTip("Info", "Files Remaining: " & $TotalFiles, 10)
        If ($iFlag + @extended = 2) Then ContinueLoop
        $sFileList &= $sDelimiter & $sFullPath & $sFileName
    WEnd
    FileClose($hSearch)

    If $sFileList = "" Then Return SetError(4, 0, 0)
    Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter)
EndFunc   ;==>_FileListToArray

 

SIGNATURE_0X800007D NOT FOUND

Link to comment
Share on other sites

  • Moderators

BinaryBrother,

The question refers to _FileListToArrayRec, not FileListToArray - which means that the problem is orders of magnitude bigger.

Thelaughedking,

As the author of the _FileListToArrayRec function I would counsel against trying to get any form of internal count of files to produce a percentage  value. For any such count to have a sensible value, you would essentially need to run the internal code of the UDF twice - once to get the count of files to test and then again to actually test them. And as you have discovered, working through a big folder tree in AutoIt can take quite some time - so doing it twice would basically double the execution time. Hence why such a functionality is not inside the code. Using DirGetSize to get a file count is equally futile as the number returned is the total files in the tree and not those which will be searched for by the UDF, which can include/exclude folders within the tree and so could have to deal with a significantly smaller number of files.

My advice has always been to use a marquee progress bar to indicate that something is happening so the the user does not come to believe that the whole process has stalled.

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

Another Doh!, in 24 hours. I was attempting to provide some useful support, while I was here asking questions and failed to note the details of OP's post.

That being said... I still have another suggestion!

Try taking a peek at this thread, where a few of the old dogs suggest different ways for dealing with this problem!

 

SIGNATURE_0X800007D NOT FOUND

Link to comment
Share on other sites

  • 2 weeks later...
On 11/8/2021 at 3:06 AM, BinaryBrother said:

It really wouldn't be all that difficult... Here's the original.

Func _FileListToArray($sFilePath, $sFilter = "*", $iFlag = $FLTA_FILESFOLDERS, $bReturnPath = False)
    Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = ""

    ; Check parameters for the Default keyword or they meet a certain criteria
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
    If $iFlag = Default Then $iFlag = $FLTA_FILESFOLDERS
    If $bReturnPath Then $sFullPath = $sFilePath
    If $sFilter = Default Then $sFilter = "*"

    ; Check if the directory exists
    If Not FileExists($sFilePath) Then Return SetError(1, 0, 0)
    If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0)
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 0, 0)

    Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
    If @error Then Return SetError(4, 0, 0)
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If ($iFlag + @extended = 2) Then ContinueLoop
        $sFileList &= $sDelimiter & $sFullPath & $sFileName
    WEnd
    FileClose($hSearch)

    If $sFileList = "" Then Return SetError(4, 0, 0)
    Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter)
EndFunc   ;==>_FileListToArray

So... You'll need to grab a total count of files from the directory that you're indexing, and then count them off...

Something like DirGetSize, maybe? This is dirty, but it gives you a good starting point. Take note of lines 15 & 21.

Func _FileListToArray($sFilePath, $sFilter = "*", $iFlag = $FLTA_FILESFOLDERS, $bReturnPath = False)
    Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = ""

    ; Check parameters for the Default keyword or they meet a certain criteria
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
    If $iFlag = Default Then $iFlag = $FLTA_FILESFOLDERS
    If $bReturnPath Then $sFullPath = $sFilePath
    If $sFilter = Default Then $sFilter = "*"

    ; Check if the directory exists
    If Not FileExists($sFilePath) Then Return SetError(1, 0, 0)
    If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0)
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 0, 0)
    
    $Return = DirGetSize($sFilePath, 1)
    $TotalFiles = $Return[1]
    Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
    If @error Then Return SetError(4, 0, 0)
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        $TotalFiles -= 1
        TrayTip("Info", "Files Remaining: " & $TotalFiles, 10)
        If ($iFlag + @extended = 2) Then ContinueLoop
        $sFileList &= $sDelimiter & $sFullPath & $sFileName
    WEnd
    FileClose($hSearch)

    If $sFileList = "" Then Return SetError(4, 0, 0)
    Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter)
EndFunc   ;==>_FileListToArray

 

Many Many thanks for you time @BinaryBrother I will try it out when I get a moment! Thanks again looks great

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