Hi all,

I have a procedure that compares release dates to file download dates. I have a download folder that is added to a string

Dim strFilePath = "\EconomicDBTemp\DownloadedCSVs\"

I've removed the directory but the folder DownloadedCSVs is where I need to search each csv file. However, I am unsure as to how to loop through the folder and look at each file in that folder. Can someone sort me out with this?

Thanks

You can do something like this:

   Dim di As New IO.DirectoryInfo(YourDirectoryHere)
   Dim diar1 As IO.FileInfo() = di.GetFiles()
   Dim dra As IO.FileInfo

   For Each dra In diar1
       If dra.Extension = ".csv" Then      
           'Do Work
       End If
   Next

Thanks Begginnerdev, just one more question that is relevant to your solution. Originally, I was using one file to get its creation date in a line like:

Dim fileCreatedDate As DateTime '= File.GetCreationTime(strFilePath + "\CPI1.csv")

How do I apply that in the 'Do Work of your solution. I was going to attempt:

Dim fileCreatedDate As DateTime
For Each dra In diar1
    If dra.Extension = ".csv" Then
        fileCreatedDate = File.GetCreationTime(dra)
    End If
Next

but it won't convert that to a string.

Nevermind, I figured that one out with:

For Each dra In diar1
    If dra.Extension = ".csv" Then
       fileCreatedDate = dra.CreationTime
    End If
Next

You will want to make use of the FullName property.

Example:

   Dim di As New IO.DirectoryInfo(YourDirectoryHere)
   Dim diar1 As IO.FileInfo() = di.GetFiles()
   Dim dra As IO.FileInfo

   For Each dra In diar1
      If dra.Extension = ".csv" Then
          fileCreatedDate = File.GetCreationTime(dra.FullName) 
      End If
   Next

Or more concisely

Dim folder As String = "D:\temp"

For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchTopLevelOnly, "*.csv")
    Debug.WriteLine(file)
    'do work
Next

Thanks to the both of you! I don't know how I would learn this if not for this site and you avid helpers.

The all mighty Google, my friend. :)

Google is good but it usually just leads to sites like this anyways. Besides, I tried to use the Googles first for this question but the query came back with different types of issues that did not suit my own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.