-2

How do I find whether a folder is a sub folder of another folder. The code should recognize that "C:/temp/abc/cde/xyz" is a sub folder of "C:/temp" folder. I tried comparing strings and it results in saying "C:/temp" is a sub folder of "C:/temp1" and is not what is required.

Thanks

SRA
  • 1
  • 1
  • Please provide the code that you have already tried yourself. – Jeroen Jan 14 '16 at 23:50
  • 1
    Possible duplicate of [How to check if directory 1 is a subdirectory of dir2 and vice versa](http://stackoverflow.com/questions/3525775/how-to-check-if-directory-1-is-a-subdirectory-of-dir2-and-vice-versa) – default Jan 14 '16 at 23:51
  • I know I know, they are different languages, but C# should be relatively easy to translate to vb.net – default Jan 14 '16 at 23:51
  • Thanks for the reply and suggestions. But the C# discussion was not helpful to me, may be because of my little knowledge on C# to vb.net conversion. Using the long method of checking each string is no brainer and thought there could be something better. – SRA Jan 15 '16 at 15:14

2 Answers2

0

In the System.IO namespace you find Directory which has a function: GetParent. So after you place:

Imports System.IO

At the very top of your code module, you can then inspect the directory (folder) structure. Just Google "Directory.GetParent"

Jeroen
  • 460
  • 6
  • 14
0

@Jeroen. This is what I finally came up with after all suggestions. It works for me.

   For Uindex = ListView1.Items.Count - 1 To 1 Step -1
        Dim uItem = ListView1.Items(Uindex)

        For dIndex = 0 To Uindex - 1 Step 1
            Dim dItem = ListView1.Items(dIndex)
            Dim SubItm, SubItmLst
            Dim RotItm, RotItmLst

            If dItem.Contains(uItem) Then
                SubItm = dItem
                SubItmLst = Split(dItem, Path.DirectorySeparatorChar)
                RotItm = uItem
                RotItmLst = Split(uItem, Path.DirectorySeparatorChar)
            ElseIf uItem.Contains(dItem) Then
                SubItm = uItem
                SubItmLst = Split(uItem, Path.DirectorySeparatorChar)
                RotItm = dItem
                RotItmLst = Split(dItem, Path.DirectorySeparatorChar)
            End If

            If Not (SubItm Is Nothing) Then
                If SubItmLst(RotItmLst.length - 1) = RotItmLst(RotItmLst.length - 1) Then

                    Dim MsgOut = MsgBox(SubItm & " is a sub folder of " & RotItm & vbCr & _
                            "Do you Want to remove this sub folder?", MsgBoxStyle.YesNo)

                    If MsgOut = MsgBoxResult.Yes Then
                        ListView1.Items.Remove(SubItm)
                        SubItm = Nothing
                        Exit For
                    End If
                End If
            End If
        Next
    Next
SRA
  • 1
  • 1