是否有一种简单的 Power Automate 方法可以递归地处理整个 OneDrive 文件结构而不受嵌套限制?

问题描述 投票:0回答:1

我正在尝试创建一个 Power Automate Flow,它本质上是递归地遍历 OneDrive 文件结构(多层深),而不受最多只能嵌套 8 个“For Each”循环的限制因素。

我确信有更好的方法来设计我的流程,但我不确定除了循环之外还可以使用哪些操作。我看到有几个人建议使用开关,但我不确定这会如何工作。

拖网的总体目的是在(通常)最深层(可能是 10 层)拾取 .pdf 文件,并将每个文件夹中的所有文件合并到一个文档中。

我尝试过嵌套“对于每个”操作,但不知道对此有限制。我不确定是否有解决方法,或者我是否可以使用不同结构的方法来合并所有这些文件。

recursion nested power-automate merging-data
1个回答
0
投票

在经典编程环境中执行此操作的方法是使用递归,但是,我尝试过,并且说它不执行是一个很大的轻描淡写。无论我做什么,都没有成功。

因此,我没有采用递归调用自身的过程,而是采取了一种方法,即向下遍历文件夹结构并提取每个文件夹并对其进行处理,直到找不到新文件夹为止。

这种方法唯一烦人的事情是文件和文件夹没有以良好的(有点)顺序组织,但是,它确实为您提供了您需要的内容,因为您可以过滤您最终想要的文件,即您的文件PDF 的。

根据您的文件结构,这可能需要一段时间才能运行,但只要给定时间,它就会完成。

如果没有完成,那么您可能需要寻找另一种方法,该方法将是更传统的编程方法。

