CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2008
    Posts
    38

    Get files from folder in ascending order based on creation date

    Hi,

    I need to get files from a folder in ascending order based on the creationtime of the files under it.

    Please help me.

    Thanks in advance.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get files from folder in ascending order based on creation date

    I would use the File System Object here, something like this :

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim fso As New FileSystemObject
    Dim fld As Folder
    Dim fil As File
    
    Set fld = fso.GetFolder("C:\Hannes")
    For Each fil In fld.Files
       
      List1.AddItem fil.Name
    Next
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
    
    End Sub
    I'd then still use the FSO to obtain the various properties of the files :

    http://www.aivosto.com/visdev/fso.html

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Get files from folder in ascending order based on creation date

    I would use the Shell libary or API

    Some useful info in this thread
    http://www.codeguru.com/forum/showthread.php?t=497321
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get files from folder in ascending order based on creation date

    I forgot about that excellent thread.

    Ignore my first post, Idris, and definitely take DataMiser's advice!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Get files from folder in ascending order based on creation date

    Yes I'd also recommend to use shell objects.
    Although you have to watch out that the creation date has to be obtained explicitly. It is not direct part of the FolderItem structure.
    To sort files according to creation date you could make use of a ListView control, if you want to make your search result visible anyway.
    You use a two-column Report-style listview, put the name in one and the date in the second column, then let the ListView sort it for you.
    If no visible control was required you need 2 Arrays, one for the filename and one for the date. You have then to write your own little sorting routine, though.

  6. #6
    Join Date
    Apr 2008
    Posts
    38

    Re: Get files from folder in ascending order based on creation date

    Thanks a lot for the comments.

    Its taking a long time when i loop through the folder since i am accessing a remote path.

    I tried with normal filedir control which lists the files under the folder in a quick turn around time. Is there any similar control which should get the files,filesize and filemodifiedate from a folder without looping through the folder.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Get files from folder in ascending order based on creation date

    The control is slow, use the shell or api should be about 5 times faster
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Get files from folder in ascending order based on creation date

    The control does also aloop to scan a folder.
    Any loop written in VB using shell objects shouldn't be any longer than filelistbox.
    I'd advise to fill a ListView with Names and Dates and let the listview do the sorting according to the column you select as sortindex.

  9. #9
    Join Date
    Apr 2008
    Posts
    38

    Re: Get files from folder in ascending order based on creation date

    Can i have a code using shell object to loop through the directory.

    Currently i am using FSO object.

    Thanks a lot.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Get files from folder in ascending order based on creation date

    Look at the link I posted,
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Apr 2008
    Posts
    38

    Re: Get files from folder in ascending order based on creation date

    Thank a lot for the comments.

    Is there any option using FSO object to get the last accessed/created filetime from a remote folder without looping through the folder

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get files from folder in ascending order based on creation date

    It seems as if we came accross a language barrier or comprehension barrier or something

    Look at the link posted by DataMiser in Post #3.

    We cannot put it into simpler English.

    The thread in the link DataMiser supplied shows all relevant information. Look there, try the things. We do not hand code out on a silver platter.

    Do some effort. What is so complicated? What is so hard?

    Look at the link posted by DataMiser in Post #3!

  13. #13
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Get files from folder in ascending order based on creation date

    The problem seems to be that the shell objects do not provide direct access to the creation date. Documentation is a bit thin there.
    But the shell32.Folder provides the GetDetailsOf() method which you'd have to use like this:
    Assumed you have the folder which holds the files you'd want to list.
    Further assume you want to put names and dates in a three-column-ListView named LV
    Code:
    Private Sub FillListView(LV As ListView, Fold As Shell32.Folder)
      Dim fit As Shell32.FolderItem
      Dim lit As ListItem
      For Each fit In Fold.Items
        If fit.IsFileSystem And Not fit.IsFolder Then 'it is a file
           Set lit = LV.ListItems.Add(, , fit.Name)
           lit.SubItems(1) = Fold.GetDetailsOf(fit, 3) 'get creation date
           lit.SubItems(2) = Fold.GetDetailsOf(fit, 4) 'get modification date
        End If
      Next
    End Sub
    You can set LV to be sorted and sort index to be 1, which is the creation date column then.

  14. #14
    Join Date
    Apr 2008
    Posts
    38

    Re: Get files from folder in ascending order based on creation date

    Hi All,

    Thanks a lot for the help.

    Atlast my expectation is fulfilled with shell32.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured