dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Resolved [RESOLVED] Write folder contents to a textfile

    Hello, I was hoping someone could offer me a little help please, although I have been able to accomplish my aim of trying list all filenames in a folder into a textfile Iím sure there is a simpler way of doing this.

    My current code is as follows:-
    Code:
            ListBox1.Items.AddRange(Directory.GetFiles("C:\TEST"))
    
            Dim x As Integer
            For x = 0 To ListBox1.Items.Count - 1
                ListBox1.Items(x) = ListBox1.Items(x).substring(8)
            Next x
    
            Dim sb As New System.Text.StringBuilder()
            For Each o As Object In ListBox1.Items
                sb.AppendLine(o)
            Next
            System.IO.File.WriteAllText("C:\Path.txt", sb.ToString())
    In summary, first I get the list of files in the directory and these are written to the ListBox, I then remove the first 8 characters of each item in the listbox so only the filename remains and the drive/folder is deleted, I then write all contents of the ListBox to a textfile

    Iím pretty sure the above is possible without having to write to a listbox/text file and directly to a textfile, any help is appreciated.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,649

    Re: Write folder contents to a textfile

    vb.net Code:
    1. Dim folder As New DirectoryInfo("folder path here")
    2. Dim files = folder.GetFiles()
    3.  
    4. File.WriteAllLines("output file path here", files.Select(Function(fi) fi.Name))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Re: Write folder contents to a textfile

    Perfect, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width