Close

Results 1 to 5 of 5
  1. #1
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default vb.net - Ignoring protected files on open?

    All,

    I have written a program to recursively search files in directories of a drive to look for some text (As the Microsoft search is shite!) within the file.
    You enter a drive letter, then a string to search for. The program then scans the directory(s) and opens the files within. At this point it will do an InStr() to look for my stirng I have entered.

    It works very very well and is very fast! However I have a problem when I hit files that cannot be opened for whatever reason...

    I have used the 'ReadOnly' command to try and identify any files that cannot be opened but this does not work...

    Any ideas? Basically it crashes when it hits a file it cannot open which is annoying. I have had to tell it to avoid certain files so I can run my search for my current query, however this is going to stop me from doing any other search where I will hit files that cannot be opened.

    Cheers in advance.

    Code:
    Spoiler:
    Code:
    Imports System
    Imports System.IO
    Imports System.Text.RegularExpressions
    Imports System.Collections.Specialized
    
    Public Class MainClass
        Shared compare As String
        Shared path As String
        Shared Sub Main()
            Dim nameOfDirectory As String
            Dim myDirectory As DirectoryInfo
            Console.WriteLine("This program will search for text within files on a drive/directory recursively.")
            Console.WriteLine("")
            Console.Write("Please enter the path you wish to recursively search - e.g C:\ > ")
            path = Console.ReadLine()
            nameOfDirectory = path
            Console.WriteLine("")
            Console.Write("Please enter the string to search for > ")
            compare = Console.ReadLine()
    
            myDirectory = New DirectoryInfo(nameOfDirectory)
            WorkWithDirectory(myDirectory)
    
            Console.WriteLine("")
            Console.WriteLine("Search Finished... Press any key to exit.")
            Console.ReadLine()
        End Sub
    
        Public Shared Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
            Dim nextDir As DirectoryInfo
            WorkWithFilesInDir(aDir)
            For Each nextDir In aDir.GetDirectories
                WorkWithDirectory(nextDir)
            Next
        End Sub
    
        Public Shared Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
            Dim aFile As FileInfo
            Dim sLine As String
            Dim FileContent As String
    
            Dim arrText As New ArrayList()
    
            For Each aFile In aDir.GetFiles()
                FileContent = aFile.FullName
                If LCase(FileContent) = LCase("V:\data\params") Or LCase(FileContent) = LCase("V:\data\versioncache") Or LCase(FileContent) = LCase("V:\data\template\en\custom\bug\edit.html.tmpl") Or LCase(FileContent) = LCase("V:\data\template\en\custom\bug\create\create.html.tmpl") Or LCase(FileContent) = LCase("V:\data\template\en\custom\dv\dv.html.tmpl") Then
                Else
                    Dim objReader As New IO.StreamReader(FileContent)
    
                    Console.WriteLine(aFile.FullName)
                    aFile.OpenRead()
                    sLine = objReader.ReadToEnd
                    objReader.Close()
                    arrText.Add(sLine)
                    If InStr(LCase(sLine), LCase(compare)) Then
                        Console.WriteLine("")
                        Console.WriteLine("File " & FileContent & " contains the string " & LCase(compare))
                        Console.WriteLine(" - Press any key to continue searching...")
                        Console.ReadLine()
                    End If
                End If
    
            Next
        End Sub
    
    
    End Class
    We all make mistakes sometimes

  2. #2
    DF VIP Member big man's Avatar
    Join Date
    Jul 2002
    Location
    The big smoke
    Posts
    2,824
    Thanks
    8
    Thanked:        0
    Karma Level
    445

    Default Re: vb.net - Ignoring protected files on open?

    i've run into this before, and the only simple way you can do it in vb.net (if anyone knows better, please correct me) is to place the open code in WorkWithFilesInDir into a try catch block.

    At least that way if you hit a locked file it won't bomb out. Not the most scientific method, but it will stop you getting errors
    I am a loud man with a very large hat. This means I am in charge

    Never argue with an idiot. They will bring you down to their level, then beat you with experience.

  3. #3
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: vb.net - Ignoring protected files on open?

    Sounds like a plan, i'll try it when I get to work and report back.

    Cheers, but any other ideas would be appreciated.
    We all make mistakes sometimes

  4. #4
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: vb.net - Ignoring protected files on open?

    Top man, that sorted it, I used it in the following procedure:

    Code:
            For Each aFile In aDir.GetFiles()
                Try
                    FileContent = aFile.FullName
                    Dim objReader As New IO.StreamReader(FileContent)
                    Console.WriteLine(aFile.FullName)
                    aFile.OpenRead()
                    sLine = objReader.ReadToEnd
                    objReader.Close()
                    arrText.Add(sLine)
                    If InStr(LCase(sLine), LCase(compare)) Then
                        Console.WriteLine("")
                        Console.WriteLine("File " & FileContent & " contains the string " & LCase(compare))
                        Console.WriteLine(" - Press any key to continue searching...")
                        Console.ReadLine()
                    End If
                Catch
                End Try
            Next
    We all make mistakes sometimes

  5. #5
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: vb.net - Ignoring protected files on open?

    Right here is the finished product (For now) if anyone wants to try it.

    It won't work on Vista unless you turn off the permissions warning (you know the one where it wants permission all the time to install or create a file etc...) as I use tmp files.

    It will list files that could not be opened and a list of the ones with the string found if at all.
    May come in handy for some folks? I appreciate I have used two variables for one instance but never mind...

    Spoiler:
    Code:
    'Copyright Goldy 2007
    
    Imports System
    Imports System.IO
    Imports System.Text.RegularExpressions
    Imports System.Collections.Specialized
    
    Public Class MainClass
        Shared compare As String
        Shared path As String
        Shared ErrorList As String = ""
        Shared ErrorReport As Boolean = False
        Shared Found As Boolean = False
    
        Shared Sub Main()
            Dim nameOfDirectory As String
            Dim myDirectory As DirectoryInfo
            Dim sLine As String
    
            Console.WriteLine("This program will search for text within files on a drive/directory recursively.")
            Console.WriteLine("It will list the location of any found files during the search.")
            Console.WriteLine("")
            Console.Write("Please enter the drive/path you wish to recursively search - e.g C:\ > ")
            path = Console.ReadLine()
            nameOfDirectory = path
            Console.WriteLine("")
            Console.Write("Please enter the string to search for (Not case sensitive) > ")
            compare = Console.ReadLine()
    
            myDirectory = New DirectoryInfo(nameOfDirectory)
            WorkWithDirectory(myDirectory)
    
            Console.WriteLine("")
            Console.WriteLine("Search Finished...")
            Console.WriteLine("")
            'After the search check that any files could not be opened. If so then we need to list the files.
            If ErrorReport = True Then
                Console.WriteLine("Could not open the following files: " & ErrorList)
                Console.WriteLine("")
                Dim objReader As New IO.StreamReader("C:\tmpList.txt")
                Do
                    'Reads through and writes out to the console, eliminating any blank lines
                    sLine = objReader.ReadLine()
                    If sLine <> "" Then
                        Console.WriteLine(sLine)
                    End If
                Loop Until sLine Is Nothing
                objReader.Close()
            Else
                Console.WriteLine("No files could not be opened.")
            End If
    
            Console.WriteLine("")
    
            If Found = True Then
                Console.WriteLine("String found in following location(s): ")
                Console.WriteLine("")
                Dim objReader2 As New IO.StreamReader("C:\searchList.txt")
                Do
                    'Reads through and writes out to the console, eliminating any blank lines
                    sLine = objReader2.ReadLine()
                    If sLine <> "" Then
                        Console.WriteLine(sLine)
                    End If
                Loop Until sLine Is Nothing
                objReader2.Close()
            Else
                Console.WriteLine("String not found in any files.")
            End If
    
            Try
                'Deletes the temp file. The try is to avoid a crash.
                Kill("C:\tmplist.txt")
            Catch
            End Try
            Try
                'Deletes the temp file. The try is to avoid a crash.
                Kill("C:\searchlist.txt")
            Catch
            End Try
    
            Console.WriteLine("")
            Console.WriteLine("Press any key to exit.")
            Console.ReadLine()
        End Sub
    
        Public Shared Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
            'Not my code - Gets all directories recursively within a path.
            Dim nextDir As DirectoryInfo
            WorkWithFilesInDir(aDir)
            For Each nextDir In aDir.GetDirectories
                WorkWithDirectory(nextDir)
            Next
        End Sub
    
        Public Shared Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
            'Gets all files recursively within a path. Opens them and reads the full file. Then looks for compare string.
            Dim aFile As FileInfo
            Dim sLine As String
            Dim FileContent As String
            Dim arrText As New ArrayList()
    
            For Each aFile In aDir.GetFiles()
                Try
                    FileContent = aFile.FullName
                    Dim objReader As New IO.StreamReader(FileContent)
                    Console.WriteLine(aFile.FullName)
                    aFile.OpenRead()
                    sLine = objReader.ReadToEnd
                    objReader.Close()
                    arrText.Add(sLine)
                    'Checks and reports the path if it exists.
                    If InStr(LCase(sLine), LCase(compare)) Then
                        Console.WriteLine("")
                        Console.WriteLine("File " & FileContent & " contains the string " & LCase(compare))
                        Found = True
                        Dim objReader3 As New IO.StreamWriter("C:\searchlist.txt", True)
                        objReader3.WriteLine(aFile.FullName & vbLf)
                        objReader3.Close()
                    End If
                Catch
                    'If a file cannot be opened write to a file so we can display at the end of the search.
                    Console.WriteLine("Cannot open file " & aFile.FullName)
                    'Set boolean to true so we know to list the files later.
                    ErrorReport = True
                    Dim objReader2 As New IO.StreamWriter("C:\tmplist.txt", True)
                    objReader2.WriteLine(aFile.FullName & vbLf)
                    objReader2.Close()
                End Try
            Next
        End Sub
    
    
    End Class
    We all make mistakes sometimes

Similar Threads

  1. Open Links in Popup window?
    By EUPHORiA in forum Website Coding & Graphics
    Replies: 10
    Last Post: 1st November 2002, 06:12 PM
  2. open membership?
    By psxcity in forum Forum Suggestions & Feedback
    Replies: 11
    Last Post: 13th October 2002, 12:37 AM
  3. DOSPROG doesn't like 6in1 hex files/Satkoos
    By jo.cassady in forum Digital Satellite TV
    Replies: 7
    Last Post: 7th October 2002, 10:29 PM
  4. copying protected cd's
    By joeninety0 in forum PC Gaming
    Replies: 14
    Last Post: 5th September 2002, 09:56 PM
  5. Thoughts on Open University
    By N3R0 in forum The Dog and Duck
    Replies: 24
    Last Post: 31st August 2002, 01:18 AM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •