Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.7.0+

Box: Find a File by Name Across All Folder Pages

See more Box Examples

Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Find one exact, case-sensitive filename in a parent folder selected by name, checking the item type and every page before using its ID.

See the Box API guide for setup and the complete workflow.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
    ;  See Global Unlock Sample.
    ;  Guide: Box API Guide.

    success = 0

    ;  BEGIN ONE-TIME SETUP
    ;  This block is repeated so each published example can run on its own.
    ;  In an application, run the setup example once and pass the SAME curl object
    ;  to operation code below. Do not recreate it for every operation.
    ;  See Reusable Box HttpCurl Setup.
    curl.i = CkHttpCurl::ckCreate()
    If curl.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpCurl::ckSetVar(curl,"access_token","BOX_ACCESS_TOKEN")
    CkHttpCurl::ckSetVar(curl,"parent_name","/")
    ;  Box's fixed root ID is an implementation detail; callers supply names.
    CkHttpCurl::ckSetVar(curl,"parent_id","0")
    setupCommand.i = CkStringBuilder::ckCreate()
    If setupCommand.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    setupReplacements.i

    ;  Register findParent; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findParent",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findFolder; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findFolder",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findFile; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findFile",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register findDestination; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"findDestination",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register getFileDetails; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"getFileDetails",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register getEnterprise; registration sends no request.
    CkStringBuilder::ckSetString(setupCommand,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/users/me?fields=enterprise" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34))
    setupReplacements = CkStringBuilder::ckReplace(setupCommand,"ACCESS_TOKEN",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckAddFunction(curl,"getEnterprise",CkStringBuilder::ckGetAsString(setupCommand))
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
    ;  when extracting each output, using the current value at that time.
    success = CkHttpCurl::ckAddOutput2(curl,"findParent","entries","name","{{parent_name}}",1,"id","parent_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findFolder","entries","name","{{folder_name}}",1,"id","folder_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findFile","entries","name","{{file_name}}",1,"id","file_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput2(curl,"findDestination","entries","name","{{destination_name}}",1,"id","destination_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","file_version.id","file_version_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getFileDetails","sha1","file_sha1")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    success = CkHttpCurl::ckAddOutput(curl,"getEnterprise","enterprise.id","enterprise_id")
    If success <> 1
        Debug CkHttpCurl::ckLastErrorText(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        ProcedureReturn
    EndIf

    ;  END ONE-TIME SETUP
    ;  The name lookups below assume each listing fits in one page (1000 items).
    ;  Parent and destination names select root children; folder_name selects a child
    ;  of parent_name. "/" selects the root parent. These are not arbitrary paths.

    ;  BEGIN OPERATION -- keep the configured object when reusing it.
    ;  Inputs are names. Retain cached IDs when the selected names have not changed.
    parentName.s = "/"
    folderName.s = "images"
    If (CkHttpCurl::ckGetVar(curl,"parent_name") <> parentName) OR ((parentName = "/") AND (CkHttpCurl::ckVarDefined(curl,"parent_id") = 0))
        CkHttpCurl::ckSetVar(curl,"parent_name",parentName)
        CkHttpCurl::ckClearVar(curl,"parent_id")
        CkHttpCurl::ckClearVar(curl,"folder_id")
        CkHttpCurl::ckClearVar(curl,"file_id")
        CkHttpCurl::ckClearVar(curl,"file_version_id")
        CkHttpCurl::ckClearVar(curl,"file_sha1")
        If parentName = "/"
            CkHttpCurl::ckSetVar(curl,"parent_id","0")
        EndIf

    EndIf

    If CkHttpCurl::ckGetVar(curl,"folder_name") <> folderName
        CkHttpCurl::ckSetVar(curl,"folder_name",folderName)
        CkHttpCurl::ckClearVar(curl,"folder_id")
        CkHttpCurl::ckClearVar(curl,"file_id")
        CkHttpCurl::ckClearVar(curl,"file_version_id")
        CkHttpCurl::ckClearVar(curl,"file_sha1")
    EndIf

    CkHttpCurl::ckSetVar(curl,"marker","")
    page.i = CkJsonObject::ckCreate()
    If page.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    count.i = 0
    more.i = 1
    nextMarker.s
    authorizedCommand1.i = CkStringBuilder::ckCreate()
    If authorizedCommand1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand1,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{folder_id}}/items?usemarker=true&limit=1000&fields=id,type,name" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34))
    headerReplacement1.i = CkStringBuilder::ckReplace(authorizedCommand1,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    pageCommand.s = CkStringBuilder::ckGetAsString(authorizedCommand1)
    wantedName.s = "penguins.jpg"
    fileId.s = ""
    matches.i = 0
    While (more = 1)
        CkHttpCurl::ckClearTargetOutput(curl,"*")
        success = CkHttpCurl::ckDoYourThing(curl,pageCommand)
        If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 200)
            Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
            Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
            Debug CkHttpCurl::ckResponseBodyStr(curl)
            CkHttpCurl::ckDispose(curl)
            CkStringBuilder::ckDispose(setupCommand)
            CkJsonObject::ckDispose(page)
            CkStringBuilder::ckDispose(authorizedCommand1)
            ProcedureReturn
        EndIf

        success = CkHttpCurl::ckGetResponseJson(curl,page)
        If success <> 1
            Debug CkHttpCurl::ckLastErrorText(curl)
            CkHttpCurl::ckDispose(curl)
            CkStringBuilder::ckDispose(setupCommand)
            CkJsonObject::ckDispose(page)
            CkStringBuilder::ckDispose(authorizedCommand1)
            ProcedureReturn
        EndIf

        count = CkJsonObject::ckSizeOfArray(page,"entries")
        i = 0
        While i < count
            CkJsonObject::setCkI(page, i)
            If (CkJsonObject::ckStringOf(page,"entries[i].type") = "file") AND (CkJsonObject::ckStringOf(page,"entries[i].name") = wantedName)
                fileId = CkJsonObject::ckStringOf(page,"entries[i].id")
                matches = matches + 1
            EndIf

            i = i + 1
        Wend
        more = 0
        If CkJsonObject::ckHasMember(page,"next_marker") = 1
            If CkJsonObject::ckIsNullOf(page,"next_marker") = 0
                nextMarker = CkJsonObject::ckStringOf(page,"next_marker")
                If nextMarker <> ""
                    If nextMarker = CkHttpCurl::ckGetVar(curl,"marker")
                        Debug "Box returned the same pagination marker; stopping."
                        CkHttpCurl::ckDispose(curl)
                        CkStringBuilder::ckDispose(setupCommand)
                        CkJsonObject::ckDispose(page)
                        CkStringBuilder::ckDispose(authorizedCommand1)
                        ProcedureReturn
                    EndIf

                    CkHttpCurl::ckSetVar(curl,"marker",nextMarker)
                    authorizedCommand2.i = CkStringBuilder::ckCreate()
                    If authorizedCommand2.i = 0
                        Debug "Failed to create object."
                        ProcedureReturn
                    EndIf

                    CkStringBuilder::ckSetString(authorizedCommand2,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/folders/{{folder_id}}/items?usemarker=true&limit=1000&fields=id,type,name&marker={{marker}}" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34))
                    headerReplacement2.i = CkStringBuilder::ckReplace(authorizedCommand2,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
                    pageCommand = CkStringBuilder::ckGetAsString(authorizedCommand2)
                    more = 1
                EndIf

            EndIf

        EndIf

    Wend
    If matches <> 1
        Debug "Expected exactly one matching file. Matches: " + Str(matches)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkJsonObject::ckDispose(page)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(authorizedCommand2)
        ProcedureReturn
    EndIf

    CkHttpCurl::ckSetVar(curl,"file_id",fileId)
    Debug "File ID: " + fileId
    CkHttpCurl::ckClearTargetOutput(curl,"*")
    authorizedCommand3.i = CkStringBuilder::ckCreate()
    If authorizedCommand3.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(authorizedCommand3,"curl -X GET " + Chr(34) + "https://api.box.com/2.0/files/{{file_id}}?fields=id,type,name,parent,size" + Chr(34) + " -H " + Chr(34) + "Authorization: Bearer {{access_token}}" + Chr(34))
    headerReplacement3.i = CkStringBuilder::ckReplace(authorizedCommand3,"{{access_token}}",CkHttpCurl::ckGetVar(curl,"access_token"))
    success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(authorizedCommand3))
    If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 200)
        Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
        Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
        Debug CkHttpCurl::ckResponseBodyStr(curl)
        CkHttpCurl::ckDispose(curl)
        CkStringBuilder::ckDispose(setupCommand)
        CkJsonObject::ckDispose(page)
        CkStringBuilder::ckDispose(authorizedCommand1)
        CkStringBuilder::ckDispose(authorizedCommand2)
        CkStringBuilder::ckDispose(authorizedCommand3)
        ProcedureReturn
    EndIf

    Debug CkHttpCurl::ckResponseBodyStr(curl)


    CkHttpCurl::ckDispose(curl)
    CkStringBuilder::ckDispose(setupCommand)
    CkJsonObject::ckDispose(page)
    CkStringBuilder::ckDispose(authorizedCommand1)
    CkStringBuilder::ckDispose(authorizedCommand2)
    CkStringBuilder::ckDispose(authorizedCommand3)


    ProcedureReturn
EndProcedure