Link to home
Start Free TrialLog in
Avatar of nickmarshall
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.GetLogicalDrives()
            For drivecount As Integer = 0 To drives.Length - 1
                rootfolders = System.IO.Directory.GetDirectories(drives(drivecount))
                For rootFolderCount As Integer = 0 To rootfolders.Length - 1
                    DirSearch(rootfolders(rootFolderCount))
                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.GetDirectories(sDir)
                For Each f In System.IO.Directory.GetFiles(d, "*.Txt")
                    lstFilesFound.Add(f)
                Next
                DirSearch(d)
            Next
        Catch excpt As System.Exception
            Debug.WriteLine(excpt.Message)
        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



Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

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
Avatar of nickmarshall
nickmarshall

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.GetLogicalDrives()
            For drivecount As Integer = 0 To drives.Length - 1
                rootfolders = System.IO.Directory.GetDirectories(drives(drivecount))
                For rootFolderCount As Integer = 0 To rootfolders.Length - 1
                    DirSearch(rootfolders(rootFolderCount))
                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.GetDirectories(sDir)
                For Each f In System.IO.Directory.GetFiles(d, "*.Txt")
                    lstFilesFound.Add(f)
                Next
                DirSearch(d)
            Next
        Catch excpt As System.Exception
            Debug.WriteLine(excpt.Message)
        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


"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.GetLogicalDrives()
        For Each rootFolder As String In System.IO.Directory.GetDirectories(drive)
          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.GetDirectories(sDir)
        For Each file As String In System.IO.Directory.GetFiles(dir, "*.Txt")
          fileList.Add(file)
        Next
        DirSearch(dir)
      Next
    Catch excpt As System.Exception
      Debug.WriteLine(excpt.Message)
    End Try
  End Sub
End Class
Ahh seems to work abit more this one.

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.GetDirectories(sDir)
Dim files as String() = System.IO.Directory.GetFiles(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.Message)
    End Try
  End Sub
Again, same error with new code.

The output txt file is being created :-/
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
filelist is nothing error now... again the output.txt does not show anything.

??
filelist does not appear to have been declared as anything.

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