Hello Johnny, On Wednesday 01 December 2010, 10:38, Johnny Rosenberg wrote: > 2010/12/1 Johnny Rosenberg <[email protected]>: > > If I know the name of a directory, let's say ~/MyImages, how can I > > easily read the names of all the files in that directory? Let's say > > that we will fill an array with all the file names. > > > > I have searched a bit online, but so far I didn't find anything that I > > understand… I'll continue searching, though. > > He he he… I tried again to search for the answer and this time I found > it almost immediately… At least I think I did: > > http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide/Files_an > d_Directories_(Runtime_Library) > > Seems to be what I am looking for. > > Other thoughts about this are still welcome, though.
I'd prefer using OOo API (not simply OOo Basic). This way you can then port
your code to any supported language.
Try the following:
REM ***** BASIC *****
Option Explicit
Sub Main
Dim oSFA as Object
oSFA = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
Dim sURL$
sURL = getHomeDir()
If NOT oSFA.exists( sURL) OR NOT oSFA.isFolder( sURL ) Then
'some message
Exit Sub
End If
Dim sHomeDirFileContents$()
Dim sHomeDirFolderContents$()
sHomeDirFileContents = oSFA.getFolderContents( sURL, false )
sHomeDirFolderContents = oSFA.getFolderContents( sURL, true )
Dim oDoc as Object
oDoc = StarDesktop.loadComponentFromURL(_
"private:factory/swriter", _
"_default", _
com.sun.star.frame.FrameSearchFlag.ALL, _
Array())
Dim oCursor as Object
oCursor =
oDoc.getText().createTextCursorByRange(oDoc.getText().getStart())
InsertContents( oCursor, sHomeDirFileContents, "Only files" )
InsertContents( oCursor, sHomeDirFolderContents, "Files and folders" )
End Sub
Sub InsertContents( oCursor as Object, sContents(), sHeading$ )
oCursor.ParaStyleName = "Heading 1"
oCursor.getText().insertString( oCursor, sHeading, false )
oCursor.getText().insertControlCharacter( oCursor, _
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,
false )
oCursor.ParaStyleName = "Default"
Dim n%
For n = 0 To UBound(sContents)
oCursor.getText().insertString( oCursor, sContents(n), false )
oCursor.getText().insertControlCharacter( oCursor, _
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,
false )
Next
End Sub
Function getHomeDir() as String
Dim oPaths
oPaths = CreateUnoService("com.sun.star.util.PathSubstitution")
getHomeDir() = oPaths.getSubstituteVariableValue("$(home)")
End Function
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
signature.asc
Description: This is a digitally signed message part.
