[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Visual Basic 2008 - read file names and date modified into array

Dim Dire As String() = IO.Directory.GetDirectories(mRootPath, "*.*", IO.SearchOption.AllDirectories)  

The above code creates an array of sub directory names inside the directory contained in the variable mRootPath.

I am interest in the code to create an array of the name and date modified of all the files inside a specified directory.



0
dastaub
Asked:
dastaub
  • 5
  • 4
1 Solution
 
käµfm³d 👽Commented:
How about this:

Dim fileData As New System.Collections.Generic.Dictionary(Of String, Date)()

For Each fi As System.IO.FileInfo In (New System.IO.DirectoryInfo(mRootPath)).GetFiles()
    fileData.Add(fi.Name, fi.LastWriteTime)
Next

Open in new window

0
 
käµfm³d 👽Commented:
...and if you want files from all sub-directories included, you can use the same options as you posted:

...
For Each fi As System.IO.FileInfo In (New System.IO.DirectoryInfo(mRootPath)).GetFiles("*", IO.SearchOption.AllDirectories)
...

Open in new window

0
 
dastaubAuthor Commented:
what would be the loop for looping through the collection of file names and dates?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
You can do:

For Each key As String in fileData.Keys
    Console.WriteLine("File: {0} - Last Modified: {1}", key, fileData(key))
Next

Open in new window

0
 
dastaubAuthor Commented:
i tried the code, it appears to get the first file name and then drop out of the loop?
0
 
käµfm³d 👽Commented:
Are you sure you have more than one file in the director? Here is what I get when I run the code against my Windows directory (using the non-recursive version for brevity):


Untitled.png
0
 
käµfm³d 👽Commented:
The GetFiles call could also be throwing an exception, so you should really wrap that logic in a Try/Catch:

Try
	For Each fi As System.IO.FileInfo In (New System.IO.DirectoryInfo(mRootPath)).GetFiles()
		fileData.Add(fi.Name, fi.LastWriteTime)
	Next
Catch ex As ArgumentNullException
	Console.WriteLine(ex.ToString())
Catch ex As ArgumentException
	Console.WriteLine(ex.ToString())
Catch ex As System.IO.DirectoryNotFoundException
	Console.WriteLine(ex.ToString())
Catch ex As Security.SecurityException
	Console.WriteLine(ex.ToString())
End Try

Open in new window

0
 
dastaubAuthor Commented:
below is the code.  it gives me 1 file name and date and drops out of loop.

        Dim fileData As New System.Collections.Generic.Dictionary(Of String, Date)()

        Try
            For Each fi As System.IO.FileInfo In New System.IO.DirectoryInfo("c:\windows").GetFiles()
                fileData.Add(fi.Name, fi.LastWriteTime)
            Next
        Catch ex As ArgumentNullException
            Console.WriteLine(ex.ToString())
        Catch ex As ArgumentException
            Console.WriteLine(ex.ToString())
        Catch ex As System.IO.DirectoryNotFoundException
            Console.WriteLine(ex.ToString())
        Catch ex As Security.SecurityException
            Console.WriteLine(ex.ToString())
        End Try
0
 
dastaubAuthor Commented:
no error messages, it simply drops out
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now