Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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



0
nickmarshall
Asked:
nickmarshall
  • 5
  • 5
1 Solution
 
Jeff CertainCommented:
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
0
 
nickmarshallAuthor Commented:
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


0
 
Jeff CertainCommented:
"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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nickmarshallAuthor Commented:
Ahh seems to work abit more this one.

I get NullReferenceException was unhandled on line;

For Each file As String In fileList
0
 
Jeff CertainCommented:
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
0
 
nickmarshallAuthor Commented:
Again, same error with new code.

The output txt file is being created :-/
0
 
Jeff CertainCommented:
Okay... let's validate the dir list as well

  Sub DirSearch(ByVal sDir As String)
    Try
      Dim directories As String() = System.IO.Directory.GetDirectories(sDir)
      If Not directories Is Nothing Then
        For Each dir As String In directories
          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
      End If
    Catch excpt As System.Exception
      Debug.WriteLine(excpt.Message)
    End Try
  End Sub
0
 
nickmarshallAuthor Commented:
filelist is nothing error now... again the output.txt does not show anything.

??
0
 
nickmarshallAuthor Commented:
filelist does not appear to have been declared as anything.

Would this be;

Dim filelist as string in "c:\output.txt"
0
 
Jeff CertainCommented:
No...

It would be :

Public Class Form1
  Private fileList As New List(Of String) ' <-- added new
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now