Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a recrusive filesystem iterator that searches for multiple file extensions. I have a label that counts the matches found but for some reason I have been unable to include the files that do not match to the file count so that I get a full system file count and not just a count of the matches.

for the files that match I wish to do some calculations and for the ones that do not match I wish to just count.



Try
dim filespecs as string = "*.exe;*.com;*.386"
                Using fse As New FindFiles.FileSystemEnumerator(pathToSearchCombo.Text, filespecs, True)
                    Dim ien As IEnumerator(Of FileInfo) = fse.Matches().GetEnumerator()
                    ien.Dispose()
                    For Each fi As FileInfo In fse.Matches
Fcount = fcount +1
label3.text = fcount.tostring
'do some work
Posted
Updated 24-Jun-12 17:32pm
v2

1 solution

Really?? FilesThatDontMatch = AllFiles - FileThatDoMatch

It should be pretty obvious you have to retrieve ALL the files in the path specified, then find the ones that match. That way, you'll have both pieces of the equation.
 
Share this answer
 
Comments
Dave Kreskowiak 25-Jun-12 11:36am    
AllFiles - FilesThatDoNotMatch IS your solution. If you are only retrieving the files that match the specification, you only get those files. You don't see the remaining files in the folder at all and therefor cannot count them!

There's isn't a single line of code here that doesn't have something wrong with it.

You create a variable called "ien" and then on the next line you Dispose the object it points to?? Why even create it then?? Your also not even using it!

Where did this "FileSystemEnumerator" come from? It's not part of the .NET Framework.

Your loop gets a FileInfo object from the fse.Matches collection, but then you completely ignore it, so yes, this code doesn't work at all.

Your If statement is useless because your are adding 1 to Fcount no matter what the outcome of the evaluation is, so what's the point of it??


All you need to do is look at the extension of the FileInfo objects you get back from the "fse".
For Each fi As FileInfo In fse.Matches
If fi.Extension = "txt" Then
Fcount += 1
Else
nonMatchingCount += 1
Next
Next
Dale 2012 25-Jun-12 20:50pm    
My last problem to all of this is only counting the files that do not match and to not carry out any other operations on thoes files. I have tried to add the routine within the if fi.extension statement to finally get it to correctly count the total number of files but for some reason it is still doing the calculations on all files and not just the files that match.
Dale 2012 25-Jun-12 21:15pm    
I want to count both files that match and files that do not but only carry out calculations on the files that match
Dave Kreskowiak 25-Jun-12 21:42pm    
Seriously, this is 1st grade math. You go through all the files and examine the extension to see if it matches your critieria. If the file doesn't match, increment a counter keeping track of those files. If it does match, increment a seperate counter tracking those files. Since you now know the number of each, any math or presentation you want to do with those number is trivial. The total number of files is both counters added together.

I honestly can't see what you're having a problem with...
Dale 2012 26-Jun-12 0:45am    
it does not seem to make any difference if I change

If fi.Extension = filespecscombo Then
Fcount += 1
Label3.Text = Fcount.ToString
Else

to

If fi.Extension = nonfilespecs Then
Fcount += 1
Label3.Text = Fcount.ToString
Else

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900