Jump to content

Recommended Posts

Posted

Hello everyone. thank you for reading this post.

I m trying to use _FileListToArrayRec to search "c:\files" for PDF files only residing within subdirectories beginning with the word "batch". I have tried the following syntaxes and am not having success. Would someone be kind enough to point me in the right direction? Thank you.

$aFiles = _FileListToArrayRec("c:\files\batch*", "*.pdf||", 1, 1, 1, 2)

$aFiles = _FileListToArrayRec("c:\files", "batch*;*.pdf||", 1, 1, 1, 2)

$aFiles = _FileListToArrayRec("c:\files", "batch*.*pdf||", 1, 1, 1, 2)

Posted (edited)

This works. $aFiles will be changed for each target folder but you should be able to work with this.

You could have a separate array for each iteration, or figure a way to concatenate all the results.

 

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

$aFolders = _FileListToArrayRec("C:\files", "batch*", 2)
If @error Then Exit
_ArrayDisplay($aFolders)
For $i = 1 To $aFolders[0]
  $aFiles = _FileListToArrayRec("C:\files\" & $aFolders[$i], "*.pdf")
  If @error Then ContinueLoop
  _ArrayDisplay($aFiles)
Next

 

Edited by pseakins
Edit1: Posted code in lieu of concept. Edit2: Removed code breaking debug statements

Phil Seakins

Posted (edited)
  On 7/17/2021 at 3:59 AM, pseakins said:

 

[...]
$aFolders = _FileListToArrayRec("C:\files", "batch*", 2)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aFolders=[' & $aFolders & '] Error code: ' & @error & @CRLF) ;### Debug Console
If @error Then Exit
[...]

 

Expand  

Caution : The ConsoleWrite function (re)sets the @error macro to 0 (not explicitly specified in the help).

So if _FileListToArrayRec returns non-0 , it will be overwritten by ConsoleWrite , and If @error Then Exit will not be triggered. Try it with e.g. an invalid path and :

 

[...]
If @error Then
    ConsoleWrite("Error found => Exit" & @CRLF)
    Exit
Else
    ConsoleWrite("no Error found => Continue" & @CRLF)
EndIf
[...]

Macros like @error or @extended should sometimes be better saved in variables :

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

Local $sBaseDir = @ScriptDir & "\TestDir", $iError, $iExtended
$aFolders  = _FileListToArrayRec($sBaseDir, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
$iError    = @error
$iExtended = @extended
ConsoleWrite('@@ Debug : Error code = ' & $iError & '   Extended = ' & $iExtended & @CRLF) ; *** Debug
If $iError Then
    ConsoleWrite("!Error found => Exit" & @CRLF)
    Exit
Else
    ConsoleWrite("no Error found => Continue" & @CRLF)
EndIf
_ArrayDisplay($aFolders)

 

Edited by Musashi
Reworded

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted (edited)
  On 7/17/2021 at 2:50 AM, millisys said:

I'm trying to use _FileListToArrayRec to search "c:\files" for PDF files only residing within subdirectories beginning with the word "batch".

Expand  

Provides an array ($aFiles) which contains all PDF files which occur in directories named Batch*.

#include <File.au3>
#include <Array.au3>
Local $sBaseDir = "c:\files", $aFolders[0], $aFiles[0], $aArr[0]
$aFolders  = _FileListToArrayRec($sBaseDir, "Batch*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If @error Then Exit MsgBox(BitOR(4096, 16), "Message : ", "Error or no folders found" & @CRLF)
For $i = 1 To $aFolders[0]
    $aArr = _FileListToArrayRec($aFolders[$i], "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
    If Not @error Then
        For $k = 1 To $aArr[0]
            _ArrayAdd($aFiles, $aArr[$k])
        Next
    EndIf
Next
_ArrayDisplay($aFiles, "matching pdf-files :")

 

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted
  On 7/17/2021 at 7:11 AM, Musashi said:

Caution : The ConsoleWrite function (re)sets the @error macro to 0

Expand  

Thanks @Musashi, I was well aware of this. I should have removed the debug statements before posting but I thought I would leave them in thinking they might help while overlooking the fact that the first one would break the following statement. They are not even useful anyway as the array (as a whole) can't be displayed in a consolewrite statement.

Phil Seakins

Posted
  On 7/18/2021 at 2:57 AM, pseakins said:

Thanks @Musashi, I was well aware of this.

Expand  

It would have been a great surprise to me, if it were otherwise ;). The hint was directed to our (no reaction showing) user @millisys :lol:.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...