Results 1 to 28 of 28

Thread: If Then Else Problem

  1. #1

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Question If Then Else Problem

    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.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: If Then Else Problem

    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,755

    Re: If Then Else Problem

    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)
    Edit - I see that TG beat me to it!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: If Then Else Problem

    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

    edit: beaten twice!

  5. #5

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    Thanks for the answer but I'm still getting an error. I don't know if Visual Studio is being just rude to me or what.. but I got this error now

    An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

    Additional information: Conversion from string "" to type 'Boolean' is not valid

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: If Then Else Problem

    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
    Does the scan once, and gets you what you want.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    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

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: If Then Else Problem

    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:

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    There Has To Be An Easier Way To Do This Then Having To Punch In Incredible Amounts of Code... Haven't Given Up Yet Haha
    Last edited by jmiller1225; Dec 3rd, 2015 at 12:39 PM.

  10. #10

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    Doing the scan once helps, which I know.. But What If The Folder Doesn't Exist? How Do You Skip The Step?

  11. #11

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    This Is My Program So Far But Some Of The Folders Aren't On My OS but are on other, That's Why I Want It Checked or Passed.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim counter15 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCache\", "*.dat").Length
            Dim counter16 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\", "*.sol").Length
            Dim counter17 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Media Player\Art Cache\LocalMLS\", "*.jpg").Length
            Dim counter18 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.dat").Length
            Dim counter19 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\", "*.lnk").Length
            Dim counter20 As Integer = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Internet Explorer\Recovery\High\Last Active\", "*.dat").Length
            Dim counter = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Prefetch\")
            Dim counter2 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\AppData\Local\Temp")
            Dim counter3 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cache\")
            Dim counter4 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\INetCache\IE\")
            Dim counter5 = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\temp\")
            Dim counter6 = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\tmp\")
            Dim counter7 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\Feeds Cache\")
            Dim counter8 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\local\microsoft\windows\Explorer\")
            Dim counter9 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Office\Recent\")
            Dim counter10 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Local\Microsoft\Windows\Temporary Internet Files\")
            Dim counter11 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Cookies\")
            Dim counter12 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Appdata\Roaming\Microsoft\Windows\Recent Items\")
            Dim counter13 = My.Computer.FileSystem.GetFiles(Environ$("systemroot") & "\Documents and Settings\Local Settings\Temp\")
            Dim counter14 = My.Computer.FileSystem.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "AppData\Roaming\Macromedia\Flashp~1\")
    
    
    
    
            Dim Result1 = counter.Count
            Dim Result2 = counter2.Count
            Dim Result3 = counter3.Count
            Dim Result4 = counter4.Count
            Dim Result5 = counter5.Count
            Dim Result6 = counter6.Count
            Dim Result7 = counter7.Count
            Dim Result8 = counter8.Count
            Dim Result9 = counter9.Count
            Dim Result10 = counter10.Count
            Dim Result11 = counter11.Count
            Dim Result12 = counter12.Count
            Dim Result13 = counter13.Count
            Dim Result14 = counter14.Count
    
    
    
    
    
    
    
    
            Dim totals = counter15 + counter16 + counter17 + counter18 + counter19 + counter20 + Result1 + Result2 + Result3 + Result4 + Result5 + Result6 + Result7 + Result8 + Result9 + Result10 + Result11 + Result12 + Result13 + Result14
            Label1.Text = totals & " Problems Found"
        End Sub
    End Class

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: If Then Else Problem

    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.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    I got it Finally Understand
    Last edited by jmiller1225; Dec 3rd, 2015 at 01:59 PM.

  14. #14
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: If Then Else Problem

    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:
    Code:
    Dim totalFiles = If(System.IO.Directory.Exists(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\")), Directory.GetFiles(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Appdata\Roaming\Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer\"), "*.sol").Length, 0)
    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.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: If Then Else Problem

    Quote Originally Posted by Sitten Spynne View Post
    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.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: If Then Else Problem

    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.
    My usual boring signature: Nothing

  17. #17
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: If Then Else Problem

    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.

  18. #18

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    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

  19. #19
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: If Then Else Problem

    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.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  20. #20
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: If Then Else Problem

    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.

  21. #21
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: If Then Else Problem

    Quote Originally Posted by jmiller1225 View Post
    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:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Imports System.IO
    6. Imports System.Collections.Generic
    7. Imports System.Linq
    8.  
    9. Friend Class Form1
    10.  
    11.     Private m_DirectoryList As List(Of AppStuff.DirectoryInfo)
    12.  
    13.     Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles Me.Shown
    14.         m_DirectoryList = New List(Of AppStuff.DirectoryInfo)
    15.  
    16.         m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer"), "*.sol"))
    17.         m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\Office\Recent"), "*.dat"))
    18.         m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\Office\Recent"), "*.lnk"))
    19.         m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppCach e"), "*.dat"))
    20.         m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData\Local\Microsoft\Media Player\Art Cache\LocalMLS"), "*.jpg"))
    21.  
    22.         Dim TotalFiles As Integer = m_DirectoryList.Sum(Function(di) di.GetFileCount())
    23.  
    24.         MessageBox.Show(String.Format("Total Files: {0}", TotalFiles), "Total Files", MessageBoxButtons.OK, MessageBoxIcon.Information)
    25.  
    26.     End Sub
    27.  
    28. End Class

    DirectoryInfo.vb:
    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Imports System.IO
    6.  
    7. Namespace AppStuff
    8.     Friend Class DirectoryInfo
    9.  
    10.         Private ReadOnly m_Path As String
    11.         Private ReadOnly m_FileType As String
    12.         Private m_PathExists As Boolean
    13.         Private m_FileCount As Integer
    14.  
    15.         Friend Sub New(Path As String)
    16.             m_Path = Path.Trim
    17.             m_FileType = String.Empty
    18.             m_PathExists = Directory.Exists(Me.Path)
    19.             m_FileCount = 0I
    20.         End Sub
    21.  
    22.         Friend Sub New(Path As String, FileExtension As String)
    23.             m_Path = Path.Trim
    24.             m_FileType = FileExtension.Trim
    25.             m_PathExists = Directory.Exists(Me.Path)
    26.             m_FileCount = 0I
    27.         End Sub
    28.  
    29.         Friend ReadOnly Property Path() As String
    30.             Get
    31.                 Return m_Path
    32.             End Get
    33.         End Property
    34.  
    35.         Friend ReadOnly Property FileType() As String
    36.             Get
    37.                 Return m_FileType
    38.             End Get
    39.         End Property
    40.  
    41.         Friend ReadOnly Property PathExists As Boolean
    42.             Get
    43.                 Return m_PathExists
    44.             End Get
    45.         End Property
    46.  
    47.         Friend ReadOnly Property FileCount As Integer
    48.             Get
    49.                 Return m_FileCount
    50.             End Get
    51.         End Property
    52.  
    53.         Friend Function GetFileCount() As Integer
    54.             m_PathExists = Directory.Exists(Me.Path)
    55.  
    56.             If Me.PathExists Then
    57.                 m_FileCount = 0I
    58.                 Try
    59.                     If m_FileType.Length > 0 Then
    60.                         m_FileCount = Directory.GetFiles(Me.Path, m_FileType).Length
    61.                     Else
    62.                         m_FileCount = Directory.GetFiles(Me.Path).Length
    63.                     End If
    64.                 Catch ex As DirectoryNotFoundException
    65.                     m_FileCount = 0I
    66.                     m_PathExists = False
    67.                 Catch ex As Exception
    68.                     Throw ex
    69.                 End Try
    70.             End If
    71.  
    72.             Return Me.FileCount
    73.         End Function
    74.  
    75.     End Class
    76.  
    77. End Namespace
    Last edited by JuggaloBrotha; Dec 4th, 2015 at 10:08 AM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  22. #22
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: If Then Else Problem

    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.

  23. #23
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: If Then Else Problem

    Quote Originally Posted by Sitten Spynne View Post
    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.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  24. #24

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    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

  25. #25

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    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)

    .m_Path = Path.Trim

    .m_FileType = FileExtension.Trim

    .m_PathExists = Directory.Exists(Me.Path)

    .m_FileCount = 0I

    End Sub



    Friend ReadOnly Property Path() As String

    .Get()

    .Return m_Path

    End Get

    End Property



    Friend ReadOnly Property FileType() As String

    .Get()

    .Return m_FileType

    End Get

    End Property


    Friend ReadOnly Property PathExists As Boolean

    .Get()

    .Return m_PathExists

    End Get

    End Property



    Friend ReadOnly Property FileCount As Integer

    .Get()

    .Return m_FileCount

    End Get

    End Property



    Friend Function GetFileCount() As Integer

    .m_PathExists = Directory.Exists(Me.Path)



    If Me.PathExists Then

    .m_FileCount = 0I

    .Try()

    If m_FileType.Length > 0 Then

    .m_FileCount = Directory.GetFiles(Me.Path, m_FileType).Length

    .Else()

    .m_FileCount = Directory.GetFiles(Me.Path).Length

    End If

    Catch ex As DirectoryNotFoundException

    .m_FileCount = 0I

    .m_PathExists = False

    Catch ex As Exception

    .Throw ex

    End Try

    End If



    .Return Me.FileCount

    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    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)



    .m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationD ata), "Macromedia\Flash Player\#SharedObjects\WUQ82GVH\#AppContainer"), "*.sol"))

    .m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationD ata), "Microsoft\Office\Recent"), "*.dat"))

    .m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationD ata), "Microsoft\Office\Recent"), "*.lnk"))

    .m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) , "AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\AppC ach e"), "*.dat"))

    .m_DirectoryList.Add(New AppStuff.DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) , "AppData\Local\Microsoft\Media Player\Art Cache\LocalMLS"), "*.jpg"))



    Dim TotalFiles As Integer = m_DirectoryList.Sum(Function(di) di.GetFileCount())



    .MessageBox.Show(String.Format("Total Files: {0}", TotalFiles), "Total Files", MessageBoxButtons.OK, MessageBoxIcon.Information)



    End Sub



    End Class




    End Sub
    End Class

  26. #26
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: If Then Else Problem

    Quote Originally Posted by jmiller1225 View Post
    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.
    Attached Files Attached Files
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  27. #27

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    37

    Re: If Then Else Problem

    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

  28. #28
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: If Then Else Problem

    Quote Originally Posted by jmiller1225 View Post
    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:
    vb.net Code:
    1. YourLabel.Text = String.Format("Total Files: {0}", TotalFiles)
    Or if you have a preceding label to show the caption you could do:
    vb.net Code:
    1. YourLabel.Text = TotalFiles.ToString
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width