Search code examples
vb.netloopsfor-loopdirectorystreamreader

How to loop files in a directory and export in DGV in VB.NET


I have my code:

    For i As Integer = 0 To lstDir.Items.Count - 1  
         For Each File As String In Directory.GetFiles(lstDir.Items(i))
              Fname = File.ToString.Substring(File.LastIndexOf("\") + 1)
              i = +1

              If File.Contains(".zip") Then
                  res = ""
                  For Each Str As Char In Fname
                      If IsNumeric(Str) Then
                         res = res & Str
                      End If
                  Next

                  For x As Integer = 0 To lstDir.Items.Count - 1
                      For Each newFile As String In Directory.GetFiles(lstDir.Items(x))
                          If newFile.Contains(res) Then
                             dgContents.Rows.Add(Fname)
                          End If
                       Next
                  Next

              End If

    Next
Next

suppose my directory contains these files:

abc123.pdf
def456.zip
ghi123.zip
jkl123.xml
mno456.pdf
pqr456.xml

I need to select just all .zip files, get the numbers in the zip filename, and loop again on the directory to search files with the same number from the zip then add it to datagrid. My problem is, my code displays output like this:

def456.zip
def456.zip
def456.zip
ghi123.zip
ghi123.zip
ghi123.zip

instead of

def456.zip
mno456.pdf
pqr456.xml 
ghi123.zip
abc123.pdf
jkl123.xml

please help...


Solution

  • I don't know if I understand you. I propose you to use next code.

    This Sub is for looping through files with a .zip extension in a given path:

    Imports System.IO
    
         Public Sub GetFilesFromDir(path As String)
                    ' Make a reference to a directory.
                    Dim di As New DirectoryInfo(path)
                    ' Get a reference to each file in that directory.
                    Dim fiArr As FileInfo() = di.GetFiles()
                    ' Display the names of the files.
                    Dim file As FileInfo
                    Dim fileName As String = ""
                    For Each file In fiArr
                        If file.Extension = ".zip" Then
                            Console.WriteLine("------------- {0} -----------", file.Name)
                            fileName = System.IO.Path.GetFileNameWithoutExtension(path & "\" & file.Name)
                            DisplayFilesByNumber(fiArr, fileName)
                        End If
                    Next
         End Sub
    

    The DisplayFilesByNumber is a Sub where the loop through files which contains a given string (in this case, the number in the fileName) is done.

    Public Sub DisplayFilesByNumber(fiArr As FileInfo(), fileName As String)
            For Each fileByNumber In fiArr
                If fileByNumber.Name.Contains(GetNumbers(fileName)) Then
                    Console.WriteLine(fileByNumber)
                End If
            Next
    End Sub
    

    This Sub needs a simple function to get the number in the fileName.

    Imports.Text.RegularExpressions
    
        Public Function GetNumbers(fileName As String) As String
                GetNumbers = Regex.Replace(fileName, "[^0-9]", String.Empty)
        End Function
    

    This gives next output:

    ------------- def456.zip -----------
    def456.zip
    mno456.pdf
    pqr456.xml
    ------------- ghi123.zip -----------
    abc123.pdf
    ghi123.zip
    jkl123.xml
    

    I hope I understood you correctly. I'm not storing values in a DGV but I'm sure you can cope with this.