chiapa
chiapa

Reputation: 4412

Getting list of folders with Sharefile API

I'm using the Sharefile API to manage files and folders from my project in VB. I need to get all the folders and subfolders from the root on. This is the function that lists the folders contained in the path specified as parameter:

Public Function f_FolderList(ByVal path As String) As List(Of String)

            Dim result As New List(Of String)

            Dim requiredParameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
            requiredParameters.Add("path", path)

            Dim url As String = BuildUrl("folder", "list", requiredParameters)

            Dim jsonObj As JObject = InvokeShareFileOperation(url)
            Dim errorStatus As Boolean = jsonObj.GetValue("error")

            If Not errorStatus Then

                Dim items As JArray = jsonObj.GetValue("value")

                For Each item As JObject In items
                    result.Add(item.GetValue("filename"))
                Next
                Return result
            Else
                'MsgBox("nothing happened")
            End If

            Return result

        End Function

And here I try to get all of the folders (for now, just displaying the results in msgboxes):

With this code (now below, after edit), I achieve the folders of level 1 and 2 below the root and I could code lists and sublists forever but that's not doable. I need something that checks the existence of subfolders in order to create sublists as they exist or not.

Can anyone help?

(EDIT) here are some changes I made, not getting the expected result but closer though (I think).

Private Sub btn_FoldersTree_Click(sender As Object, e As EventArgs) Handles btn_FoldersTree.Click

        tv_Folders.Nodes.Clear()

        Dim folderList As List(Of String) = sfs.f_FolderList(rootPath)
        Dim path As String = rootPath
        Dim count As Integer = folderList.Count()
        Dim tempPath As String = String.Empty

        For Each folder As String In folderList

            path += "/" + folder
            MsgBox(path)
            folderList = sfs.f_FolderList(path)

            For Each subfolder In folderList

                tempPath = path
                path += "/" + subfolder
                MsgBox(path)
                folderList = sfs.f_FolderList(path)
                path = tempPath

            Next

            path = rootPath

        Next

    End Sub

Upvotes: 0

Views: 2417

Answers (1)

Vland
Vland

Reputation: 4262

Try playing around with a recursive function like this: You start from a rootPath like "E:\Program Files\Fiddler2" and anysubfolder will be found and listed in listDir

Private Sub btn_FoldersTree_Click(sender As Object, e As EventArgs) Handles btn_FoldersTree.Click

    Dim listDir As New List(Of String) '= sfs.f_FolderList(rootPath)
    Dim path As String = rootPath

    GetDirectories(path, listDir)

    'For Each s As String In listDir
        'MsgBox(s)
    'Next
End Sub

Sub GetDirectories(ByVal rootPath As String, ByRef DirectoryList As List(Of String))
    Try
        Dim dirArray() As String = Directory.GetDirectories(rootPath)
        DirectoryList.AddRange(dirArray)

        'Get total number of subdirs
        Console.WriteLine(String.Format("Dir: {0}, SubDirCount: {1}", rootPath, dirArray.Count()))

        For Each Dir As String In dirArray
            GetDirectories(Dir, DirectoryList)
        Next
    Catch ex As Exception
    End Try
End Sub

E.g. Result

Dir: E:\Program Files\Fiddler2, SubDirCount: 6
Dir: E:\Program Files\Fiddler2\FiddlerHook, SubDirCount: 4
Dir: E:\Program Files\Fiddler2\FiddlerHook\Content, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\FiddlerHook\defaults, SubDirCount: 1
Dir: E:\Program Files\Fiddler2\FiddlerHook\defaults\preferences, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\FiddlerHook\locale, SubDirCount: 1
Dir: E:\Program Files\Fiddler2\FiddlerHook\locale\en-US, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\FiddlerHook\skin, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\ImportExport, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\Inspectors, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\ResponseTemplates, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\ScriptEditor, SubDirCount: 0
Dir: E:\Program Files\Fiddler2\Scripts, SubDirCount: 0

Major EDIT

Adapted to real sharefile account and structure:

Video: Sharefile trial account(now deleted) api project built starting from previous link

SharefileExlorer Solution .zip

Upvotes: 2

Related Questions