Solved

How could I output the results of a filesearch to a txt file

Posted on 2006-07-21
10
177 Views
Last Modified: 2012-05-05
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
Comment
Question by:nickmarshall
  • 5
  • 5
10 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17154150
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
 
LVL 1

Author Comment

by:nickmarshall
ID: 17154807
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17154913
"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
 
LVL 1

Author Comment

by:nickmarshall
ID: 17155008
Ahh seems to work abit more this one.

I get NullReferenceException was unhandled on line;

For Each file As String In fileList
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17155041
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:nickmarshall
ID: 17155089
Again, same error with new code.

The output txt file is being created :-/
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 250 total points
ID: 17155133
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
 
LVL 1

Author Comment

by:nickmarshall
ID: 17165971
filelist is nothing error now... again the output.txt does not show anything.

??
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17165987
filelist does not appear to have been declared as anything.

Would this be;

Dim filelist as string in "c:\output.txt"
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17166978
No...

It would be :

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now