|
-
May 6th, 2012, 10:06 AM
#1
Thread Starter
Lively Member
Exclude path from scan
Hey guys I have this code to scan my folder and list the files in a listbox:
vb Code:
Dim LOCAL = "F:\Family" 'Dim LOCAL As String = My.Computer.FileSystem.GetFiles("F:\Family", FileIO.SearchOption.SearchAllSubDirectories) Dim xmlFiles() As String = Directory.GetFiles(LOCAL, "*.xml", SearchOption.AllDirectories) Dim srtFiles() As String = Directory.GetFiles(LOCAL, "*.srt", SearchOption.AllDirectories) Dim aviFiles() As String = Directory.GetFiles(LOCAL, "*.avi", SearchOption.AllDirectories) ListBox2.Items.Clear() ListBox1.Items.Clear() For Each avi As String In aviFiles If Array.IndexOf(xmlFiles, avi.Replace(".avi", ".xml")) >= 0 Then ListBox1.Items.Add(avi) ElseIf Array.IndexOf(srtFiles, avi.Replace(".avi", ".srt")) >= 0 Then ListBox1.Items.Add(avi) Else ListBox2.Items.Add(avi) End If 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.
-
May 6th, 2012, 06:34 PM
#2
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()
-
May 7th, 2012, 09:43 AM
#3
Thread Starter
Lively Member
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?
-
May 7th, 2012, 09:51 AM
#4
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.
-
May 7th, 2012, 10:07 AM
#5
Thread Starter
Lively Member
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()
-
May 7th, 2012, 10:40 AM
#6
New Member
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.
-
May 7th, 2012, 11:27 AM
#7
Thread Starter
Lively Member
Re: Exclude path from scan
I'm coding using vb 2008.
-
May 7th, 2012, 11:34 AM
#8
New Member
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. =]
-
May 7th, 2012, 11:39 AM
#9
Re: Exclude path from scan
 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()
-
May 7th, 2012, 12:10 PM
#10
Thread Starter
Lively Member
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.
-
May 7th, 2012, 06:13 PM
#11
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))
-
May 7th, 2012, 07:00 PM
#12
Thread Starter
Lively Member
Re: Exclude path from scan
Thank you. That did the trick!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|