Need to know how to keep certain folders while deleting the rest

Using VB.Net 2005

I am trying to scan a folder and delete all subfolders and files if they are not certain 'required' folders from my app:

There are 3 folders that must always remain. Source code else where ensures that. The code I have keeps picking up the ones I want to remain and targets them for deletion, right along with the ones I do want gone.

 It seems that my logic is flawed. Please help.

Public Sub cleandacrap()
        Dim SourceDir As String = Application.StartupPath & "\MYAPP"
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)
        Dim FiCurrent As String
        Dim DiCurrent As String
        If source.Exists Then
            For Each DiCurrent In Directory.GetDirectories(SourceDir)
                If DiCurrent <> "Projects" Or DiCurrent <> "Reports" Or DiCurrent <> "Volume Tables" Then ' this line does not work. I have wrote different ways but have yet to discover the 'right' way!
                    If MsgBox("about to delete folder " & DiCurrent, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    End If
                End If
End Sub

    Private Sub DelteFiles(ByVal dirname As String)
        Dim Currentfile As String
        Dim CurrentDirectory As String

        For Each Currentfile In Directory.GetFiles(dirname)

        For Each CurrentDirectory In Directory.GetDirectories(dirname)
            DelteFiles(dirname + "\" + Path.GetFileName(CurrentDirectory))
    End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Hi PBLack,

You should do the same in the DelteFiles sub

 If DiCurrent <> "Projects" Or DiCurrent <> "Reports" Or DiCurrent <> "Volume Tables" Then
End If

PBLackAuthor Commented:
Hi japaulino,

I can't add that line to the sub as that sub is being called by other actions in my app. What I don't understand is why the line:

If DiCurrent <> "Projects" Or DiCurrent <> "Reports" Or DiCurrent <> "Volume Tables

does not filter out the Folders I want to keep. These are being picked up, as evident with my MsgBox and I don't know why. Could you please explain how I could reword this statement accurately.


Jorge PaulinoIT Pro/DeveloperCommented:

In this sub you call it again so it will loop. If you have the Projects, Reports  or Volumes Tables in the first dir then it should'nt delete them but if you have as a sub dir then it will delete. I will try to explain better:

  |______ MyDirA
  |______ Reports <--It will delete this one
Projects <--- shouldn't delete this one

Is this what is appening ?

You can make an new sub just for this like DelteFiles1

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Jorge PaulinoIT Pro/DeveloperCommented:
Another thing:

replace the OR with AND

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PBLackAuthor Commented:

I found the problem...and it is a bit embarrassing for me as I missed the obvious:

If DiCurrent <> "Projects" Andr DiCurrent <> "Reports" And DiCurrent <> "Volume Tables

Should read:

If DiCurrent <> SourceDir & "\Projects" And DiCurrent <> SourceDir & "\Volume Tables" And DiCurrent <> SourceDir & "\Reports" Then

It works fine without putting the qualifier in the sub. I don't have to worry about my app having those 3 folders name down deep in another folder as I am trapping them as reserved words for the app.



Jorge PaulinoIT Pro/DeveloperCommented:
Glad I could help!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.