[HELP] Searching Files

Posts 1–15 of 15 · Page 1 of 1
[HELP] Searching Files
I try this:

Code:
 Dim files As ReadOnlyCollection(Of String)
        files = My.Computer.FileSystem.FindInFiles("C:\", TextBox1.Text, False, FileIO.SearchOption.SearchAllSubDirectories, "*.txt")
        For Each FilexD As String In files
            ListBox1.Items.Add(FilexD)
        Next
But the problem is , In C:\ have some denied directories (Open as admin do not work), HOw can i make it ignore it, if is not allowed ? ( Or something)

Thanks.
On Error Resume Next?
Quote Originally Posted by user782106 View Post
On Error Resume Next
Not that easy, If you suggest something, Test it first then show me how. Please.
Code:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;306666
[highlight=vb.net]Try
ListBox1.Items.Add(FilexD)
Catch
End Try[/highlight]
Quote Originally Posted by master131 View Post
[highlight=vb.net]Try
ListBox1.Items.Add(FilexD)
Catch
End Try[/highlight]
Doesn't work I'm pretty sure, since your 'for' statement is the first thing to try and access the file.

Want proof? Try running just this statement.

[highlight=vb.net]
FileIO.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories)
[/highlight]
Why not list every file, then remove the ones not containing the string? Seems like an easy temporary workaround.
Quote Originally Posted by user782106 View Post
Why not list every file, then remove the ones not containing the string? Seems like an easy temporary workaround.
-.- , It dont even can add the denied directories, Thats my problem.
If possible i add them all, But i cant.
HERE, use this. Works for me.

Add this to a CLASS file

Code:
Option Strict On
Option Explicit On

Imports System.IO

Public Class FileSearch

    Private Const DefaultFileMask As String = "*.*"
    Private Const DefaultDirectoryMask As String = "*"

#Region " Member Variables "

    Private _InitialDirectory As DirectoryInfo
    Private _DirectoryMask As String
    Private _FileMask As String

    '
    Private _Directories As New ArrayList
    Private _Files As New ArrayList

#End Region

#Region " Properites "

    Public Property InitialDirectory() As DirectoryInfo
        Get
            Return _InitialDirectory
        End Get
        Set(ByVal Value As DirectoryInfo)
            _InitialDirectory = Value
        End Set
    End Property
    Public Property DirectoryMask() As String
        Get
            Return _DirectoryMask
        End Get
        Set(ByVal Value As String)
            _DirectoryMask = Value
        End Set
    End Property
    Public Property FileMask() As String
        Get
            Return _FileMask
        End Get
        Set(ByVal Value As String)
            _FileMask = Value
        End Set
    End Property

    Public ReadOnly Property Directories() As ArrayList
        Get
            Return _Directories
        End Get
    End Property
    Public ReadOnly Property Files() As ArrayList
        Get
            Return _Files
        End Get
    End Property



#End Region

#Region " Constructors "

    Public Sub New()

    End Sub

    Public Sub New( _
        ByVal BaseDirectory As String, _
        Optional ByVal FileMask As String = DefaultFileMask, _
        Optional ByVal DirectoryMask As String = DefaultDirectoryMask)

        Me.New(New IO.DirectoryInfo(BaseDirectory), FileMask, DirectoryMask)

    End Sub

    Public Sub New( _
        ByVal BaseDirectory As DirectoryInfo, _
        Optional ByVal FileMask As String = DefaultFileMask, _
        Optional ByVal DirectoryMask As String = DefaultDirectoryMask)

        _InitialDirectory = BaseDirectory
        _FileMask = FileMask
        _DirectoryMask = DirectoryMask

    End Sub

