[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
robleyd
Moderator
Posts: 5082
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

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

Post by robleyd »

Unix patterns are possibly better known as shell patterns; this Linux Journal article may be of interest. It lists the patterns for the bash shell; there may be some differences between other shells such as ksh, csh, tcsh etc.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
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 »

just fo the record: my Answer was exactly to:
Sébastien C wrote:@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.
I'm not surprised that SFA has not this capabilities.
Lupp wrote:but can they actually skip the creation of an explicit sequence of the folder content?
I hope we all agree that it is not possible to get information without asking for it.
AFAIK any FS (Filesystem) needs an efficient way storing Information about itself.
The Python-code above does not hold the whole sequence, it yields only one entry after other at any time.
If I would need the whole List i would write:

Code: Select all

…
p = list(Path('/some/folder/path').glob("blabla_0?.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)
eeigor
Posts: 214
Joined: Sun Apr 12, 2020 10:56 pm

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

Post by eeigor »

In Basic, Dir() has always suited me. Even with its strange way of calling.
And why do we need case sensitivity using the Like operator when selecting files? Yes, Linux, for example, is case-sensitive to the file name, but I try to avoid this (Mac OS (Unix, like Linux), Windows are insensitive).
Of course, this is (below) overkill (see: Binary compare option).

Code: Select all

Option VBASupport 1
Option Compare Binary  'Text=Case-insensitive (default)
Sub TestLikeOperator()
	Dim s$: s = "blabla_0a.png"
	Print s Like "blabla_0?.png"  'returns True
	Print s Like "Blabla_0?.png"  'returns False
End Sub
Using the Like operator in case-insensitive mode does not require 'Option VBASupport' at all.
All other Basic solutions complicate the task. Use Python. Or use TextSearch service which can search a string with a defined algorithm in another string (regexp).
Тhere are a lot of strange things in programming, and many things are far from perfect.

Edit:
Missing API? Then extend the capabilities of Basic: Dir() SFA + WildFilter(). But this is still the same Like operator.

It is not difficult to implement the VBA Filter() function: Filter(sourcearray, match, [ include, [compare ]]) with the support of the wildcards.
Note: Include Optional. Boolean value indicating whether to return substrings that include or exclude match. If include is True, Filter returns the subset of the array that contains match as a substring. If include is False, Filter returns the subset of the array that does not contain match as a substring.

VBA code. You should return the Variant array and fix, perhaps, something else (remove the default values from the list of parameters, and so on).

Code: Select all

'  Purpose: Filters the elements of the source array,
'           checking them for compliance with the matching pattern.
'     Note: Unlike the standard Filter function,
'           it allows the use of wildcards when specifying a sample.
'  Accepts: See Filter function (above).
'  Returns: A zero-based, one-dimensional array of substrings
'           that include or exclude match.
Function WildFilter(SourceArray() As String, ByVal Match As String _
 , Optional ByVal Include As Boolean = True) As String()
    Dim astrSelected() As String
    Dim i&, j&

    If Include Then
        For i = LBound(SourceArray) To UBound(SourceArray)
            If SourceArray(i) Like Match Then
                ReDim Preserve astrSelected(0 To j)
                astrSelected(j) = SourceArray(i)
                j = j + 1
            End If
        Next
    Else
        For i = LBound(SourceArray) To UBound(SourceArray)
            If Not SourceArray(i) Like Match Then
                ReDim Preserve astrSelected(0 To j)
                astrSelected(j) = SourceArray(i)
                j = j + 1
            End If
        Next
    End If
    WildFilter = astrSelected
End Function
Note: Probably, the same function can be implemented using the TextSearch service. Moreover, case-sensitive, if required.
Demo code

Code: Select all

    oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
    oOptions = CreateUnoStruct("com.sun.star.util.SearchOptions")
    oOptions.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
    With com.sun.star.i18n.TransliterationModules
        oOptions.transliterateFlags = .IGNORE_CASE  'only if case-insensitive (case-sensitive by default)
    End With
    '...
    oOptions.searchString = sPattern
    oTextSearch.setOptions(oOptions)
Note: However, you can add a case-sensitive mode directly to the body of the regular expression: "(?-i)". But this is the default value. Usually we do the opposite: "(?i)".

In my opinion, that's enough...
Last edited by eeigor on Mon Sep 13, 2021 2:07 pm, edited 2 times in total.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
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 »

Code: Select all

Option Compare TEXT
I have the idea this is working.
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
eeigor
Posts: 214
Joined: Sun Apr 12, 2020 10:56 pm

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

Post by eeigor »

Alas,
Option VBASupport 1
Option Compare...
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Mountaineer
Posts: 314
Joined: Sun Sep 06, 2020 8:27 am

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

Post by Mountaineer »

Lupp wrote: I myself ... but twice asked in return the crucial question of the context:
What's a "Unix pattern"?
When we say Unix pattern usually we mean the pattern expansion provided by the bash. (As robleyd already mentioned there may be some differences, if you replaced bash by something else.
Gnu-version (you may not the similiarity to the ICU-regular expressions in LibreOffice ) : https://www.gnu.org/software/bash/manua ... ching.html

Comparing with MS-DOS *.* and ? there are much more options and it should also be noted, that the dot was not actually part of a DOS-Filename, while it is part of a Unix-Filename.

If I'm remembering right the Unix-bash does the pattern expansion before calling the program.
So programs may be virtually support patterns, while actually only bash reeally handle this.

J.
OpenOffice 3.1 on Windows Vista
User avatar
Lupp
Volunteer
Posts: 3549
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 »

Thanks to those who tried to help me understand!

However, I feel bad about the ways things are going on.

For the very short time of my serious studies in the field more than 50 years ago a definition was a definition, and an implementation was an implementation. There was a rather clear distinction between syntax and related semantics, between explanations (using examples) and examples (mostly accompanied by explanations)...

Now we have RFCs where not only the style and the content are doubtable concerning precision, but even the name behind the three-letter-thing is gravely misleading. Then we seem to have advanced to defimentations and implenitions. Nobody can tell (e.g.) what characters actually need to be "escaped" under what conditions and everything...

You surely know of the discussions about "intelligent design"? Intelligent systems evolving from what humans designed by their supposed intelligence may one day be in trouble concerning their question if there was intelligence involved in their creation...

I can't escape occasional apocalyptic dreams concerning a world having "digitalized" (clouded actually?) everything.
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 »

Ok I see …
please exuse for disturbing this intressting topic :!:
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)
Post Reply