Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I detect the number of temporary files in the hard drive in Visual Basic? I'm using this code, and it's not working:

VB
Dim drive As String = Path.GetPathRoot(System.Environment.ExpandEnvironmentVariables("%SystemDrive%"))

Dim counter3 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
label1.text = (CStr(counter3.Count))
Posted
Comments
Kythen 7-Jul-10 17:56pm    
How is it not working? What result are you getting and what are you expecting to see?
drummerboy0511 7-Jul-10 17:58pm    
It's giving me "0" and I'm expecting to see the total number of temporary files from my hard drive. And I know for a fact (by looking in my various folders) that there are more than 0 files with the "*.tmp" extension.

Module Module1
    Dim cnt As Integer
    Sub Main()
        cnt = 0
        Console.WriteLine(CountFiles())
        Console.Read()
    End Sub
    Function CountFiles(Optional ByVal dri As String = "C:\", Optional ByVal subDir As Boolean = True) As Integer
        On Error Resume Next
        For Each fo As String In System.IO.Directory.GetDirectories(dri)
            If subDir Then
                CountFiles(fo, True)
            End If
        Next
        For Each fi As String In System.IO.Directory.GetFiles(dri)
            cnt += 1
        Next
        Return cnt
    End Function
End Module
 
Share this answer
 
Ok, I found your solution, your drive string contains 'C:' you need to append the '\' to it.

VB
Dim drive As String = Path.GetPathRoot(System.Environment.ExpandEnvironmentVariables("%SystemDrive%")) & "\"
Dim counter3 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
Label1.Text = (CStr(counter3.Count))
 
Share this answer
 
Comments
William Winner 8-Jul-10 12:30pm    
Actually, it works just fine without it. When I ran the code as the person had it, I ended up with an accessibility issue. It crapped out after like 5 minutes saying that a folder was inaccessible. So, at least for me, there was a security issue.

But, I just tried
Dim counter3 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter3 = My.Computer.FileSystem.GetFiles("C:", FileIO.SearchOption.SearchTopLevelOnly, "*.shp")

MessageBox.Show(counter3.Count)

and it worked fine for me.
idenizeni 8-Jul-10 12:40pm    
Interesting, when I run the poster's version I end up searching the directory my project's exe is in. When I add the slash at the end I search the 'C:' directory. I'm using XP what OS are you using?

I too ran into a permission issue when it tried to access certain directories.
drummerboy0511 8-Jul-10 13:55pm    
RLH68 - I'm using XP too... with the permission issues, run the app as an administrator. That's the best I can come up with there. What version are you using, William Winner?
drummerboy0511 8-Jul-10 13:56pm    
I do have a piece of code where I can detect the operating system (XP, Vista, or 7) and then run a different piece of code for each system. Perhaps I can use that in this case.
idenizeni 8-Jul-10 14:20pm    
Just append the slash to your drive variable. I don't think you need to worry about the OS. I only asked about the OS becuase I thought it may explain why William Winner had different results than me.

Again, with the slash appended I get the correct results everytime, without the slash appened the process ends up searching the current application's dirrectory and not the C: drive.
When I test your code I find it searching the directory my executable is in; not the system root.

I get valid results if I change this line...

counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")

To this...

counter3 = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
 
Share this answer
 
Comments
drummerboy0511 8-Jul-10 9:21am    
See, that's the thing, I don't want it to search just that folder. I want it to search the whole drive.

And, for those worried about lag issues, I'm going to do it asynchronously.
idenizeni 8-Jul-10 11:03am    
I understand, I was just posting that code to help show where the issue might be.
drummerboy0511 8-Jul-10 11:56am    
Reason for my vote of 1
Not what I was looking for...
Have you stepped through the code ? Did you expect GetFiles to search subdirectories ? It does not. You need to write a recursive function for that.
 
Share this answer
 
Comments
drummerboy0511 8-Jul-10 9:38am    
Get files DOES search subdirectories, with the code that I put in.
drummerboy0511 8-Jul-10 11:56am    
i have personally used it many times, successfully.
drummerboy0511 8-Jul-10 16:43pm    
Reason for my vote of 1
...

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