#End Region

    Protected Overrides Sub Finalize()
        _Files = Nothing
        _Directories = Nothing
        MyBase.Finalize()
    End Sub


    Public Sub Search( _
        Optional ByVal BaseDirectory As DirectoryInfo = Nothing, _
        Optional ByVal FileMask As String = Nothing, _
        Optional ByVal DirectoryMask As String = Nothing)

        If Not IsNothing(BaseDirectory) Then
            _InitialDirectory = BaseDirectory
        End If

        If IsNothing(_InitialDirectory) Then
            Throw New ArgumentException("A Directory Must be specified!", "Directory")
        End If

        If IsNothing(FileMask) Then
            _FileMask = DefaultFileMask
        Else
            _FileMask = FileMask
        End If


        If IsNothing(DirectoryMask) Then
            _DirectoryMask = DefaultDirectoryMask
        Else
            _DirectoryMask = DirectoryMask
        End If

        DoSearch(_InitialDirectory)
    End Sub

    Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)

        Try
            _Files.AddRange(BaseDirectory.GetFiles(_FileMask))
        Catch u As UnauthorizedAccessException
            'Siliently Ignore this error, there isnt any simple
            'way to avoid this error.

        End Try

        Try
            Dim Directories() As DirectoryInfo = BaseDirectory.GetDirectories(_DirectoryMask)
            _Directories.AddRange(Directories)

            For Each di As DirectoryInfo In Directories
                DoSearch(di)
            Next

        Catch u As UnauthorizedAccessException
            'Siliently Ignore this error, there isnt any simple
            'way to avoid this error.

        End Try

    End Sub

End Class
To use it:

Code:
 Dim x As New FileSearch(New IO.DirectoryInfo("c:\Screenshots[imageUP]"), "*.png")

        *****arch()

        

        For Each imageFiles As FileInfo In x.Files

            Dim img = Image.FromFile(imageFiles.FullName)

            ListBox1.Items.Add(imageFiles.FullName)


            img = Nothing
        Next
Works for Subdirectories!
[highlight="VB.Net"] Private Function SearchFiles(ByVal Path As String, ByVal searchtype As Microsoft.VisualBasic.FileIO.SearchOption, ByVal ParamArray pattern() As String) As List(Of String)
Dim Files As New List(Of String)
For Each file As String In My.Computer.FileSystem.GetFiles(Path, searchtype, pattern)
Try
Files.Add(file)
Catch ex As AccessViolationException
Continue For
End Try
Next

Return Files
End Function[/highlight]

Multithreading is recommended in this case,
Quote Originally Posted by Blubb1337 View Post
[highlight="VB.Net"] Private Function SearchFiles(ByVal Path As String, ByVal searchtype As Microsoft.VisualBasic.FileIO.SearchOption, ByVal ParamArray pattern() As String) As List(Of String)
Dim Files As New List(Of String)
For Each file As String In My.Computer.FileSystem.GetFiles(Path, searchtype, pattern)
Try
Files.Add(file)
Catch ex As AccessViolationException
Continue For
End Try
Next

Return Files
End Function[/highlight]

Multithreading is recommended in this case,
Can you say me what is the ParamArray ? @Blubb1337

EDIT: Not worked, "Access to the path 'C:\Documents and Settings\' is denied."
Quote Originally Posted by Lyoto Machida View Post


Can you say me what is the ParamArray ? @Blubb1337

EDIT: Not worked, "Access to the path 'C:\Documents and Settings\' is denied."
ParamArray means to allow a subrotine to take a variable no. of arguments.

You can just use "*.*" as last parameter.

Start the program as admin + try to change the exception type depending on the exception you get.
Quote Originally Posted by Blubb1337 View Post
ParamArray means to allow a subrotine to take a variable no. of arguments.

You can just use "*.*" as last parameter.

Start the program as admin + try to change the exception type depending on the exception you get.
The error is not in Files.Add , Its before the Try thing ..

@user782106 Still giving me error.
Code:
Dim files As String()
        files = IO.Directory.GetFiles("C:\", "*.exe", IO.SearchOption.AllDirectories)
        For Each File As String In files
           Listbox1.Items.Add(File)
        Next
I just realized I have been using this code in my current project. -.-

Hmm, AllDirectories is glitchy (atleast searching for all .exe files in Windows folder). however Toplevelonly works instantly..

AllDirectories works with a limited no of files.
What error

Ah I see. Did you try the link in my earlier post?
Posts 1–15 of 15 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us