Results 1 to 7 of 7

Thread: Reading a Directory//Writing to a Text File?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154

    Reading a Directory//Writing to a Text File?

    Hey everyone,

    Can someone tell me how I can read the contents of a directory...then insert each file name in that directory into a text file??

    The reason is I have like 600 Mp3's and a friends wants them...and I dont want to have to type each one out...so this way it will create a list of the songs and then I can give her that.

    Thanks in advance!!

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Use DirectoryInfo and FileInfo classes. Very Easy.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here you go:

    Code:
    private void button1_Click(object sender, System.EventArgs e) {
        DirectoryInfo dir = new DirectoryInfo(@"c:\music");
        StreamWriter stream = new StreamWriter(@"c:\music list.txt");
        foreach(FileInfo f in dir.GetFiles()) {
            stream.WriteLine(f.Name);
        }
        stream.Close();
    }

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    Thanks for the reply...

    but I sitll cant get it to work....first thing is I'm doing it in VB.net not C..

    But I converted it to this.

    Code:
    Dim Dir As New System.IO.DirectoryInfo("C:\Downloads\Mp3's")
            Dim F As New System.IO.FileInfo("C:\Downloads\Mp3's\New ****\Eminem - Lose Yourself")
                   Dim Writer As New System.IO.StreamWriter("C:\New Songs.txt")
    
            For Each F In Dir.GetFiles
                Writer.WriteLine(F.Name)
            Next
            Writer.Close()
    You never posted any code about the fileinfo delcarations and I think that is where my problem is??

    So I want to have a list of all the songs that are in "C:\Downloads\Mp3's"

    Thanks again.

  5. #5
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    You could do it the old fashion way and use DOS commands

    PHP Code:
    dir *.mp3 mysongs.txt 
    -Shurijo

  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    oi, dos commands = evil

    here you go

    VB Code:
    1. Private Sub Find_Mp3(ByVal startDir As String)
    2.         Dim dir As System.IO.DirectoryInfo, file As System.IO.FileInfo
    3.         Dim stream As System.IO.StreamWriter
    4.  
    5.         dir = New System.IO.DirectoryInfo(startDir)
    6.         stream = New System.IO.StreamWriter(startDir & "Music list.txt")
    7.         For Each file In dir.GetFiles
    8.             If file.Extension = ".mp3" Then stream.WriteLine(file.Name)
    9.         Next
    10.         stream.Close()
    11.  
    12.     End Sub
    called like such
    VB Code:
    1. Find_Mp3("C:\Mp3's\New Found Glory\")

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You may like to go to sub directories as well.
    Try this:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim outfile As StreamWriter= New StreamWriter("C:\test.txt")
    3.         Me.Cursor.Current = Cursors.WaitCursor
    4.         tree("E:\", outfile)
    5.         outfile.Close()
    6.         Me.Cursor.Current = Cursors.Default
    7. End Sub
    8.  
    9. Private Sub tree(ByVal dr As String, ByVal outfile As StreamWriter)
    10.         Dim maindir As DirectoryInfo = New DirectoryInfo(dr)
    11.         Dim subfd As FileSystemInfo
    12.         outfile.WriteLine(maindir.FullName)
    13.         Try
    14.             For Each subfd In maindir.GetFileSystemInfos
    15.                 If TypeOf subfd Is FileInfo Then
    16.                     If subfd.Extension = ".mp3" Then
    17.                         outfile.WriteLine(subfd.Name)
    18.                     End If
    19.                 Else
    20.                         tree(subfd.FullName, outfile)
    21.                 End If
    22.             Next
    23.         Catch
    24.         End Try
    25. End Sub
    Be sure to import System.IO
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

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



Click Here to Expand Forum to Full Width