Using Array as FSO

nooch32

Member
Joined
Oct 25, 2006
Messages
17
Programming Experience
3-5
Hi,

I have an array that contains a list of files. All the files in the array are in the same directory, however the directory also contains other files, so in a way the array is a filtered list of files that i'm after.

Is it possible to convert my array into a file system object so i can obtain file information from the array of files, such as date created/modified?

In code its something like the following i'm trying to do (where arrFileList is the array containing the list of files, and fso is FileSystemObject, but it can be a collection of folders or whatever you suggest)...

Dim fsoFileList as fso
fsoFileList = arrFileList

Hope you understand what i'm trying to achieve. If anyone can help me it would be most appreciated :)
 
Not sure about the way your asking to do it, but you might find the following a useful alternative.

VB.NET:
Dim dirInputFileInfo As IO.FileInfo
Dim dirInputFileArray As IO.FileInfo()
dirInputFileArray = dirInputInfo.GetFiles("*.txt") 'Load list of .txt files into array

For Each dirInputFileInfo in dirInputFileArray
'Do more filtering if necessary (i.e. If dirInputFileInfo.Contains("test") then...)
'Process File here, all file attributes are available through enumerations under dirInputFileInfo.
Next
 
ah right, i've just dimmed my array as array for now, but i guess seen as tho my array contains file paths, i could dim it as a fileinfo, i'll try that thanks matey :D
 
ah it wouldn't let me do it that way, because the way the list is generated is using a join command and it's saying it can't convert it into fileinfo

If this helps you to understand the array i've generated would contain full file paths, for example:

(0) "C:\photos\me.jpg"
(1) "C:\photos\me1.jpg"
(2) "C:\photos\me and the dog.jpg"
(3) "C:\photos\charlie.jpg"
(4) "C:\photos\joe.jpg"

etc...

and i'm trying to preview the newest created one in a picbox, but i can't get the information from the array as it's all stored as text. I could open each one up individually, but if my array gets to a large number of files, say 100 then this is gonna be time consuming. I thought i might be able to sort the fileinfo array somehow by created time and then just show the one that's at the top of the array.

Maybe the only way to do it is to go through each file in the array and find out it's information :(
 
I think I get you now, sorry for the delay, took me a minute to find some code that would do it: The following should be something like what you want, heavily modified from here (great article): http://aspnet.4guysfromrolla.com/articles/060403-1.2.aspx

VB.NET:
Imports System.IO
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dirInputInfo As New DirectoryInfo("C:\Test\")                    'Directory to be scanned.
        Dim dirInputFileInfo As FileInfo
        Dim dirInputFileArray As FileInfo() = dirInputInfo.GetFiles("*.jpg") 'Load list of .txt files into array.

        Array.Sort(dirInputFileArray, New CompareFileInfoEntries)   'Sort the array, leaving the newest at the top.

        Dim myNewestFile As FileInfo = dirInputFileArray(0)         'Pull the first (newest) file entry from the array.

        'Here you can do what you like with the "myNewestFile" object. (i.e. load into picture box)


    End Sub
End Class

Public Class CompareFileInfoEntries
    Implements IComparer
    Public Overridable Overloads Function Compare(ByVal file1 As Object, _
           ByVal file2 As Object) As Integer Implements IComparer.Compare
        'Convert file1 and file2 to FileInfo entries
        Dim f1 As FileInfo = CType(file1, FileInfo)
        Dim f2 As FileInfo = CType(file2, FileInfo)

        Return -DateTime.Compare(f1.CreationTime, f2.CreationTime)  'Compare the file creation timestamps, newest first.
    End Function
End Class
 
Sounds good. I've just found the FileSystemWatcher control, you see the files in the array are the difference between two snapshots of a folder. I wonder if it would have been easier just using the FileSystemWatcher control to get this list.
 
But thanks for your example, i think that will do what i'm trying to achieve, just might have been easier for me to use the FileSystemWatcher in the first place. Ah well (in case you haven't guessed i've just crossed the line from vb to .net sorry for my inexperience)
 
No problem at all, I learned a little something myself in looking that up.

If your looking to constantly monitor a folder and refresh a picture box as new images are created then I think you would be better off executing a FSWatcher since its nearly instantaneous.


With my method you would load that code into a timer tick event set at a relatively low interval to achieve the same thing. Either would work, though the FSW would be more eloquent I believe.
 
Back
Top