how to read all file name in a folder

i want to read all file names in any folder and store in the excel file so how i can do this


Answer this question

how to read all file name in a folder

  • Gianpi

    The above will create a csv file which excel can read in but alternatively if you want to create a excel file (xls)

    The following statement constructs an array for file names from the folder
    Dim files() As String = System.IO.Directory.GetFiles("c:\myfolder")

    For Each s as string in files

    'Write an excel cell.

    Next

    and then an interesting little article will show you how to write Excel spreadsheet directly from VB - all you would be doing is iterating through the array as above - but in the write an excel cell - you would be writing the variable s to a cell in the spreadsheet


    How to automate Excel from VB.Net
    http://support.microsoft.com/kb/q301982/


  • Scott_P

    Another alternative, not requiring Excel installed on the computer - a while ago I came across this library, which seemed very useful for creating excel files.

    Andrej



  • andyedw

    i m getting close to achive my output. can anyone help me...how to add file names in excel

    Dim file As New FileInfo(path)

    Dim folder As New DirectoryInfo(path)

    Dim cellCount As Integer = 1

    Dim f As FileInfo

    For Each file In folder.GetFiles()

    Try

    ObjWSheet.Range("A" & cellCount).Value() = f.Name

    cellCount += 1

    Catch exc As Exception

    MsgBox(exc.Message)

    End Try

    Next



  • Mr.Yesser

    The easiest solution would be reading all filenames in a folder than write those in a separate .csv file so Excel could easily read it:

    Dim files() As String = System.IO.Directory.GetFiles("c:\myfolder")

    Dim text = String.Join(Environment.NewLine, files)

    System.IO.File.WriteAllText("c:\files.csv", text)

    Andrej



  • how to read all file name in a folder