[Basic] List a directory with SFA and an Unix pattern.

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

[Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

Since I said here that I am asking only one question, it is high time to ask a second, just to show to the entire world how much I admire the answers… But I promise one thing: make Villeroy adore the use of spreadsheets…

As I am currently on the way to becoming a big boy who goes to the big school (pre-teenager in short), I start to study the serious things and to speak about creation of Instances all the time in my life as if to make me pass as an adult in authorized saloons (yeeepee!). So obviously, using a function like "Dir" in Basic, it is completely outdated, obsolete, it looks bad kind, like in the twentieth century, GWbasic and so on! No, but I'm a big boy, me!

So now, with broad shoulders, I use the service "com.sun.star.ucb.SimpleFileAccess"; SFA for friends. It's been really cool, people look at me like a professional and, like the big boys who go to the big school, I start to believe in it (Yes I know, we are satisfied with very little things).

SFA does have the well-documented "getFolderContents" method, which I never get tired of playing with, as with many other methods that Xray lists well. But there is a problem: where it was very simple to put an Unix pattern like "* .jpg" (or even "??a? .JPG"), I can't find the equivalent with SFA.

Code: Select all

Sub ExampleDir
 Dim sPath As String, sDir As String, sValue As String

   sDir = "Directory:"
  sPath = CurDir & getPathSeparator & "Images" + getPathSeparator + "*.png"
 sValue = Dir$(sPath)

 Do
  If sValue <> "." And sValue <> ".." Then sDir = sDir & chr(13) & sValue
  sValue = Dir$
 Loop Until sValue = ""

 MsgBox sDir, 0, sPath
End Sub
Say, aren't you still going to tell me to go back to GWbasic anyway?
No ? :super:
Yes ? :ouch:

Really ??? :lol:
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
RoryOF
Moderator
Posts: 34610
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RoryOF »

In the listing in
https://wiki.openoffice.org/wiki/Docume ... le_Control
there is a line using SetCurrentFilter

This may be what you are seeking.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

Oh I am sorry RoryOF!

I do not talk about "com.sun.star.ui.dialogs.FilePicker" service, that manage a dialog box but about "com.sun.star.ucb.SimpleFileAccess" service, that manage files directly... It is not exacty the same thing…
I understand of course that the SFA is used in your example, but just for testing if a file exists or if it is a folder. The filter is on the filePicker ; not on the SFA... ;)
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
RoryOF
Moderator
Posts: 34610
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RoryOF »

Try
https://www.debugpoint.com/2015/01/use- ... penoffice/
which seems to use a combination of SFA and filepicker to filter files.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

Uh, I think you haven't understood RoryOF yet!
I know how to use this filePicker perfectly; I even did it already; several times.
But I do NOT want to use filePicker; just have a variable that contains, as with Dim (code above), a list of files responding to a pattern filter.

And that, without having to validate any dialog box which, in my case, is not appropriate.
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

