Results 1 to 12 of 12
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    First & Last Filename (VB6)

    Using FileSystemObject, is it possible to find the first filename, last filename, first filedate created, last filedate created.

    There are many threads involving FSO but First & Last don't seem to be part of the FSO object.

    I don't want to fill a listbox ( I already have code for this), but separate textbox's.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: First & Last Filename (VB6)

    Dave,

    The FilesystemObject has no facilities for sorting files. You can store the file information in a disconnected ADO recordset and sort that on the field you want, then retrieve the first and last record. The code is not trivial; it's up to you to decide whether you want to go this way. An example can be found in this newsgroup post.

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: First & Last Filename (VB6)

    Hmmmmm

    I see what you mean. !
    I'll do some tests and see what I can come up with.
    It's not that critical for the application so it can sit on the back boiler for now.

    Thanks for the link anyway.
    Have a great weekend. <img src=/S/blackteeth.gif border=0 alt=blackteeth width=20 height=20>

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: First & Last Filename (VB6)

    Hi Dave,

    VB/VBAphobes can do this sort of thing with DOS batch files:


    Get Last Listed File Into A Variable
    "Last non hidden/system file" refers to the last file listed in a folder disk which is not necessarily the last file written to disk in that folder.
    :: lastfile.bat
    @ECHO off
    :: Get the last non hidden/system file in current directory into
    :: a variable (last file listed, not necessarily most recent).
    :: Accepts wildcard specification for parameter 1.
    SET lastfil=
    IF not "%1"=="" FOR %%f in (%1) do set lastfil=%%f
    IF "%1"=="" FOR %%f in (*.*) do set lastfil=%%f
    IF not "%1"=="" ECHO. last %1 file in directory is %lastfil%
    IF "%1"=="" ECHO. last file in directory is %lastfil%



    Get First Listed File Into A Variable
    :: 1stfile.bat
    @ECHO off
    :: Get the first non hidden/system file in current directory into
    :: avariable (1st file listed, not necessarily oldest).
    :: Accepts wildcard specification for parameter 1.
    IF "%2"=="ReCuRs" GOTO recurs
    SET fspec=%1
    IF not "%1"=="" FOR %%f in (%1) do %0 %%f ReCuRs
    IF "%1"=="" FOR %%f in (*.*) do %0 %%f ReCuRs

    :recurs
    SET 1stfile=%1
    IF not "%fspec%"=="" ECHO. 1st %fspec% file in directory is %1
    IF "%fspec%"=="" ECHO. 1st file in directory is %1
    ::


    I picked both of these up from the net many moons ago.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: First & Last Filename (VB6)

    Thanks Macropod for your code ( not sure how to implent it, but anyway).

    I managed to create a workaround within a routine I use for a hidden list box..
    Since listbox's populate in file name order, I can extract what I need from there.

    <pre> Dim fso As New Scripting.FileSystemObject
    Dim fld As Scripting.Folder
    Dim fil As Scripting.File
    Dim FNF As String
    Dim FNL As String
    Dim N As Integer
    Set fld = fso.GetFolder("L:mmpdfImage")
    Me.MousePointer = vbHourglass
    For Each fil In fld.Files
    Me.lstFiles.AddItem fil.Name
    Next fil
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing


    For N = 0 To lstFiles.ListCount - 1
    FNF = lstFiles.List(N)
    Me.Text3.Text = FNF
    Next N

    For N = lstFiles.ListCount - 1 To 0 Step -1
    FNL = lstFiles.List(N)
    Me.Text4.Text = FNL
    Next N

    Call Command1_Click
    Call Command2_Click
    Me.MousePointer = vbDefault</pre>



    Basically step backwards until the beginning of the list.

    Its sort of crude, but for my purpose works just fine.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: First & Last Filename (VB6)

    You don't need the loops for this: you can replace

    For n = 0 To lstFiles.ListCount - 1
    FNF = lstFiles.List(n)
    Me.Text3.Text = FNF
    Next n

    For n = lstFiles.ListCount - 1 To 0 Step -1
    [tabFNL = lstFiles.List(n)
    Me.Text4.Text = FNL
    Next n

    by

    Me.Text3.Text = lstFiles.List(lstFiles.ListCount - 1)
    Me.Text4.Text = lstFiles.List(0)

    You can only get the first and last names (alphabetically) this way; there is no way to get the oldest and newest without using a batch file or complicated code.

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: First & Last Filename (VB6)

    Hans
    Would your version be any quicker ?
    I don't really need dates, but the attached PNG shows what I was trying to achieve:

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: First & Last Filename (VB6)

    Dave,

    My version would be faster and more efficient. For example, the effect of this loop

    For N = 0 To lstFiles.ListCount - 1
    FNF = lstFiles.List(N)
    Me.Text3.Text = FNF
    Next N

    is as follows for a list with 20 items (numbered 0 to 19):

    <table border=1><td align=center>N</td><td align=center>Me.Text3.Text</td><td align=center>0</td><td>lstFiles.List(0)</td><td align=center>1</td><td>lstFiles.List(1)</td><td align=center>2</td><td>lstFiles.List(2)</td><tr><td align=center>

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: First & Last Filename (VB6)

    Yes I see now.
    I was looping several times thru the data and populated to text each time (Stepped thru).
    But
    We got there in the end.
    Have a good weekend. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  10. #10
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: First & Last Filename (VB6)

    In further reply, added modified functions/subs for sorting files in folder with a specified file extension. See attached text file. Modified subs end in "Ex": SortFilesInFolderEx allows you to specify file extension along with folder name; QuickSortTwoDimEx sorts the array, etc. Note that in the modified procedures, it was necessary to "swap" the order of the dimensions, ie, from (rows, columns) to (columns, rows) order. That's because you can only use Redim Preserve statement to resize the upper bound of the last dimension in an array. (Probably should have used columns, rows in first place....) Modified functions worked OK in tests; though with large number of files expect a short delay for data to be processed. Sample output:

    SortFilesInFolderEx "C:WINDOWSSYSTEM32", "dll"

    Folder: C:WINDOWSSYSTEM32
    File Extension: *.dll
    File Count: 1186
    Sort by File Name:
    First file name: 6TO4SVC.DLL 8/29/2002 5:00:00 AM
    Last file name: ZIPFLDR.DLL 3/3/2003 12:17:10 PM

    Sort by File Created Date:
    First file (date created): IGFXDO.DLL 1/1/1980
    Last file (date created): VFP6R.DLL 2/5/2004 8:15:12 PM

    When testing (with smaller number of files) and printing entire sorted array, the results were consistent with file info displayed in Windows Explorer for the specified folder.

    HTH

  11. #11
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: First & Last Filename (VB6)

    <P ID="edit" class=small>(Edited by MarkD on 15-Feb-04 08:00. Deleted attachment; replaced with revised file in subsequent post.)</P>Here is another option, if interested. The SortFilesInFolder sub (see attachment) uses FileSystemObject to populate an array with file info (file name & date created), then uses a Quicksort algorithm to sort array by specified element. Code in attached text file (exported VBA module) was adapted from a recent project where I needed to sort a two-dimensional array. The QuickSortTwoDim procedure allows you to specify which element in 2D array to sort. The arrays are dimmed as Variant so this will work with any simple data type like strings, numbers, or dates. If only certain files are supposed to be counted/sorted (based on file extension), modify sub accordingly (if this is case, I'd keep each file type in its own subfolder to simplify things).

    I don

  12. #12
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: First & Last Filename (VB6)

    Mark
    Thanks for the additional help here.
    Just goes to show, there are many ways to skin a cat.
    My form is working ok at the moment and only basic information is required.
    If I need to expand it's capabilities, your module will come in very handy.
    Again, thanks.

Posting Permissions

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