Results 1 to 12 of 12

Thread: Exclude path from scan

  1. #1
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Exclude path from scan

    Hey guys I have this code to scan my folder and list the files in a listbox:

    vb Code:
    1. Dim LOCAL = "F:\Family"
    2.         'Dim LOCAL As String = My.Computer.FileSystem.GetFiles("F:\Family", FileIO.SearchOption.SearchAllSubDirectories)
    3.         Dim xmlFiles() As String = Directory.GetFiles(LOCAL, "*.xml", SearchOption.AllDirectories)
    4.         Dim srtFiles() As String = Directory.GetFiles(LOCAL, "*.srt", SearchOption.AllDirectories)
    5.         Dim aviFiles() As String = Directory.GetFiles(LOCAL, "*.avi", SearchOption.AllDirectories)
    6.         ListBox2.Items.Clear()
    7.         ListBox1.Items.Clear()
    8.         For Each avi As String In aviFiles      
    9.             If Array.IndexOf(xmlFiles, avi.Replace(".avi", ".xml")) >= 0 Then
    10.                 ListBox1.Items.Add(avi)
    11.             ElseIf Array.IndexOf(srtFiles, avi.Replace(".avi", ".srt")) >= 0 Then
    12.                 ListBox1.Items.Add(avi)
    13.             Else
    14.                 ListBox2.Items.Add(avi)
    15.             End If
    16.         Next

    Is there an option to exclude one path inside the "LOCAL"? Like scan and list everything but "F:\Family\Pictures", "F:\Family\Texts"...

    Thank you.
    Last edited by Acrobater; May 6th, 2012 at 10:12 AM.
    Thank you VB Forums!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Exclude path from scan

    Directory.GetFiles can either get just the folder specified or every subfolder too, nothing in between. It's completely up to you what you display from those results though. You might use a bit of LINQ to exclude just those files from a particular folder, e.g.
    Code:
     Dim filePaths = Directory.GetFiles(folderPath, pattern, SearchOption.AllDirectories).Where(Function(filePath) Path.GetDirectorName(filePath) <> folderPathToExclude).ToArray()

  3. #3
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Re: Exclude path from scan

    Here is the error I get:
    Code:
    Error	1	
    Overload resolution failed because no accessible 'Where' can be called with these arguments:
    Extension method 'Public Function Where(predicate As System.Func(Of String, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of String)' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of String, Integer, Boolean)'.
    Extension method 'Public Function Where(predicate As System.Func(Of String, Boolean)) As System.Collections.Generic.IEnumerable(Of String)' defined in 'System.Linq.Enumerable': Lambda parameter 'LOCAL' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.	D:\Minha Pasta\Instala&#231;&#245;es\Visual Basic\MOVIES v3\WindowsApplication1\SRTScanner.vb	9	25	WindowsApplication1
    Using this code:
    'vb' Code:
    1. Dim LOCAL = "F:\Family"
    2. Dim folderPathToExclude = "F:\Family\Pictures"
    3. Dim filePaths = Directory.GetFiles(LOCAL, "*.srt", SearchOption.AllDirectories).Where(Function(LOCAL) Path.GetDirectorName(LOCAL) <> folderPathToExclude).ToArray()

    Is it possible to exclude more than one folder?
    Thank you VB Forums!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Exclude path from scan

    Works fine for me. I can only assume that it's either because you used the same name for the parameter in your Lambda as for a local variable or because you misspelled Directory.

  5. #5
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Re: Exclude path from scan

    I changed it to everything and it gives me the same error... Do I have to Import anything?
    vb Code:
    1. Dim MyMovies = "F:\Family"
    2. Dim Exclude_Movies = "F:\Family\Pictures"
    3. Dim filePaths = Directory.GetFiles(MyMovies, "*.srt", SearchOption.AllDirectories).Where(Function(MyMovies) Path.GetDirectoryName(MyMovies) <> Exclude_Movies).ToArray()
    Thank you VB Forums!

  6. #6
    New Member
    Join Date
    May 12
    Posts
    9

    Re: Exclude path from scan

    Are you writing this in vb2008 still or 2010? I have a snip it of stuff I could send you that I used to scan custom folders with ".exe" or custom extensions.

  7. #7
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Re: Exclude path from scan

    I'm coding using vb 2008.
    Thank you VB Forums!

  8. #8
    New Member
    Join Date
    May 12
    Posts
    9

    Re: Exclude path from scan

    Code:
      Private Function GetDirectoryFileNames(ByVal directoryPath As String) As String()
    
            Dim FileNames() As String = New String() {}
            Dim TempNames() As String
    
            Try
                For Each FileName As String In System.IO.Directory.GetFiles(directoryPath, "*.LNK")
                    ReDim Preserve FileNames(FileNames.GetLength(0))
                    FileNames(UBound(FileNames)) = FileName
                Next
    
                For Each Directory As String In System.IO.Directory.GetDirectories(directoryPath)
                    TempNames = GetDirectoryFileNames(Directory)
                    ReDim Preserve FileNames(FileNames.GetLength(0) + TempNames.GetLength(0) - 1)
                    Array.Copy(TempNames, 0, FileNames, FileNames.GetLength(0) - TempNames.GetLength(0), TempNames.GetLength(0))
                Next
    
            Catch ex As Exception
            End Try
            Return FileNames
    
        End Function
    Code:
    Dim FileDirectory As String() = GetDirectoryFileNames(My.Computer.FileSystem.SpecialDirectories.Desktop)
            Dim FileDirectory2 As String() = GetDirectoryFileNames("C:\Users\Public\Desktop")
            Dim FileDirectory3 As String() = GetDirectoryFileNames(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData)
    
            ListBox1.Items.AddRange(FileDirectory)
            ListBox1.Items.AddRange(FileDirectory2)
            ListBox1.Items.AddRange(FileDirectory3)
    Thats the method I used to scan items into a listbox. Hope this might help. =]

  9. #9
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,153

    Re: Exclude path from scan

    Quote Originally Posted by Acrobater View Post
    I changed it to everything and it gives me the same error... Do I have to Import anything?
    "filePath" is a variable name used in the function itself, you'd only need to change it if you already had variable with that name in use.

    Code:
    Dim filePaths = Directory.GetFiles(MyMovies, "*.srt", SearchOption.AllDirectories).Where(Function(filePath) Path.GetDirectoryName(filePath) <> Exclude_Movies).ToArray()

  10. #10
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Re: Exclude path from scan

    jmcilhinney thank you for the code and help.
    pvocc thank you for your code snippet.
    Edgemeal thank you for pointing that out.

    Also.. Is there a way to add multiple paths to exclude?

    I tried this but didn't work.. of course the function isn't right the way I used ...
    Code:
    Dim filePaths = Directory.GetFiles(MyMovies, "*.srt", SearchOption.AllDirectories).Where(Function(filePath) Path.GetDirectoryName(filePath) <> Exclude_Movies <> Exclude_Movies1 <> Exclude_Movies2).ToArray()
    I also tried this and didn't work:
    Code:
    Dim stringMovies() As String = New String() {"path","path0","path1"}
    Dim filePaths = Directory.GetFiles(MyMovies, "*.srt", SearchOption.AllDirectories).Where(Function(filePath) Path.GetDirectoryName(filePath) <> stringMovies).ToArray()
    Last edited by Acrobater; May 7th, 2012 at 02:25 PM.
    Thank you VB Forums!

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Exclude path from scan

    I did mention in post #4 that you were using the same name for the Lambda parameter as for a local variable, but I now see that the misspelling of Directory was mine.

    Anyway, if you want multiple excluded paths then using an array is fine and you can change your Lambda to this:
    vb.net Code:
    1. Function(filePath) Not folderPathsToExclude.Contains(Path.GetDirectoryName(filePath))

  12. #12
    Lively Member
    Join Date
    Sep 10
    Posts
    101

    Re: Exclude path from scan

    Thank you. That did the trick!
    Thank you VB Forums!

Posting Permissions

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