Solved

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

Posted on 2006-07-21
10
176 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

16 Experts available now in Live!

Get 1:1 Help Now