Recent

Author Topic: [SOLVED] FindAllFiles - How to create multiple file type masks  (Read 11806 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] FindAllFiles - How to create multiple file type masks
« on: November 22, 2012, 01:07:15 pm »
I'm using FindAllFiles with great success for finding all files recursively throughout a folder structure. So I know how to search for all files (using '*') and PDF's ('*.pdf) etc, and DOC files (*.doc).

However, I can't work out how to create MULTIPLE file masks using this function? e.g. PDF and DOC files, combined. AFAIK, you can't just replace the mask to '*.pdf, *.doc' - is there a way to seperate the mask so it will find both of those file types, is my question?

I found a solution here http://stackoverflow.com/questions/5991040/how-to-search-different-file-types-using-findfirst) but it's a bit more complicated. I wanted to check that the function, FindAllFiles, didn't already have the ability to built in and I was just missing it or mis-understanding it, which I have done many times in the past!.
« Last Edit: November 22, 2012, 05:43:39 pm by tedsmith »

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
Re: FindAllFiles - How to create multiple file type masks
« Reply #1 on: November 22, 2012, 01:26:59 pm »

@ted
How about to get all files with * and do
if (extension doc) or (extension pdf) then do something...
Just an idea, i havent tried just a small sketch.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FindAllFiles - How to create multiple file type masks
« Reply #2 on: November 22, 2012, 01:40:40 pm »
Ah, I used those two examples just to keep things simple. In reality, I intend to have an edit box with an instruction to the user "Enter file extension to search for, seperated with a comma", so the user could ask for all sorts of file types (and ones I don't even know about), but not necessarily all files and not necessarily just these two doc and pdf files as exampled. So my edit1.Text could possibly be

*.pdf, *.xls, *.xlsx, *.doc, *.msg

I will then need to pass that comma seperated string to FindAllFiles


KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: FindAllFiles - How to create multiple file type masks
« Reply #3 on: November 22, 2012, 03:14:46 pm »
Change edit to ->

*.pdf;*.xls;*.xlsx;*.doc;*.msg

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FindAllFiles - How to create multiple file type masks
« Reply #4 on: November 22, 2012, 04:16:01 pm »
So seperated with ; ? I treied that, but it just finds files that match the first mask and non of the ones that follow. Unless I am doing something wrong. So I have :

MyStringList := FindAllFiles(Directory, '*.pdf;*.xls;*.xlsx;*.doc;*.msg', True);

And it just finds pdfs.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: FindAllFiles - How to create multiple file type masks
« Reply #5 on: November 22, 2012, 04:27:55 pm »
AFAIR, it just searches for the first extension. Have a look at the source code to confirm or deny...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: FindAllFiles - How to create multiple file type masks
« Reply #6 on: November 22, 2012, 05:22:44 pm »
AFAIR, it just searches for the first extension. Have a look at the source code to confirm or deny...

No it should search all,..

But it looks like this that's causing the problem ->
Code: [Select]
//In Masks.pas
S := TParseStringList.Create(AValue, ASeparator + ' ');

For some reason it's wanting a space after separator.
So try ->
Code: [Select]
MyStringList := FindAllFiles(Directory, '*.pdf; *.xls; *.xlsx; *.doc; *.msg', True);




Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FindAllFiles - How to create multiple file type masks
« Reply #7 on: November 22, 2012, 05:29:49 pm »
Ah, gutted. Nope, it still doesn't work :-(

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: FindAllFiles - How to create multiple file type masks
« Reply #8 on: November 22, 2012, 05:38:57 pm »
Works here fine.

eg.
This ->
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  r:TStrings;
begin
  r := FindAllFiles('C:\SomeDir','*.pas; *.res');
  listbox1.Items.assign(r);
  r.free;
end; 

Finds me all .pas & all .res  files.
Maybe it's a version thing,  ..
Mine is FPC 2.7.1  Lazarus 1.1

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FindAllFiles - How to create multiple file type masks
« Reply #9 on: November 22, 2012, 05:43:25 pm »
Darn!

Mine is FPC 2.6.0, Laz 0.9.31

I will have to look at trying to upgrade. Always a struggle for me in the Linux environment (easier in Windows)

But that looks like the answer to my question, anyway, so thread SOLVED. Thanks mate.
« Last Edit: November 22, 2012, 05:46:27 pm by tedsmith »

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: FindAllFiles - How to create multiple file type masks
« Reply #10 on: November 22, 2012, 06:53:46 pm »
But it looks like this that's causing the problem ->
Code: [Select]
//In Masks.pas
S := TParseStringList.Create(AValue, ASeparator + ' ');

IIRC this means that both Separator and a space are treated as list separator.

Bart

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: [SOLVED] FindAllFiles - How to create multiple file type masks
« Reply #11 on: November 22, 2012, 09:12:39 pm »
IIRC this means that both Separator and a space are treated as list separator.

Sorry, how is that any different to what I've already told @tedsmith?

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: [SOLVED] FindAllFiles - How to create multiple file type masks
« Reply #12 on: November 23, 2012, 01:37:11 pm »
IIRC this means that both Separator and a space are treated as list separator.

Sorry, how is that any different to what I've already told @tedsmith?

I think we misunderstand eachother.
What I meant is that it breaks the string on evrey occurence of either a space or ASeparator.
So 'abc def;ghi' will break into 'abc', 'def' and 'ghi'

For some reason it's wanting a space after separator.
So try ->
Code: [Select]
MyStringList := FindAllFiles(Directory, '*.pdf; *.xls; *.xlsx; *.doc; *.msg', True);

You wrote it needed a space after each ASeparator, which would imply that
'abc def;ghi' would not break at all, since ASeparator (';') is never followed by a space in tis example.

Bart

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: [SOLVED] FindAllFiles - How to create multiple file type masks
« Reply #13 on: November 23, 2012, 03:43:54 pm »
Quote
I think we misunderstand eachother.

Ah, I see you meant to say or not and.

Yes, it seems that way.  So @tedsmith's problem must just be version based.

 

TinyPortal © 2005-2018