|
-
Apr 16th, 2003, 02:23 AM
#1
Thread Starter
Addicted Member
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!!
-
Apr 16th, 2003, 03:49 AM
#2
Frenzied Member
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
-
Apr 16th, 2003, 07:53 AM
#3
PowerPoster
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();
}
-
Apr 16th, 2003, 12:48 PM
#4
Thread Starter
Addicted Member
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.
-
Apr 16th, 2003, 01:33 PM
#5
Addicted Member
You could do it the old fashion way and use DOS commands
PHP Code:
dir *.mp3 > mysongs.txt
-
Apr 16th, 2003, 01:46 PM
#6
Hyperactive Member
oi, dos commands = evil
here you go
VB Code:
Private Sub Find_Mp3(ByVal startDir As String)
Dim dir As System.IO.DirectoryInfo, file As System.IO.FileInfo
Dim stream As System.IO.StreamWriter
dir = New System.IO.DirectoryInfo(startDir)
stream = New System.IO.StreamWriter(startDir & "Music list.txt")
For Each file In dir.GetFiles
If file.Extension = ".mp3" Then stream.WriteLine(file.Name)
Next
stream.Close()
End Sub
called like such
VB Code:
Find_Mp3("C:\Mp3's\New Found Glory\")
-
Apr 16th, 2003, 01:57 PM
#7
Frenzied Member
You may like to go to sub directories as well.
Try this:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim outfile As StreamWriter= New StreamWriter("C:\test.txt")
Me.Cursor.Current = Cursors.WaitCursor
tree("E:\", outfile)
outfile.Close()
Me.Cursor.Current = Cursors.Default
End Sub
Private Sub tree(ByVal dr As String, ByVal outfile As StreamWriter)
Dim maindir As DirectoryInfo = New DirectoryInfo(dr)
Dim subfd As FileSystemInfo
outfile.WriteLine(maindir.FullName)
Try
For Each subfd In maindir.GetFileSystemInfos
If TypeOf subfd Is FileInfo Then
If subfd.Extension = ".mp3" Then
outfile.WriteLine(subfd.Name)
End If
Else
tree(subfd.FullName, outfile)
End If
Next
Catch
End Try
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|