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?
Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
Another thing:

replace the OR with AND
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.


Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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

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!

All Courses

From novice to tech pro — start learning today.