How to get latest file from Azure file share using Azure Logic App?

Keshav Rajput 1 Reputation point
2022-05-09T07:42:35.817+00:00

Hi Team,

I am working on scenario in that I need to transfer the file from one location to another using Azure Logic App. Let's take an example : -
Suppose I have a folder name TEST in azure file share and inside folder has multiple files. My Aim is to copy only latest file from TEST folder to TEST-1 folder.

Please provide me the steps to perform my task.

Thanks

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. VenkateshDodda-MSFT 25,241 Reputation points Microsoft Employee Moderator
    2022-05-10T04:23:20.307+00:00

    @Keshav Rajput Thanks for reaching out. Unfortunately, we don't have any built-in Azure file storage connector triggers to copy files from one location to another location. To achieve this, you need to create a custom workflow by combining multiple actions (compose, list files, get file, control, copy file). I have created a workflow by including all the actions that i have mentioned before.

    1. Used recurrence as trigger to run the workflow at a frequency for every minute.
    2. Using Get Files Pulled the list of existing files in that file share.
    3. Added for each loop to iterate those lists of files using Get File metadata using path to get the file creation time.
    4. Added the below condition to check the existing file is latest or not, if the condition is true, it will copy the file from source location to destination location. div(sub(ticks(utcNow()),ticks(body('Get_file_metadata_using_path')?['LastModified'])),3600000000)

    **workflow. Json file: **

    {  
        "definition": {  
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",  
            "actions": {  
                "For_each": {  
                    "actions": {  
                        "Condition": {  
                            "actions": {  
                                "Copy_file": {  
                                    "inputs": {  
                                        "headers": {  
                                            "ReadFileMetadataFromServer": true  
                                        },  
                                        "host": {  
                                            "connection": {  
                                                "name": "@parameters('$connections')['azurefile']['connectionId']"  
                                            }  
                                        },  
                                        "method": "post",  
                                        "path": "/datasets/default/copyFile",  
                                        "queries": {  
                                            "destination": "<destinationPath>",  
                                            "overwrite": true,  
                                            "queryParametersSingleEncoded": true,  
                                            "source": "@items('For_each')?['Path']"  
                                        }  
                                    },  
                                    "runAfter": {},  
                                    "type": "ApiConnection"  
                                }  
                            },  
                            "expression": {  
                                "and": [  
                                    {  
                                        "equals": [  
                                            "@div(sub(ticks(utcNow()),ticks(body('Get_file_metadata_using_path')?['LastModified'])),3600000000)",  
                                            0  
                                        ]  
                                    }  
                                ]  
                            },  
                            "runAfter": {  
                                "Get_file_metadata_using_path": [  
                                    "Succeeded"  
                                ]  
                            },  
                            "type": "If"  
                        },  
                        "Get_file_metadata_using_path": {  
                            "inputs": {  
                                "host": {  
                                    "connection": {  
                                        "name": "@parameters('$connections')['azurefile']['connectionId']"  
                                    }  
                                },  
                                "method": "get",  
                                "path": "/datasets/default/GetFileByPath",  
                                "queries": {  
                                    "path": "@items('For_each')?['Path']",  
                                    "queryParametersSingleEncoded": true  
                                }  
                            },  
                            "runAfter": {},  
                            "type": "ApiConnection"  
                        }  
                    },  
                    "foreach": "@body('List_files')?['value']",  
                    "runAfter": {  
                        "List_files": [  
                            "Succeeded"  
                        ]  
                    },  
                    "type": "Foreach"  
                },  
                "List_files": {  
                    "inputs": {  
                        "host": {  
                            "connection": {  
                                "name": "@parameters('$connections')['azurefile']['connectionId']"  
                            }  
                        },  
                        "method": "get",  
                        "path": "/datasets/default/foldersV2/@{encodeURIComponent(encodeURIComponent('JTJmdGVzdGluZ2ZpbGVz'))}",  
                        "queries": {  
                            "nextPageMarker": "",  
                            "useFlatListing": false  
                        }  
                    },  
                    "metadata": {  
                        "JTJmdGVzdGluZ2ZpbGVz": "/testingfiles"  
                    },  
                    "runAfter": {},  
                    "type": "ApiConnection"  
                }  
            },  
            "contentVersion": "1.0.0.0",  
            "outputs": {},  
            "parameters": {  
                "$connections": {  
                    "defaultValue": {},  
                    "type": "Object"  
                }  
            },  
            "triggers": {  
                "Recurrence": {  
                    "evaluatedRecurrence": {  
                        "frequency": "Minute",  
                        "interval": 1  
                    },  
                    "recurrence": {  
                        "frequency": "Minute",  
                        "interval": 1  
                    },  
                    "type": "Recurrence"  
                }  
            }  
        },  
        "parameters": {  
            "$connections": {  
                "value": {  
                    "azurefile": {  
                        "connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<resourcegroup>/providers/Microsoft.Web/connections/azurefile",  
                        "connectionName": "azurefile",  
                        "id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/eastus/managedApis/azurefile"  
                    }  
                }  
            }  
        }  
    }  
    

    **Here is the screenshot of the Workflow Designer: **

    200512-image.png

    Feel free to reach back to me if you have any further questions.

    0 comments No comments

  2. Mandal, Dhara V 1 Reputation point
    2022-08-29T21:22:13.867+00:00

    Hi,

    Thanks for the detailed explanation. I have a slight different requirement. I wanted to fetch the latest file from the ADLS path and send that file as an attachment on emails. I am able to send an email when a file is generated however I am still not able to attached that file with the email.

    Is it possible to help me with this?

    Thanks
    Dhara


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.