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()
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ções\Visual Basic\MOVIES v3\WindowsApplication1\SRTScanner.vb 9 25 WindowsApplication1
Using this code:
'vb' Code:
Dim LOCAL = "F:\Family"
Dim folderPathToExclude = "F:\Family\Pictures"
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?
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.
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:
Dim MyMovies = "F:\Family"
Dim Exclude_Movies = "F:\Family\Pictures"
Dim filePaths = Directory.GetFiles(MyMovies, "*.srt", SearchOption.AllDirectories).Where(Function(MyMovies) Path.GetDirectoryName(MyMovies) <> Exclude_Movies).ToArray()
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.
Re: Exclude path from scan
I'm coding using vb 2008.
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. =]
Re: Exclude path from scan
Quote:
Originally Posted by
Acrobater
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()
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()
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:
Function(filePath) Not folderPathsToExclude.Contains(Path.GetDirectoryName(filePath))
Re: Exclude path from scan
Thank you. That did the trick!