I'm trying to count files in a directory if the directory is there, if not the number should be 0. I wrote up this code thinking it would work but it keeps giving me strange error. Can anyone help me fix?
Code:
If Dir(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
Dim counter16 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
Else
Dim counter16 As Integer = 0
End If
label1.Text (counter16 & " Files Found")
This Code Looks Like It Should Work, I'm not understanding why its not. I keep getting this error "Error 1 'counter16' is not declared. It may be inaccessible due to its protection level." but I'm running Visual Studio as Admin so idk.
Last edited by jmiller1225; Dec 3rd, 2015 at 09:05 AM.
It's because you dimed the variables inside the if/else blocks... so they are only wisible within those blocks of code... once it exits out either part of the if, the variable no longer exists....
You need to declare it outside of the if, then set it inside:
Code:
Dim counter16 As Integer
If Dir(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
counter16 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
Else
counter16 = 0
End if
Additionally you can defaul;t it to 0, and only set it if found...
Code:
Dim counter16 As Integer = 0
If Dir(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
counter16 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
End if
That eliminates the else portion...
-tg
edit - that just addresses the problem you reported... I'd optimize it further to only do the scan once rather than twice.
You're declaring counter16 inside the scope of your conditional statement. Try doing this instead:
Code:
Dim myDirectory As String = Environment.SpecialFolder.UserProfile & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\"
Dim counter16 As Integer = If(Not IO.Directory.Exists(myDirectory), 0, IO.Directory.GetFiles(myDirectory).Length - 1)
It's because of where you declared the variables... as you declared them inside code blocks (the "if" and "else" sections), they only exist within that area - so you actually have two completely different variables that happen to have the same name, and either of them exist outside the If-End If.
To be able to use the variable after the End If, you need to declare it outside, eg:
Code:
Dim counter16 As Integer = 0
If Dir(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
counter16 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
End If
That's a completely different error... but it means what it says... something is a string (empty) and an empty string can't be converted to a boolean.
This actually goes back to what I mentioned about doing the scan once instead of twice.
first thing to understand is what Dir returns... which is a string... and if there is no matching file, it returns an empty string... which is why you're getting the error.
Code:
Dim counter16 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
so what you're pretty much telling me is that Visual Studio won't let me check first to see if the folder exists and if the folder does exist count the certain .sol extension in the folder. Because not all computers have the Flash Player Folder, but some do. Or is there a way around this? Because all I get is error
In general, VS is not the issue. You can do pretty nearly anything you need to do, and often by many different ways. If you want to check whether the directory exists, you can just use this method:
If you want to check to see if a Directory Exists... then use Directory.Exists as Shaggy pointed out... that returns a boolean... then from there, you use GetFiles and get the count... Dir doesn't return a boolean... it returns a string... but it's not a string that you want.
In general, you can use Directory.Exists() to see if a directory exists. In practice, I say don't bother. The filesystem is volatile. What this means is you can check to see if something exists, but someone can delete it after you check. So it's better to just try to use the filesystem and handle FileNotFoundException or DirectoryNotFoundException. You may not like this. But it's how it works, and you won't find another programming language where it isn't true.
The filesystem tools are sort of primitive because it's easier to do complicated things with simple tools than it is to start with complicated tools. Imagine an oven that could magically guarantee it always baked perfect cookies. Forget the chocolate chips? They magically appear. Too much sugar? Fixed. What could go wrong with this oven? You'll know the moment you try to cook a turkey and it responds by setting your house on fire because turkey isn't cookies.
There's more than one way to skin a cat, but usually "writing a lot of code" is synonymous with "writing clearer code", in my opinion.
To demonstrate, here's a few ways to get the count of files or 0 if the directory doesn't exist. See if more lines is worse.
This is how I'd write a first pass:
Code:
Dim userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim appDataPath = "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\"
Dim fullDirectoryPath = System.IO.Path.Combine(userProfile, appDataPath)
Dim filter = "*.sol"
Dim totalFiles = 0
Try
Dim allFiles = System.IO.Directory.GetFiles(fullDirectoryPath, "*.sol")
totalFiles = allFiles.Length
Catch ex As DirectoryNotFoundException
totalFiles = 0
End Try
After verifying that, I'd clean it up:
Code:
Public Function CountFlashSolFiles() As Integer
Dim flashDirPath = BuildFlashPath()
Dim filter = "*.sol"
Return CountFiles(flashDirPath, filter)
End Function
Private Function BuildFlashPath() As String
Dim userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim appDataPath = "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\"
Dim fullDirectoryPath = System.IO.Path.Combine(userProfile, appDataPath)
Return fullDirectoryPath
End Function
Private Function CountFiles(ByVal directoryPath As String, ByVal filter As String)
Try
Dim allFiles = System.IO.Directory.GetFiles(directoryPath, filter)
Return allFiles.Length
Catch ex As DirectoryNotFoundException
Return 0
End Try
End Function
If I were in some weird competition where using the fewest number of lines would get me a lot of money, I'd do this:
I'd argue the worst code of the bunch uses the fewest lines, and the best uses the most. This rule often holds.
And if you apply some thought, you'll see how that code in the middle would make it easy to work with many directories, whereas the other techniques will employ copy/paste.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
In general, you can use Directory.Exists() to see if a directory exists. In practice, I say don't bother. The filesystem is volatile. What this means is you can check to see if something exists, but someone can delete it after you check. So it's better to just try to use the filesystem and handle FileNotFoundException or DirectoryNotFoundException. You may not like this. But it's how it works, and you won't find another programming language where it isn't true.
What hades-devised environment are you working in that in mere ms after checking for a folder could it possibly not exist? I don't think the file system is THAT volitile... sure you may not be able to count on a folder being there hours after you check, but if you check and then the very next thing attempt to get something from it, odds are, it's still there and you're good.
Well, I would say that both answers are true. It is VERY unlikely that the directory will be deleted between the check and the use, but I seem to remember it once happening to me...in a very peculiar situation. On the other hand, the OP has stated a couple of times that the directories will not always be there, which means that the exceptions will be thrown on a reasonable basis. Catching exceptions is SLOW, so I certainly wouldn't suggest JUST relying on exception handling. Checking for the existence will be far faster than catching exceptions resulting from the non-existence, but since there are obscure cases where the directory might be deleted between the check and the use, the code should be wrapped in a Try...Catch block to catch that rare case.
I think the OP mentioned network drives, too, which means that another rare case could intrude between the check and the use: The network could fail. Also very unlikely, but exception handling would take care of that, too.
A fileserver hosting several thousand users? A tool that checks an FTP service for data files? Godforsaken cheap anti-virus deciding the folder looks suspicious? A network share in New Zealand that's a spotty connection that fails 80% of the time? A network share on a server in a datacenter that experiences a power outage?
You can usually, with 99.9% certainty, assume that it's not going to happen in milliseconds. It's fine to take these shortcuts when you're writing an app where the consequences of failure are "ugh, I have to start over". But you should know it's a shortcut, just in case you get in a situation where worse things happen if you fail due to an unexpected exception. I'm in one of those cases, our customer pays a lot of money for my tool to never fail. I've seen a lot of "this should never happen" exceptions trip, and I smile every time I'm proven right for handling it just in case.
And in this case, it's not like I'm asking for a complex framework: I'm just saying "Catch DirectoryNotFound instead of using Directory.Exists(), it's more reliable."
Network resources are similar. You don't generally ping a server to see if it's there, then make the connection, confident everything's OK. You just try the connection and handle the failure if the network's down. Or, if you do ping it, you still catch the failure afterwards because you know that one success doesn't mean you're guaranteed success forever. It turns out it's easier to try and catch than check, try, and catch.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Ive gotten the code to work properly until I get up to counter10, counter11, counter12, counter13, and counter14
Heres the code that started showing errors and not running
Code:
Dim counter10 As Integer = 0
Dim counter10a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\") Then
counter10 = counter10a.Count
End If
Dim counter11 As Integer = 0
Dim counter11a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\") Then
counter11 = counter11a.Count
End If
Dim counter12 As Integer = 0
Dim counter12a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\") Then
counter12 = counter12a.Count
End If
Dim counter13 As Integer = 0
Dim counter13a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\")
If Directory.Exists(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\") Then
counter13 = counter13a.Count
End If
Dim counter14 As Integer = 0
Dim counter14a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\") Then
counter14 = counter14a.Count
End If
Label1.Text = counter14 & " Problems Found"
But if I change Label1.Text = anyother counter besides 10,11,12,13,14 it works great.. Heres my whole code. It counts Temp Files
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter16 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
counter16 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
End If
Dim counter15 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCach e\") Then
counter15 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCach e\", "*.dat").Length
End If
Dim counter17 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Media Player\Art Cache\LocalMLS\") Then
counter17 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Media Player\Art Cache\LocalMLS\", "*.jpg").Length
End If
Dim counter18 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter18 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.dat").Length
End If
Dim counter19 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter19 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.lnk").Length
End If
Dim counter20 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Internet Explorer\Recovery\High\Last Active\") Then
counter20 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Internet Explorer\Recovery\High\Last Active\", "*.dat").Length
End If
Dim counter As Integer = 0
Dim countera = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Prefetch\")
If Directory.Exists(Environ$("systemroot") & "\Prefetch\") Then
counter = countera.Count
End If
Dim counter2 As Integer = 0
Dim counter2a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\AppData\Local\Temp")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\AppData\Local\Temp") Then
counter2 = counter2a.Count
End If
Dim counter3 As Integer = 0
Dim counter3a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cache\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cache\") Then
counter3 = counter3a.Count
End If
Dim counter4 As Integer = 0
Dim counter4a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\INetCache\IE\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\INetCache\IE\") Then
counter4 = counter4a.Count
End If
Dim counter5 As Integer = 0
Dim counter5a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\temp\")
If Directory.Exists(Environ$("systemroot") & "\temp\") Then
counter5 = counter5a.Count
End If
Dim counter7 As Integer = 0
Dim counter7a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Feeds Cache\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Feeds Cache\") Then
counter7 = counter7a.Count
End If
Dim counter8 As Integer = 0
Dim counter8a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\Explorer\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\Explorer\") Then
counter8 = counter8a.Count
End If
Dim counter9 As Integer = 0
Dim counter9a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter9 = counter9a.Count
End If
Dim counter10 As Integer = 0
Dim counter10a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\") Then
counter10 = counter10a.Count
End If
Dim counter11 As Integer = 0
Dim counter11a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\") Then
counter11 = counter11a.Count
End If
Dim counter12 As Integer = 0
Dim counter12a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\") Then
counter12 = counter12a.Count
End If
Dim counter13 As Integer = 0
Dim counter13a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\")
If Directory.Exists(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\") Then
counter13 = counter13a.Count
End If
Dim counter14 As Integer = 0
Dim counter14a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\") Then
counter14 = counter14a.Count
End If
Label1.Text = counter14 & " Problems Found"
End Sub
End Class
Someone should test it and tell me what you think.. Keep shooting back error codes though when I get to counter10, counter11, counter12, counter13, counter14. Eventually all the counters will add up to total
Looks like someone should create a function that does the checking and getting of the files count, then just have a Dictionary or something that has all the folder paths, loop the dictionary and set the count part of the dictionary to the count for each folder path. To get the total just use Linq to enumerate the dictionary and add up the counts, or keep a running total that adds the count to it on each iteration of the dictionary when you assign the count.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
jmiller1225:
Some of your code looks like this, and this is valid:
Code:
Dim counter16 As Integer = 0
If Directory.Exists(...) Then
counter16 = IO.Directory.GetFiles(..., "*.sol").Length
End If
Some of your code looks like this, and this is invalid:
Code:
Dim counter As Integer = 0
Dim countera = My.Computer.FileSystem.GetFiles(...)
If Directory.Exists(...) Then
counter = countera.Count
End If
The variable countera is an Integer, or maybe a Long, it doesn't really matter. These are numbers. They do not have a Count property. The biggest giveaway that something is wrong is you should notice the "shape" of your code is different. See how, in the working code, you call Directory.Exists() before GetFiles()? See how, in the code that doesn't work, you call them in a different order, and create an extra variable? Since everything is basically doing the same thing, shouldn't they all have the same "shape"?
Exception catching discussion:
Yeah, exceptions are really slow. I was going to play the "It doesn't matter in this case" card, but I don't like that card. Here's a better thought.
The more I think about it, there's a tension. This is the most robust pattern if you need to loop over many folders and expect many of them to not exist:
Code:
For Each dirPath in directories
If Directory.Exists(dirPath) Then
Try
Dim files = Directory.GetFiles(...)
...
Catch ex As DirectoryNotFoundException
...
End Catch
Else
' repeat code from the Catch
End If
Next
Directory.Exists() is nice as a canary if it returns False. That's a lot faster than Try/Catch. But if it returns True, we're only 99% certain the directory exists. Now we fall back on Try..Catch, because at this point it's super unlikely we'll incur the penalty. And it IS a penalty, I averaged 700ms in a test!
But there's a lot of reasons you might want to just use Directory.Exists(): maybe you're doing a quick validation because if any don't exist, you'll fail. Maybe they'll be deleted later, but you handle the error then. Maybe it's a quick one-off script and you know that folder's not going anywhere. The main thing is realizing that every Directory method but Exists() might throw DirectoryNotFound, EVEN if you check Directory.Exists() first. Someone, somewhere has been surprised by this, but Exists() isn't a talisman that protects the directory.
I never write filesystem code without handling the "What if it doesn't exist?" case in exception handlers at some point in the program, unless t's the kind of program where "crash to desktop" is acceptable to me.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Ive gotten the code to work properly until I get up to counter10, counter11, counter12, counter13, and counter14
Heres the code that started showing errors and not running
Code:
Dim counter10 As Integer = 0
Dim counter10a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\") Then
counter10 = counter10a.Count
End If
Dim counter11 As Integer = 0
Dim counter11a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\") Then
counter11 = counter11a.Count
End If
Dim counter12 As Integer = 0
Dim counter12a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\") Then
counter12 = counter12a.Count
End If
Dim counter13 As Integer = 0
Dim counter13a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\")
If Directory.Exists(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\") Then
counter13 = counter13a.Count
End If
Dim counter14 As Integer = 0
Dim counter14a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\") Then
counter14 = counter14a.Count
End If
Label1.Text = counter14 & " Problems Found"
But if I change Label1.Text = anyother counter besides 10,11,12,13,14 it works great.. Heres my whole code. It counts Temp Files
Someone should test it and tell me what you think.. Keep shooting back error codes though when I get to counter10, counter11, counter12, counter13, counter14. Eventually all the counters will add up to total
Here's an example, I did 5 of them for you.
Form1:
vb.net Code:
Option Explicit On
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Friend Class Form1
Private m_DirectoryList As List(Of AppStuff.DirectoryInfo)
Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles Me.Shown
m_DirectoryList = New List(Of AppStuff.DirectoryInfo)
I'd personally pick a different name from DirectoryInfo, despite that being a good one, because it clashes with System.IO.DirectoryInfo and that's a pain to deal with if you need to use both. Other than that, it's a good use of a class to hide repetitive ugly.
One nitpick though: this pattern is basically a no-op:
Code:
Try
Catch ex As Exception
Throw ex
End Try
It may as well not be there, but it's also kind of a good placeholder for "I want to do something with this exception later but right now I haven't figured out what that is."
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
I'd personally pick a different name from DirectoryInfo, despite that being a good one, because it clashes with System.IO.DirectoryInfo and that's a pain to deal with if you need to use both. Other than that, it's a good use of a class to hide repetitive ugly.
One nitpick though: this pattern is basically a no-op:
Code:
Try
Catch ex As Exception
Throw ex
End Try
It may as well not be there, but it's also kind of a good placeholder for "I want to do something with this exception later but right now I haven't figured out what that is."
That's a good point, I neglected to put the DirectoryNotFound catch in there to ignore it in the event it comes up missing between the exists check and actually using it.
For everything else, just re-throw the exception and update the code to handle it once it's known.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Thanks everyone for the post.. JuggaloBrotha I'm going to try your way. The program does work great already, if you're using Windows 10 haha Tried it on Windows 7 got errors, so we will try your code out
I'm having troubles I don't know if it's because I'm using Visual Studio 2012 or what the deal. But I keep getting errors with your code. I think I must be setting it up wrong. I understand the code, but I think I'm setting up the forms and everything wrong. I know I'm a pain but can you help
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Option Explicit On
Option Strict On
Option Infer Off
.Imports System.IO
.Namespace AppStuff
Friend Class DirectoryInfo
Private ReadOnly m_Path As String
Private ReadOnly m_FileType As String
Private m_PathExists As Boolean
Private m_FileCount As Integer
Friend Sub New(Path As String)
.m_Path = Path.Trim
.m_FileType = String.Empty
.m_PathExists = Directory.Exists(Me.Path)
.m_FileCount = 0I
End Sub
Friend Sub New(Path As String, FileExtension As String)
I'm having troubles I don't know if it's because I'm using Visual Studio 2012 or what the deal. But I keep getting errors with your code. I think I must be setting it up wrong. I understand the code, but I think I'm setting up the forms and everything wrong. I know I'm a pain but can you help
Yep, that's completely not how to copy/paste it into our project.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Thanks JuggaloBrotha your Code works great! I may use it.. I finally figured out my issue with my code and now it works on windows 7, 8, 8.1, and 10. Now I'm trying to add the total to "Label1" as the script progresses. But that's another thread haha Heres my code if anyone wants to play with it
Code:
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter16 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\") Then
counter16 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
End If
On Error Resume Next
Dim counter15 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCach e\") Then
counter15 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCach e\", "*.dat").Length
End If
On Error Resume Next
Dim counter17 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Media Player\Art Cache\LocalMLS\") Then
counter17 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Media Player\Art Cache\LocalMLS\", "*.jpg").Length
End If
On Error Resume Next
Dim counter18 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter18 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.dat").Length
End If
On Error Resume Next
Dim counter19 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter19 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.lnk").Length
End If
On Error Resume Next
Dim counter20 As Integer = 0
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Internet Explorer\Recovery\High\Last Active\") Then
counter20 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Internet Explorer\Recovery\High\Last Active\", "*.dat").Length
End If
On Error Resume Next
Dim counter As Integer = 0
Dim countera = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Prefetch\")
If Directory.Exists(Environ$("systemroot") & "\Prefetch\") Then
counter = countera.Count
End If
On Error Resume Next
Dim counter2 As Integer = 0
Dim counter2a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\AppData\Local\Temp")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\AppData\Local\Temp") Then
counter2 = counter2a.Count
End If
On Error Resume Next
Dim counter3 As Integer = 0
Dim counter3a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cache\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cache\") Then
counter3 = counter3a.Count
End If
On Error Resume Next
Dim counter4 As Integer = 0
Dim counter4a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\INetCache\IE\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\INetCache\IE\") Then
counter4 = counter4a.Count
End If
On Error Resume Next
Dim counter5 As Integer = 0
Dim counter5a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\temp\")
If Directory.Exists(Environ$("systemroot") & "\temp\") Then
counter5 = counter5a.Count
End If
On Error Resume Next
Dim counter7 As Integer = 0
Dim counter7a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Feeds Cache\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Feeds Cache\") Then
counter7 = counter7a.Count
End If
On Error Resume Next
Dim counter8 As Integer = 0
Dim counter8a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\Explorer\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\Explorer\") Then
counter8 = counter8a.Count
End If
On Error Resume Next
Dim counter9 As Integer = 0
Dim counter9a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\") Then
counter9 = counter9a.Count
End If
On Error Resume Next
Dim counter10 As Integer = 0
Dim counter10a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\") Then
counter10 = counter10a.Count
End If
On Error Resume Next
Dim counter11 As Integer = 0
Dim counter11a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\") Then
counter11 = counter11a.Count
End If
On Error Resume Next
Dim counter12 As Integer = 0
Dim counter12a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\") Then
counter12 = counter12a.Count
End If
On Error Resume Next
Dim counter13 As Integer = 0
Dim counter13a = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\")
If Directory.Exists(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\") Then
counter13 = counter13a.Count
End If
On Error Resume Next
Dim counter14 As Integer = 0
Dim counter14a = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\")
If Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\") Then
counter14 = counter14a.Count
End If
On Error Resume Next
System.Threading.Thread.Sleep(1500)
Dim totals = counter + counter2 + counter3 + counter4 + counter5 + counter7 + counter8 + counter9 + counter10 + counter11 + counter12 + counter13 + counter14 + counter15 + counter16 + counter17 + counter18 + counter19 + counter20
Label1.Text = totals & " Problems Found"
End Sub
End Class
Thanks JuggaloBrotha your Code works great! I may use it.. I finally figured out my issue with my code and now it works on windows 7, 8, 8.1, and 10. Now I'm trying to add the total to "Label1" as the script progresses. But that's another thread haha Heres my code if anyone wants to play with it
You can easily assign the files count to a Label instead of showing it in a MessageBox by doing this: