| View previous topic :: View next topic |
| Author |
Message |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Wed Jun 02, 2004 5:58 am Post subject: Directory listing into a Writer Table |
|
|
The following program creates a Writer document that has a five column table, which is a directory listing of some folder.
The five columns of the table are....
1. Filename
2. IsFolder?
3. IsReadOnly?
4. Size (bites)
5. Last Modified
| Code: |
Sub Main
'------------------------------ C H A N G E M E ------------------------------
' CHANGE ME!
cFolder = "C:\"
'cFolder = "/home/danny"
'cFolder = "/home/danny/Desktop"
'cFolder = "C:\Documents and Settings\dbrewer\Desktop"
'------------------------------ C H A N G E M E ------------------------------
' Now compute the URL for the folder.
cFolderUrl = ConvertToURL( cFolder )
' Now get a SimpleFileAccess service.
oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
' From SimpleFileAccess, get an ARRAY of the names of what is in the folder.
' Pass True to include folder names, False to not include folder names.
' The variable aNames is an ARRAY of strings, just as surely as if we had
' used the Dim statement: Dim aNames( nNumNames ) As String.
aNames = oSimpleFileAccess.getFolderContents( cFolderUrl, True )
' Get number of items in array.
nNumNames = UBound( aNames ) + 1
' Create new Writer document.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array()
' Create a table
oTable = oDoc.createInstance( "com.sun.star.text.TextTable" )
' Initialise table to have the number of rows for the number of file names,
' and two columns.
oTable.initialize( nNumNames+1, 5 )
' Get view cursor
oViewCursor = oDoc.getCurrentController().getViewCursor()
' Get text of the document.
oText = oDoc.getText()
'Insert table into text at current view cursor position.
oText.insertTextContent( oViewCursor, oTable, False )
'Column headings.
oTable.getCellByPosition( 0, 0 ).setString( "File" )
oTable.getCellByPosition( 1, 0 ).setString( "Folder?" )
oTable.getCellByPosition( 2, 0 ).setString( "ReadOnly?" )
oTable.getCellByPosition( 3, 0 ).setString( "Size" )
oTable.getCellByPosition( 4, 0 ).setString( "Modified" )
' Now let's have some fun with the table.
' Fill in all the cells.
For nRow = 1 To oTable.getRows().getCount() - 1
cNameUrl = aNames( nRow-1 ) ' get name from array element.
cName = ConvertFromURL( cNameUrl )
nFileSizeBytes = oSimpleFileAccess.getSize( cNameUrl )
tWhenModified = oSimpleFileAccess.getDateTimeModified( cNameUrl )
oTable.getCellByPosition( 0, nRow ).setString( cName )
If oSimpleFileAccess.isFolder( cNameUrl ) Then
oTable.getCellByPosition( 1, nRow ).setString( "Folder" )
Else
oTable.getCellByPosition( 1, nRow ).setString( "File" )
EndIf
If oSimpleFileAccess.isReadOnly( cNameUrl ) Then
oTable.getCellByPosition( 2, nRow ).setString( "ReadOnly" )
Else
oTable.getCellByPosition( 2, nRow ).setString( "" )
EndIf
oTable.getCellByPosition( 3, nRow ).setString( CStr( nFileSizeBytes ) )
oTable.getCellByPosition( 4, nRow ).setString( DateTimeToStr( tWhenModified )
Next
End Sub
Function DateTimeToStr( oDateTime As com.sun.star.util.DateTime ) As String
cString = PadL( CStr( oDateTime.Hours ), 2, "0" ) _
+ ":" _
+ PadL( CStr( oDateTime.Minutes ), 2, "0" ) _
+ ":" _
+ PadL( CStr( oDateTime.Seconds ), 2, "0" ) _
+ " " _
+ PadL( CStr( oDateTime.Day ), 2, "0" ) _
+ "-" _
+ ShortMonthName( oDateTime.Month ) _
+ "-" _
+ PadL( CStr( oDateTime.Year ), 4, "0" )
DateTimeToStr = cString
End Function
'----------
' Return a string which is the short name of the month.
' nMonth must be from 1 to 12. 1 is January.
'
Function ShortMonthName( nMonth As Integer ) As String
ShortMonthName() = Mid( "JanFebMarAprMayJunJulAugSepOctNovDec", (nMonth-1)*3+1, 3 )
End Function
' If cString is longer than nLen, then truncate SUFFIX characters (from the right)
' to make cString's length exactly nLen.
' If cString is shorter than nLen, then pad it on the left with cPadChar.
' If cPadChar is not specified, then it is a blank.
Function PadL( ByVal cString As String, ByVal nLen As Long, Optional cPadChar ) As String
nStrLen = Len( cString )
If nStrLen > nLen Then
cString = Left( cString, nLen )
ElseIf nStrLen < nLen Then
If IsMissing( cPadChar ) Then
cPadChar = " "
EndIf
Do
cString = cPadChar + cString
Loop While Len( cString ) < nLen
EndIf
PadL() = cString
End Function
|
This program was written in response to this question....
http://www.oooforum.org/forum/viewtopic.php?t=9447
I edited the above program with somewhat improved comments. _________________ Want to make OOo Drawings like the colored flower design to the left?
Last edited by DannyB on Sat Oct 09, 2004 8:30 am; edited 1 time in total |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|