OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Search file system and report

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
tcom
General User
General User


Joined: 18 Mar 2004
Posts: 7

PostPosted: Thu Apr 08, 2004 2:11 am    Post subject: Search file system and report Reply with quote

Hi All,

Question for you :

Content of A1 is a folder called 'waves' on the filesystem.
I need B1 to have searched the folder 'waves' to find the most recently added name.wav file.

Is this easy?

Cheers, Mark..
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Apr 08, 2004 9:16 am    Post subject: Reply with quote

Code:
Sub Main
   ' Specify the top most folder.
   cTopFolder = "C:\cygwin"
   
   ' Convert it to a URL.
   cTopFolderUrl = ConvertToUrl( cTopFolder )
   
   Print MostRecentlyAddedFile( cTopFolderUrl )
End Sub


' Return the URL of the most recently added file within a folder.
Function MostRecentlyAddedFile( cFolderUrl ) As String
   oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
   
   ' Set the maximum date/time seen so far to zero.
   oMaxDateTime = createUnoStruct( "com.sun.star.util.DateTime" )
   ' This is the name of the file with the biggest datetime.
   cMaxDateTimeFileName = ""
   
   ' Get an array of names of things in the folder.
   aFolderContents = oSimpleFileAccess.getFolderContents( cFolderUrl, False )
   
   ' Iterate over each element in the array.
   For i = LBound( aFolderContents ) To UBound( aFolderContents )
      cFolderItemName = aFolderContents( i ) ' get one item from the array
      
      ' Ignore if it is a folder.
      ' (Note: it should not be a folder, since we passed False
      '  on the getFolderContents().
      ' This is here just to show how to separate the men fro...er..um..
      '  how to separate the folders from the files.
      If oSimpleFileAccess.isFolder( cFolderItemName ) Then
         ' It's a folder, do nothing.
      Else
         ' Its a file.
         
         ' Get the file's date time.
         oDateTime = oSimpleFileAccess.getDateTimeModified( cFolderItemName )
         
         ' Is it greater than anything we've seen so far?
         If DateTimeGreater( oDateTime, oMaxDateTime ) Then
            oMaxDateTime = oDateTime
            cMaxDateTimeFileName = cFolderItemName
         EndIf
      EndIf
   Next
   
   MostRecentlyAddedFile = cMaxDateTimeFileName
End Function



'##########
'  Stuff ripped from my library.....

