nickmarshall
asked on
How could I output the results of a filesearch to a txt file
Hi,
I would like to output the results of a file search to a text file. Here is my code;
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
End Sub
Private Sub searchFiles()
Try
Dim drives() As String
Dim rootfolders() As String
Dim d As String
Dim f As String
drives = System.IO.Directory.GetLog icalDrives ()
For drivecount As Integer = 0 To drives.Length - 1
rootfolders = System.IO.Directory.GetDir ectories(d rives(driv ecount))
For rootFolderCount As Integer = 0 To rootfolders.Length - 1
DirSearch(rootfolders(root FolderCoun t))
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Dim d As String
Dim f As String
Dim lstFilesFound As ArrayList
Try
For Each d In System.IO.Directory.GetDir ectories(s Dir)
For Each f In System.IO.Directory.GetFil es(d, "*.Txt")
lstFilesFound.Add(f)
Next
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess age)
End Try
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
End Sub
Also, here is the loop to run through the files, but dont know where to add this and how to add output to it;
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
I would like to output the results of a file search to a text file. Here is my code;
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
End Sub
Private Sub searchFiles()
Try
Dim drives() As String
Dim rootfolders() As String
Dim d As String
Dim f As String
drives = System.IO.Directory.GetLog
For drivecount As Integer = 0 To drives.Length - 1
rootfolders = System.IO.Directory.GetDir
For rootFolderCount As Integer = 0 To rootfolders.Length - 1
DirSearch(rootfolders(root
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Dim d As String
Dim f As String
Dim lstFilesFound As ArrayList
Try
For Each d In System.IO.Directory.GetDir
For Each f In System.IO.Directory.GetFil
lstFilesFound.Add(f)
Next
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess
End Try
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
End Sub
Also, here is the loop to run through the files, but dont know where to add this and how to add output to it;
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
ASKER
This is how the code reads now.... quite a few errors occuring here;
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
Imports System.IO
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For count As Integer = 0 To lstFilesFound.Count
sr.WriteLine(lstFilesFound (count))
Next
sr.Close()
End Sub
Private Sub searchFiles()
Try
Dim drives() As String
Dim rootfolders() As String
Dim d As String
Dim f As String
drives = System.IO.Directory.GetLog icalDrives ()
For drivecount As Integer = 0 To drives.Length - 1
rootfolders = System.IO.Directory.GetDir ectories(d rives(driv ecount))
For rootFolderCount As Integer = 0 To rootfolders.Length - 1
DirSearch(rootfolders(root FolderCoun t))
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Dim d As String
Dim f As String
Dim lstFilesFound As ArrayList
Try
For Each d In System.IO.Directory.GetDir ectories(s Dir)
For Each f In System.IO.Directory.GetFil es(d, "*.Txt")
lstFilesFound.Add(f)
Next
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess age)
End Try
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
Imports System.IO
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For count As Integer = 0 To lstFilesFound.Count
sr.WriteLine(lstFilesFound (count))
Next
sr.Close()
End Sub
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
Imports System.IO
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For count As Integer = 0 To lstFilesFound.Count
sr.WriteLine(lstFilesFound
Next
sr.Close()
End Sub
Private Sub searchFiles()
Try
Dim drives() As String
Dim rootfolders() As String
Dim d As String
Dim f As String
drives = System.IO.Directory.GetLog
For drivecount As Integer = 0 To drives.Length - 1
rootfolders = System.IO.Directory.GetDir
For rootFolderCount As Integer = 0 To rootfolders.Length - 1
DirSearch(rootfolders(root
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Dim d As String
Dim f As String
Dim lstFilesFound As ArrayList
Try
For Each d In System.IO.Directory.GetDir
For Each f In System.IO.Directory.GetFil
lstFilesFound.Add(f)
Next
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess
End Try
For count As Integer = 0 To lstFilesFound.Count
Dim path As String = lstFilesFound(count)
Next
Imports System.IO
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For count As Integer = 0 To lstFilesFound.Count
sr.WriteLine(lstFilesFound
Next
sr.Close()
End Sub
"Imports" statements always have to be the first lines of your code....
You're also going to run into problems with your code, because you'll probably end up with file confilcts writing this way.
I've rewritten the code for you, using VS2005. If you're not using 2005, you won't be able to use the generic collections -- if so, let me know, and I'll make it 2003 code.
Imports System.IO
Imports System.Collections.Generic
Public Class Form1
Private fileList As List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
OutputFileNames()
End Sub
Private Sub OutputFileNames()
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For Each file As String In fileList
sr.WriteLine(file)
Next
sr.Close()
End Sub
Private Sub searchFiles()
Try
For Each drive As String In System.IO.Directory.GetLog icalDrives ()
For Each rootFolder As String In System.IO.Directory.GetDir ectories(d rive)
DirSearch(rootFolder)
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Try
For Each dir As String In System.IO.Directory.GetDir ectories(s Dir)
For Each file As String In System.IO.Directory.GetFil es(dir, "*.Txt")
fileList.Add(file)
Next
DirSearch(dir)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess age)
End Try
End Sub
End Class
You're also going to run into problems with your code, because you'll probably end up with file confilcts writing this way.
I've rewritten the code for you, using VS2005. If you're not using 2005, you won't be able to use the generic collections -- if so, let me know, and I'll make it 2003 code.
Imports System.IO
Imports System.Collections.Generic
Public Class Form1
Private fileList As List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
searchFiles()
OutputFileNames()
End Sub
Private Sub OutputFileNames()
Dim fileName As String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For Each file As String In fileList
sr.WriteLine(file)
Next
sr.Close()
End Sub
Private Sub searchFiles()
Try
For Each drive As String In System.IO.Directory.GetLog
For Each rootFolder As String In System.IO.Directory.GetDir
DirSearch(rootFolder)
Next
Next
Catch ex As Exception
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Try
For Each dir As String In System.IO.Directory.GetDir
For Each file As String In System.IO.Directory.GetFil
fileList.Add(file)
Next
DirSearch(dir)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess
End Try
End Sub
End Class
ASKER
Ahh seems to work abit more this one.
I get NullReferenceException was unhandled on line;
For Each file As String In fileList
I get NullReferenceException was unhandled on line;
For Each file As String In fileList
You got nothing back from one of your file searches...
Sub DirSearch(ByVal sDir As String)
Try
For Each dir As String In System.IO.Directory.GetDir ectories(s Dir)
Dim files as String() = System.IO.Directory.GetFil es(dir, "*.Txt")
if not files is nothing then
For Each file As String In files
fileList.Add(file)
Next
end if
DirSearch(dir)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess age)
End Try
End Sub
Sub DirSearch(ByVal sDir As String)
Try
For Each dir As String In System.IO.Directory.GetDir
Dim files as String() = System.IO.Directory.GetFil
if not files is nothing then
For Each file As String In files
fileList.Add(file)
Next
end if
DirSearch(dir)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Mess
End Try
End Sub
ASKER
Again, same error with new code.
The output txt file is being created :-/
The output txt file is being created :-/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
filelist is nothing error now... again the output.txt does not show anything.
??
??
ASKER
filelist does not appear to have been declared as anything.
Would this be;
Dim filelist as string in "c:\output.txt"
Would this be;
Dim filelist as string in "c:\output.txt"
No...
It would be :
Public Class Form1
Private fileList As New List(Of String) ' <-- added new
It would be :
Public Class Form1
Private fileList As New List(Of String) ' <-- added new
Dim fileName as String = "C:\output.txt"
Dim sr As New StreamWriter(fileName)
For count As Integer = 0 To lstFilesFound.Count
sr.WriteLine( lstFilesFound(count))
Next
sr.Close