这就是定义...

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "contentVersion": "undefined",
    "parameters": {
        "$authentication": {
            "defaultValue": {},
            "type": "SecureObject"
        },
        "$connections": {
            "defaultValue": {},
            "type": "Object"
        }
    },
    "triggers": {
        "manual": {
            "metadata": {},
            "type": "Request",
            "kind": "Http",
            "inputs": {
                "schema": {
                    "type": "object",
                    "properties": {
                        "folderId": {
                            "type": "string"
                        },
                        "files": {
                            "type": "array",
                            "items": {
                                "type": "object"
                            }
                        }
                    }
                },
                "triggerAuthenticationType": "All"
            }
        }
    },
    "actions": {
        "Initialize_Files_And_Folders": {
            "runAfter": {
                "Initialize_Loop_Finished": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable",
            "inputs": {
                "variables": [
                    {
                        "name": "Files And Folders",
                        "type": "array",
                        "value": "@triggerBody()?['files']"
                    }
                ]
            }
        },
        "List_Files_In_Folder_(Root)": {
            "runAfter": {
                "Initialize_Files_And_Folders": [
                    "Succeeded"
                ]
            },
            "metadata": {
                "6e202211-2856-4d17-9ded-5beb8b8626b0": "/"
            },
            "type": "OpenApiConnection",
            "inputs": {
                "parameters": {
                    "id": "6e202211-2856-4d17-9ded-5beb8b8626b0"
                },
                "host": {
                    "apiId": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness",
                    "connectionName": "shared_onedriveforbusiness",
                    "operationId": "ListFolderV2"
                },
                "retryPolicy": {
                    "type": "none"
                },
                "authentication": "@parameters('$authentication')"
            },
            "runtimeConfiguration": {
                "paginationPolicy": {
                    "minimumItemCount": 5000
                }
            }
        },
        "Initialize_Loop_Finished": {
            "runAfter": {},
            "type": "InitializeVariable",
            "inputs": {
                "variables": [
                    {
                        "name": "Loop Finished",
                        "type": "boolean",
                        "value": false
                    }
                ]
            }
        },
        "Set_Files_And_Folders_(From_Root)": {
            "runAfter": {
                "List_Files_In_Folder_(Root)": [
                    "Succeeded"
                ]
            },
            "type": "SetVariable",
            "inputs": {
                "name": "Files And Folders",
                "value": "@outputs('List_Files_In_Folder_(Root)')?['body/value']"
            }
        },
        "Do_Until_Loop_Finished_EQ_True": {
            "actions": {
                "If_New_Folders_Exist": {
                    "actions": {
                        "Set_Loop_Finished": {
                            "type": "SetVariable",
                            "inputs": {
                                "name": "Loop Finished",
                                "value": true
                            }
                        }
                    },
                    "runAfter": {
                        "For_Each_New_Folder_To_Process": [
                            "Succeeded"
                        ]
                    },
                    "else": {
                        "actions": {}
                    },
                    "expression": {
                        "or": [
                            {
                                "equals": [
                                    "@length(variables('New Folder List'))",
                                    0
                                ]
                            }
                        ]
                    },
                    "type": "If"
                },
                "For_Each_New_Folder_To_Process": {
                    "foreach": "@outputs('Compose_New_Folders_To_Process')",
                    "actions": {
                        "List_All_Files_In_A_Folder": {
                            "runAfter": {
                                "Compose_Current_Folder_Item": [
                                    "Succeeded"
                                ]
                            },
                            "type": "OpenApiConnection",
                            "inputs": {
                                "parameters": {
                                    "id": "@outputs('Compose_Current_Folder_Item')['Id']"
                                },
                                "host": {
                                    "apiId": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness",
                                    "connectionName": "shared_onedriveforbusiness",
                                    "operationId": "ListFolderV2"
                                },
                                "authentication": "@parameters('$authentication')"
                            }
                        },
                        "Compose_Current_Folder_Item": {
                            "type": "Compose",
                            "inputs": "@item()"
                        },
                        "For_Each_New_File_And_Folder": {
                            "foreach": "@outputs('List_All_Files_In_A_Folder')?['body/value']",
                            "actions": {
                                "Append_To_Files_And_Folders": {
                                    "type": "AppendToArrayVariable",
                                    "inputs": {
                                        "name": "Files And Folders",
                                        "value": "@item()"
                                    }
                                },
                                "Condition": {
                                    "actions": {
                                        "Append_To_New_Folders_List": {
                                            "type": "AppendToArrayVariable",
                                            "inputs": {
                                                "name": "New Folder List",
                                                "value": "@item()"
                                            }
                                        }
                                    },
                                    "runAfter": {
                                        "Append_To_Files_And_Folders": [
                                            "Succeeded"
                                        ]
                                    },
                                    "else": {
                                        "actions": {}
                                    },
                                    "expression": {
                                        "and": [
                                            {
                                                "equals": [
                                                    "@item()['IsFolder']",
                                                    true
                                                ]
                                            }
                                        ]
                                    },
                                    "type": "If"
                                }
                            },
                            "runAfter": {
                                "List_All_Files_In_A_Folder": [
                                    "Succeeded"
                                ]
                            },
                            "type": "Foreach"
                        }
                    },
                    "runAfter": {
                        "Reset_New_Folder_List": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Compose_New_Folders_To_Process": {
                    "type": "Compose",
                    "inputs": "@variables('New Folder List')"
                },
                "Reset_New_Folder_List": {
                    "runAfter": {
                        "Compose_New_Folders_To_Process": [
                            "Succeeded"
                        ]
                    },
                    "type": "SetVariable",
                    "inputs": {
                        "name": "New Folder List",
                        "value": []
                    }
                }
            },
            "runAfter": {
                "Initialize_New_Folder_List": [
                    "Succeeded"
                ]
            },
            "expression": "@equals(variables('Loop Finished'),true)",
            "limit": {
                "count": 5000,
                "timeout": "PT660H"
            },
            "type": "Until"
        },
        "Initialize_New_Folder_List": {
            "runAfter": {
                "Filter_For_Folders_Only_(From_Root)": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable",
            "inputs": {
                "variables": [
                    {
                        "name": "New Folder List",
                        "type": "array",
                        "value": "@body('Filter_For_Folders_Only_(From_Root)')"
                    }
                ]
            }
        },
        "Filter_For_Folders_Only_(From_Root)": {
            "runAfter": {
                "Set_Files_And_Folders_(From_Root)": [
                    "Succeeded"
                ]
            },
            "type": "Query",
            "inputs": {
                "from": "@variables('Files And Folders')",
                "where": "@equals(item()['IsFolder'],true)"
            }
        }
    }
}

第 1 节

第 2 节

第 3 节

© www.soinside.com 2019 - 2024. All rights reserved.