Its easy to filter the results yourself (which Dir and getFolderContents don't do for you)

If right(filename,4) = ".jpg" then

Edit:Oops can filter with Dir as Villeroy says below.
Last edited by JeJe on Thu Feb 27, 2020 10:55 pm, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34610
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RoryOF »

What I am politely trying to show is that others use a combination of the two processes, which suggests (doesn't prove) that there may be no way to do what you wish using only SFA.

I'm going offline for the night.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

VB6 and OOBasic are very close on these things. There will be several examples of doing these things with VB6 that you can easily find and may need little adaptation to OOBasic. Dir

http://www.vbforums.com/showthread.php? ... -extension

Semi-Recursive Dir

http://www.vbforums.com/showthread.php? ... hlight=dir
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Villeroy »

The Basic function dir() named after the DOS command is very strange. You call it once with a pattern and it returns the first match. Then you call it with no argument and it returns the subsequent matches until it runs empty.

Code: Select all

	sPattern =  "file:///path/*.abc"
	sFile = dir(sPattern) REM returns the first match from the set of matching files
	while len(sFile)>0
		REM do stuff
       	sFile = dir()  REM return the next match of the initial set
	wend
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

Interesting...
The path specifications listed in Dir may use the * and ? place holders in both VBA and Apache OpenOffice Basic. In Apache OpenOffice Basic, the * place holder may however only be the last character of a file name and/or file extension, which is not the case in VBA.
https://wiki.openoffice.org/wiki/Docume ... e_Library)
* (asterisk) Zero or more characters in that position.
? (question mark) Zero or one character in that position.
https://docs.microsoft.com/en-us/dotnet ... mework-4.8
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

@RoryOF
I am sorry if I have been rude to you; I apologise really if it the case for you. But the question is really not where you wanted to take us.

@JeJe
I easily understand your proposals. Now imagine that we let the user choose to create his own patterns himself (in a dialog box, in a Calc cell, whatever) since their use is perfectly standard, as much in a Unix environment than M$-W. It is obviously out of the question that I program all the configurations so that we can respond to situations like "??a? .JpG" as I say above. As Villeroy very well suggests, Dim operates with a logic that borders on the fourth dimension of the 17th century. If I can not find anything else, I will take Dim. But I also wish I did not have to speak GWbasic’s like.

@Villeroy
Indeed. Very strange but documented an explained. So, like in my example above (or yours), it works. It is because it seem that very stange that I feel it like something without future. For that, i ask the people about the SFA service.
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

Another (very clunky but flexible) approach would be to open a hidden Writer file, copy the results of getFolderContents to that and use regular expressions to find the files you want

This File Name & File Extensions Regex seems to work
([a-zA-Z0-9\s_\\.\-\(\):])+(.doc|.docx|.pdf)$
https://www.regextester.com/100077
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Villeroy »

Sébastien C wrote:@Villeroy
Indeed. Very strange but documented an explained. So, like in my example above (or yours), it works. It is because it seem that very stange that I feel it like something without future. For that, i ask the people about the SFA service.
Same behaviour since the first versions of Visual Basic. It will be doomed when the whole Baic language goes to hell.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

@Jeje
The problem is not to find a solution for filter files with regular expression or strings treatment (left, right, mid, instr, &c.) ; we know to do that. The problem is to allow users to give a pattern, that respect a standard commonly used. For example, if you type in a linux console "ls blabla_0?.png", the interpretor understand that so well and can answer:
  • blabla_01.png
  • blabla_02.png
  • blabla_03.png
  • blabla_0a.png
  • blabla_0b.png
  • &c.
It is possible (but I am not aware of it) that there is already available an algorithm which can start from a pattern of this Unix standard TO a regular expression (in turn usable). But it's extremely complicated when Dim does the work anyway.

I am surprising that the SFA do not allow a way for that.

@villeroy
From this we can note very pragmatically two things:
  1. I don't always disagree you…
  2. hell is, basically, like the coronavirus; you can be in his anteroom without necessarily wallowing in.
;)
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RPG »

Maybe you are searching for this:

Code: Select all

sub SimpleFileExample
dim oSimplefile
dim FileContent , FileName
oSimplefile = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
FileContent = oSimplefile.getFolderContents(converttourl("/home/romgro/Documenten/werk"),true )
for each FileName in FileContent 
	IF    FileName like "*werk/s*"    then PRINT FileName
' like does work  as in VBA but is BASIC 
' maybe all regexpression do work for each line
NEXT 
end sub
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

Thank you very much for your intervention, RPG; you fully understand my request.

The "like" operator is poorly documented. But we are lucky, in French, to have Bernard Marcelly's book which gives us good examples and shows us cases very close to regular expressions. It is with this book (and after tests on my part) that we realize that we are (very) far from the Unix standard that Dim respects. "like" brings a lot of differences that would make us want to use it … if they weren't as numerous.

For example, a pattern like "??-?.jpg" works fine with Dim, responding in this way to the Shell. But the same pattern does not work with "like". Under Unix, case sensitive is capital; but the "like" operator does not care.

But the idea is there; it just remains to find better. :crazy:
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RPG »

I did studied what is possible with VBASupport in OpenOffice the last two month. That make also that I discovered again LIKE.
73830 – VBA "LIKE" implementation
In the link you can find some explanations but also a testfile. I do understand that LIKE can do all regular expression what is possible in some kind.
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

To convert to a regular expression, replace instances of
? with [a-zA-Z0-9]
* with [a-zA-Z0-9]*
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

With a blank open writer document, something like:

Code: Select all


Sub Main

PTH = "C:\tmp"
ST = "\*.t?t"

sfa = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
a=sfa.getFolderContents(converttourl(pth),false)
vc = Thiscomponent.currentcontroller.viewcursor
for i= 0 to ubound(a)
vc.string = convertfromurl(a(i)) & chr(10)
vc.collapsetoend
next

aF  = CreateUnoService("com.sun.star.sheet.FunctionAccess")
REDIM ARR(2)

ARR(0)=ST
ARR(1) ="*"
ARR(2) =  "[a-zA-Z0-9]*"
RES= aF.callfunction("SUBSTITUTE",ARR)
ARR(0)=RES
ARR(1) ="?"
ARR(2) =  "[a-zA-Z0-9]"
res= aF.callfunction("SUBSTITUTE",ARR)

ARR(0)=RES
ARR(1) ="\"
ARR(2) =  "\\"
res= aF.callfunction("SUBSTITUTE",ARR)

ARR(0)=RES
ARR(1) ="."
ARR(2) =  "\."
res= aF.callfunction("SUBSTITUTE",ARR)

oSearch = thiscomponent.createSearchDescriptor()

with oSearch
   .SearchString = res
   .SearchRegularExpression = true
end with

oResult = thiscomponent.findall(oSearch)

thiscomponent.currentcontroller.select(oresult)
End Sub

Edit: Updated to add escape characters for \ (I'm on Windows) and . in the path.
Will probably need testing and refinement.
Edit2: like handling - in the path as well. I'd stick with dir ha!

Edit3: This might work better (I'm not great with regex):

Code: Select all

Sub Main

PTH = "C:\tmp"
ST = "*.txt"

insertfiles pth
selectSearch(st)
end sub

sub SelectSearch(st)
aF  = CreateUnoService("com.sun.star.sheet.FunctionAccess")
REDIM ARR(2)

ARR(0)=st
ARR(1) ="\"
ARR(2) =  "\\"
res= aF.callfunction("SUBSTITUTE",ARR)

ARR(0)=res
ARR(1) ="*"
ARR(2) = "[a-zA-Z0-9/:\\.-]*"
RES= aF.callfunction("SUBSTITUTE",ARR)
ARR(0)=RES
ARR(1) ="?"
ARR(2) = "[a-zA-Z0-9/:\\.-]"
res= aF.callfunction("SUBSTITUTE",ARR)


oSearch = thiscomponent.createSearchDescriptor()

with oSearch
   .SearchString = res
   .SearchRegularExpression = true
end with

oResult = thiscomponent.findall(oSearch)
thiscomponent.currentcontroller.select(oresult)

'result may not find first search result so this may be needed too.
'oResult = thiscomponent.findfirst(oSearch) 
End Sub


sub insertfiles(pth)
sfa = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
a=sfa.getFolderContents(converttourl(pth),false)
vc = Thiscomponent.currentcontroller.viewcursor
for i= 0 to ubound(a)
vc.string = convertfromurl(a(i)) & chr(10)
vc.collapsetoend
next
end sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RPG »

@Sébastien C
I think it is good to make a step in programming. I have made a choice to use only BASIC and a lot of the API. I use BASIC only for simple things but when you want do more complex things then it is maybe a good idea to use Python.

On the Dutch forum I did wrote sometime ago: Use no macros and complex functions in your spreadsheet but learn what you can do with the normal working of OOo-calc. Using a lot of macros and functions is something from history. Example people do use a lot of functions or macros what easy can be done with a pivot-table.

The reason I did special study all thing about VBAsupport was: I want understand what was added to OpenOffice.What is added: some little thing what make OpenOffice easy for good VBA programmer but not for the bad VBAprogrammers.

The reason why it is difficult for me to switch to Python is to learn how to declare all the modules but maybe I will do it once.
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Sébastien C »

A big thank you to all of you, for all your interventions. Still receive my apologies for the slowness of my present answer. I had prepared it (always first written in French, but "RPG" went before to ... basically say what I think ... Difficult, after that, to take up my own thread, let alone in a language with which I still struggle as much... So, I will respond to the points of view which, in my opinion, require refinements but with which it is understood that I generally agree.

Totally agree with you that the "macro reflex" is probably too present in many people. But I still believe that my own level makes me able to judge whether it is good or not, to use programming and languages to achieve my ends. If I want to get a 18th century graphic like this (each stroke is a pipe’s organ diameter), I know how to calculate it (geometrically) in Calc. As for printing, at scale, it turns out that the programmers of Open/LibreOffice did not think of this specific case... But the modules of the suite know very well how to talk to each other, via macro. So I stop crying about the limitation (!) of the charts in Calc which do not provide for the organological graphs of the 18th century (a scandal, admit it) and I am doing my little macro which is fine. The API offers me, however, a plethora of extremely complex, varied solutions, even with only Basic!

And that is the point that lets me to underline that I am still surprised that something as simple as listing the files in a directory according to a given pattern is not documented, precisely in the API, precisely because SFA does things that there are quite complex (for example, read and write a binary file), while a function in use for the less Rock'n Roll which dates from the Middle Ages and which has not yet burned with the framework of Notre-Dame of Paris, still works to this day. I am always very respectful of old and historic things. But still recognize that a directory file list is not the pinnacle of complexity that needs to be carved with an ax. We can, sometimes, find effective the electric planer...

I absolutely don't think Python is difficult to learn. I, for my part, started to dive into it, probably pushed even further by Villeroy, who started off on this particular subject, probably a little too much offten, but with reasons whose foundations it would be difficult to deny.

I had started studying VBA from the (glorious) time of M$-Excel in version 5. Micro$oft changed its language after this version to the one still valid today. But in the meantime, the versions of the two Basics had become incompatible. I am not the type to appreciate this type of jokes, especially to serve a trading system. When OpenOffice was released and I realized it was providing macros, I was obviously very happy. I have never regretted my choices to have studied this API and even a forum thread like this, allows me, again, to confirm it.

Because your insistence made me delve into "Option VBASupport 1" since it allows to change the behavior of the "like" operator which becomes case sensitive. Above all, I took the time to put all of this in the context of a script, quite long, that I am writing. And there, it is very funny: "Option VBASupport 1" causes me errors where there is none if the Option is absent. Like you, I am always as scrupulously as possible the API (from LibreOffice). On the other hand, it is obviously not an option to have to adapt to the whims of a support that I do not need. "VBASupport" seems to me to be a bad choice; simply because the two philosophies are diametrically opposed, also because I find it difficult to imagine a correct compatibility of a system which I have already experienced that it can change at all, without the slightest regard for its users. To devote energy and weighing the number of its libraries, it is obviously Python which, this time, is no longer an option.

I started this thread humorously (well, I think :roll: :ouch: ) knowing from the start my first example worked. But I do not sign this thread as "Solved"; because I cannot be convinced that the creators of an API, as complex as the one we are dealing with here, may not have thought of this Pattern’s case. It’s too huge, especially when a prehistoric tool is capable of it.

A huge thank you, too, to Jeje and his regular expressions ideas that can be used in any situation other than this.
:D
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RPG »

I think the thread change a little from writing macros to how to make a good drawing. If I understand you good then you have to make a complex drawing. I have the idea that OOo-draw is not the good tool for what you want. 20 years a go I have learned a little about CAD and I think a good but simple CAD program can do more for you then OOo-draw. I say a simple CAD program there the more complex CAD programs are so difficult that it is only to handle when you get instructions.

I mean a simple program as QCad but maybe there are more of that level. The trial version can be used for 10 minutes and store your drawing. You can go futher with the stored drawing.

I have not enough knowledge about VBAoptions for draw but I think: real soon you have to go to the API of OpenOffice.

CAD
In CAD you are always working with real sizes. You have all kinds of tools what will make your work easy. You can print your drawing on each size of paper what you want.

Learning a draw program
Learning a draw program is maybe as difficult as learning a spreadsheet program. In OOo-draw there are tools the same as in a professinal CAD program but they come clear when you real study the manual.


SFA
I think the SFA is not the tool what you want. What you need is maybe stamping

Stamping in LibeOffice
select something
menu --> Edit --> Duplicate
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
RoryOF
Moderator
Posts: 34610
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by RoryOF »

Working on Windows years (10+?) ago I found Drafix Cad was very easy to work with.

SFA is "Simple File Access", and I interpret "Simple" to mean "with no frills". Filepicker might be a better choice.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Lupp »

Sorry!
Everbody here seems to know how to understand -exactly- the term "unix pattern". I don't, and I couldn't find a syntax for it. Of course I would need some explanations in addition concerning the semantics (usage of the "wildcard" characters!), but I definitely cannot cope wih the usual "some examples" or the links to amateur lectures on youtube. There MUST be a clear and unambiguous specification somewhere. Can you point me to one?

And: The SimpleFileAccess service, even in its most advanced implementation, (https://api.libreoffice.org/docs/idl/re ... ccess.html) definitely doesn't provide means for filtering.

As soon as we clearly know what patterns should be allowed and what semantics is to regard we will surely(?) be able to "translate" the pattern into a RegEx which then can be used to to filter the names of folder contents.
Using LibreOffice V 6.2 or higher we will not need a hidden helper file to apply a RegEx search and we will have case sensitive RegEx search at hand using the REGEX() function. There is a problem with the third parameter forcing us to resort to a workaround, but otherwise that's simple. The case-sensitivity of RegEx can, of course, not cure the Windows inconsistencies: Searching files on Win must be case insensitive after all to avoid additional problems.
We also need to know what "special characters" need to be translated in what way for the filter first and for the output then.

If the question also includes the additional unix-like extensions to listing folder contents (FIND command), we will need to do that completely by user code, and to accept that not everyting necessarily is mappable to every different operating system.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Villeroy »

As far as the undocumented StarBasic operator Like is concerned, we seem to talk about glob pattern matching: https://en.wikipedia.org/wiki/Glob_%28programming%29
Python: https://docs.python.org/2/library/glob.html

My bash shell also supports character classes such as {a,d,f,x} matching any of the listed characters.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by JeJe »

Microsoft's documentation for the Like operator is here:

https://docs.microsoft.com/en-us/dotnet ... e-operator

OOBasic's implementation also supports lists of characters

MsgBox "B" Like "[B,A,T]"

returns true
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Villeroy »

JeJe wrote:Microsoft's documentation for the Like operator is here:

https://docs.microsoft.com/en-us/dotnet ... e-operator

OOBasic's implementation also supports lists of characters

MsgBox "B" Like "[B,A,T]"

returns true
Thanks.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Lupp »

JeJe wrote:OOBasic's implementation also supports lists of characters
MsgBox "B" Like "[B,A,T]"
returns true
The list itself is formed without any list seperator in this case. Same thing as with RegEx.
The comma in "[B,A,T]" is treated as a fourth literal character.
"," Like "[B,A,T]"
is True. (The repetition of an element is irrelevant when describing a set. The list is interpreted as a set here.)

Bump: Can someone point me to a definition/specification of the term "Unix pattern", please? I would prefer one using the Backus notation, joined by semantic explanations.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by karolus »

Please excuse me for reviving this old topic!

Globbing Syntax in python

Code: Select all

from pathlib import Path

p = Path('/some/folder/path').glob("blabla_0?.png")

for entry in p:
    print(f'{entry.name}')
 
↓↓↓
  • blabla_01.png
    blabla_02.png
    blabla_03.png
    blabla_0a.png
    blabla_0b.png
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Basic] List a directory with SFA and an Unix pattern.

Post by Lupp »

So, we all know the original question, hopefully, and also we all know that there were suggested different "solutions" rightfully rejected by the OriginalQuestioner (Sébastien C) as insufficient/unwelcome workarounds - or as missing the question basically.

Since the service SimpleFileAccess and its interfaces obviously don't support fully what's wanted, The questioner will need to evantually accept a workaround or to quit (and tell this explicitly), and contributors may suggest a completely different approach (without SFA), but any answer would require a clear understanding of the relevant part of the question posted as "and an Unix pattern".

I myself neither suggested a solution nor a workaround, but twice asked in return the crucial question of the context:
What's a "Unix pattern"?

Mimicking the humorous style of the very first post of this thread, i might state that you well may shelve me among the "not yet big boys" or the "kids", but you shouldn't refuse me an answer. When I still was a (full-grown, professional) teacher, I never refused answering questions by the young ones. Now being a schoolboy again, I hope to earn due respect insofar.
I'm not interested in a few more examples, but in a clear definition.

See:
posting.php?mode=reply&f=20&t=101206&si ... b#pr487943
and
posting.php?mode=reply&f=20&t=101206&si ... b#pr487984

BTW (concerning efficiency):
I wouldn' expect any OS/FileSystem to maintain an index allowing it to directly answer a request for folder contents filtered in an arbitrary way. I would assume it goes through the complete content, and does filter it "on the fly". It will surely be more efficient (concerning time / RAM - factually any resource) this way than Basic filtering a probably huge sequence subsequently.
Python means may (will) be faster, but can they actually skip the creation of an explicit sequence of the folder content?
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply