Sign in to follow this  
Followers 0
asgarcymed

About File Names - How can I use a filter that has more than one component?

10 posts in this topic

Inside a main directory I have many files, and I want to get a list of all files that simultaneously have the PDF extension (*.pdf) AND name beginning by an "A" (A*). Or either, I am trying to use a filter that has more than one component.

I could make 2 different scripts that use a filter with one-only component:

CODE
$path = "C:\Test"

$filter = "*.pdf"

$search = FileFindFirstFile ($path & "\" & $filter)

While $search = 1

$file = FileFindNextFile ($search)

If @error Then ExitLoop

MsgBox (0, "File Name", $file)

WEnd

CODE
#Include <File.au3>

#Include <Array.au3>

$path = "C:\Test"

$filter = "*.pdf"

$file_list = _FileListToArray ($path, $filter)

_ArrayDisplay ($file_list, "File List")

I tried

$filter = "A*" And "*.pdf"

But I got an error....

How can I use a filter that has more than one component?

Thanks in advance.

Best regards.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites



I don't know about using multiple filters. It is possible with other functions, like "FileOpenDialog" but it doesn't seem to work here.

Anyway, try this:

#include <File.au3>
#include <Array.au3>
$path = "C:\Test"
$filter = "*.pdf"
$search = _FileListToArray($path,$filter)
If IsArray($search) Then
    For $i=0 to $search[0]
        If StringLeft($search[$i],1)="A" Then
            MsgBox(0,"",$search[$i])
        EndIf
    Next
Else
    MsgBox(0,"Error","No files found")
EndIf

Share this post


Link to post
Share on other sites

Hi,

Try link in my sig to new proposed _FileListToArray;

#include<_FileListToArrayNew2g.au3>
local $path = @ScriptDir,$filter = "*.txt|A*"
$ar_Array = _FileListToArray3 ($path, $filter, 1, 0, 0)
_ArrayDisplay($ar_Array, "File List")
Best, Randall

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

randallc - If I use:

$filter = "A*|*.pdf"

or

$filter = "A*"|"*.pdf"

I get the same error - @Error = 2 (Invalid $sFilter)

And if I write:

#include <_FileListToArrayNew2g.au3>

I get another error message - " ==> Error opening the file.: <_FileListToArrayNew2g.au3>

Nahuel - your code works fine! Thank you!

But how to do in order to use, for example, a filter with 4 components?

Filter_with_4_conditions = ((filename with the "A*" wildcard) AND (filename with the "*.pdf" wildcard) AND 10 < StringLen (filename) <22) AND (FileGetSyze > 4 MB))

I am trying to code a File Sorter by many parameters I specify...

Forgive my late post but I had a problem in my home and only now I am near my computer again. Your answer was very quick; thanks! Please do not abandon this question....

Thanks in advance!

Edited by asgarcymed

MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites

#include <_FileListToArrayNew2g.au3>

I get another error message - " ==> Error opening the file.: <_FileListToArrayNew2g.au3>

You need to dowload that file:

http://www.autoitscript.com/forum/index.ph...mp;#entry372868

save it in the Include folder in the AutoIt directory and try again. I think randallc's script works fine :)

Share this post


Link to post
Share on other sites

Now the script runs fine but the search results are not correctly restricted to the filters... Files must have its name beginning with an "A" (filter 1 = "A*" wildcard") and its extension (last letters after the last dot) must be txt (filter 2 = "*.txt" wildcard). I even tried (again; now with the new include file) -

$filter = "A*|*.pdf" (no error messages but the list is much bigger / inaccurate)

vs

$filter = "A*"|"*.pdf" (got error message - ==> Unable to parse line.: ).

It seems that this new file included <_FileListToArrayNew2g.au3> may have a bug; or it is my brain buggy?

Thanks for replying :)


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Now the script runs fine but the search results are not correctly restricted to the filters... Files must have its name beginning with an "A" (filter 1 = "A*" wildcard") and its extension (last letters after the last dot) must be txt (filter 2 = "*.txt" wildcard). I even tried (again; now with the new include file) -

$filter = "A*|*.pdf" (no error messages but the list is much bigger / inaccurate)

vs

$filter = "A*"|"*.pdf" (got error message - ==> Unable to parse line.: ).

It seems that this new file included <_FileListToArrayNew2g.au3> may have a bug; or it is my brain buggy?

Thanks for replying :)

OK thanks,

I'll have a look; did you try the way I had tested?; "*.txt|A*"?

Thanks, Randall

Edited by randallc

Share this post


Link to post
Share on other sites

$filter = "A*"|"*.pdf" (got error message - ==> Unable to parse

There's your mistake. It should be:

$filter = "A*|*.txt"

But you're right, it doesn't work...

I suppose you could just add more conditions to the code I gave you before... I don't know, just an idea..

#include <File.au3>
#include <Array.au3>
$path = "C:\"
$filter = "*.ini"
$search = _FileListToArray($path,$filter)
If IsArray($search) Then
    For $i=0 to $search[0]
        If StringLeft($search[$i],1)="A" Then
            If (10>StringLen($search[$i])) And (StringLen($search[$i])>20) Then
                MsgBox(0,"",$search[$i])
            EndIf
        EndIf
    Next
Else
    MsgBox(0,"Error","No files found")
EndIf

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

$filter = "A*|*.txt"

But you're right, it doesn't work...

Hi,

Maybe rem out this line till I think about it! - It works again this way.. [about line 47 in UDF]

;~  $sFilter = StringReplace("*" & $sFilter & "*", "**", "*")
Best, randall

[PS You can be more specific also if you use ArraySearch, arrayFindAll - 1helpfile, 2search ultima]

Edited by randallc

Share this post


Link to post
Share on other sites

After many hours of brain working, I could improve my knowledge a lot! And the help you gave me was very, very important! So I want to "say louder" - THANK YOU!!!

;)

:)


MLMK - my blogging craziness...

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