' Return True if oDateTime1 is equal to oDateTime2.
Function DateTimeEqual( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeEqual = False
   If         oDateTime1.HundredthSeconds   = oDateTime2.HundredthSeconds _
      And      oDateTime1.Seconds         = oDateTime2.Seconds _
      And      oDateTime1.Minutes         = oDateTime2.Minutes _
      And      oDateTime1.Hours         = oDateTime2.Hours _
      And      oDateTime1.Day            = oDateTime2.Day _
      And      oDateTime1.Month         = oDateTime2.Month _
      And      oDateTime1.Year            = oDateTime2.Year _
         Then
      DateTimeEqual = True
   EndIf
End Function

' Return True if oDateTime1 is greater than oDateTime2.
Function DateTimeGreater( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeGreater = False
   If oDateTime1.Year > oDateTime2.Year Then
      DateTimeGreater = True
   ElseIf oDateTime1.Year = oDateTime2.Year Then
      If oDateTime1.Month > oDateTime2.Month Then
         DateTimeGreater = True
      ElseIf oDateTime1.Month = oDateTime2.Month Then
         If oDateTime1.Day > oDateTime2.Day Then
            DateTimeGreater = True
         ElseIf oDateTime1.Day = oDateTime2.Day Then
            If oDateTime1.Hours > oDateTime2.Hours Then
               DateTimeGreater = True
            ElseIf oDateTime1.Hours = oDateTime2.Hours Then
               If oDateTime1.Minutes > oDateTime2.Minutes Then
                  DateTimeGreater = True
               ElseIf oDateTime1.Minutes = oDateTime2.Minutes Then
                  If oDateTime1.Seconds > oDateTime2.Seconds Then
                     DateTimeGreater = True
                  ElseIf oDateTime1.Seconds = oDateTime2.Seconds Then
                     If oDateTime1.HundredthSeconds > oDateTime2.HundredthSeconds Then
                        DateTimeGreater = True
                     EndIf
                  EndIf
               EndIf
            EndIf
         EndIf
      EndIf
   EndIf
End Function

' Return True if oDateTime1 is less than oDateTime2.
Function DateTimeLess( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeLess = DateTimeGreater( oDateTime2, oDateTime1 )
End Function

' Return True if oDateTime1 is greater than or equal to oDateTime2.
Function DateTimeGreaterOrEqual( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeGreaterOrEqual = Not DateTimeLess( oDateTime1, oDateTime2 )
End Function

' Return True if oDateTime1 is less than or equal to oDateTime2.
Function DateTimeLessOrEqual( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeLessOrEqual = Not DateTimeGreater( oDateTime1, oDateTime2 )
End Function

' Return True if oDateTime1 is not equal to oDateTime2.
Function DateTimeNotEqual( ByVal oDateTime1 As com.sun.star.util.DateTime, _
                  ByVal oDateTime2 As com.sun.star.util.DateTime ) As Boolean
   DateTimeNotEqual = Not DateTimeEqual( oDateTime1, oDateTime2 )
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
tcom
General User
General User


Joined: 18 Mar 2004
Posts: 7

PostPosted: Fri Apr 09, 2004 1:37 am    Post subject: Reply with quote

Excellent work DannyB!

Many thanks... and Happy easter Smile

OK.. so now I would like this to cd into the next directory if there are no files in that dir and keep going until it finds a file.

Cheers, Mark.
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Apr 09, 2004 6:34 am    Post subject: Reply with quote

Just a trivial matter of adding recursion.

See these three lines....
Code:
      If oSimpleFileAccess.isFolder( cFolderItemName ) Then
         ' It's a folder, do nothing.
      Else


Replace those three lines with....
Code:
      If oSimpleFileAccess.isFolder( cFolderItemName ) Then
         ' It's a folder.
         
         ' Get the most recently added file within the folder.
         cMostRecentlyAddedSubFile = MostRecentlyAddedFile( cFolderItemName )
         ' If there was one...
         If Len( cMostRecentlyAddedSubFile ) > 0 Then
            ' Get the sub folder file's date time.
            oDateTime = oSimpleFileAccess.getDateTimeModified( cMostRecentlyAddedSubFile )
            
            ' Is it greater than anything we've seen so far?
            If DateTimeGreater( oDateTime, oMaxDateTime ) Then
               oMaxDateTime = oDateTime
               cMaxDateTimeFileName = cMostRecentlyAddedSubFile
            EndIf
         EndIf
      Else



See the line that says....
Code:
   aFolderContents = oSimpleFileAccess.getFolderContents( cFolderUrl, False )


Change the False to True so that sub folder names are returned in addition to just file names. See the comment above the "If" statement that says we passed False to getFolderContents? Change that comment.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
tcom
General User
General User


Joined: 18 Mar 2004
Posts: 7

PostPosted: Mon Apr 12, 2004 12:41 am    Post subject: Reply with quote

excellent.
soo how do I know code the answer so it displays in a Cell rather than an alert message?

cheers, Mark..
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Apr 12, 2004 6:59 am    Post subject: Reply with quote

The Sub Main simply says Print MostRecentlyAddedFile( .... ). The Print statement is what displays a little dialog box.

You can call the function MostRecentlyAddedFile() and do anything you want with the result. You pass it a folder Url, it returns a file Url.

It is also possible to call Basic functions from a spreadsheet.

You could write a trivial wrapper function around MostRecentlyAddedFile() so that you pass it a folder pathanme (instead of a Url) and it returns a file pathname (instead of a Url). Such a function would be more convenient to call from the formula of a spreadsheet cell. The Sub Main uses ConvertFileToURL to convert a folder pathname into a Url. Use the inverse function ConvertURLToFile on the return result.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
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