How to write an expression to know whether certain words exist or not in the file name.

King Java 790 Reputation points
2023-08-25T00:09:56.0066667+00:00

This is very related to my previous posts, but I am posting it here with more focused scenario (and I did not get the solution yet).

I am trying to write an expression to do this:

Go thru each csv files, and move csv files to either folder A or folder B based on the name of csv files (whether certain words are contains or not).

User's image

The results should be boolean (true or false) because I am using IF Condition:

So, I am trying to go thru each csv files and create a logic like "if csv file has "LosAngeles" on the file name, this file will to go Folder A".

And "if csv file has word "NewYork" on the file name, this file will to go Folder B. etc"...

@contains(
	activity('Get Metadata2').output.itemName, 
	
	["LosAngeles","NewYork","SanFrancisco"]

	 )

Initially, I was trying to use array parameter Cities (to hold values) ,but I do not understand how to use the array parameter.

"Cities": [
  "LosAngeles",
  "NewYork",
  "SanFrancisco",
  // ... more cities
]

If using array parameter is way to go, please let me know.

Thanks.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,655 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nandan Hegde 36,156 Reputation points MVP Volunteer Moderator
    2023-08-25T02:55:35.8466667+00:00

    Hey,

    Either you can iterate across each loop based on the parameter that is passed as stated by Amira,

    or you can use the below expression format :

    @or(contains(item().name, 'LosAngeles'), contains(item().name, 'NewYork'))

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Amira Bedhiafi 34,336 Reputation points Volunteer Moderator
    2023-08-25T00:37:11.0233333+00:00

    I already left a comment on your last post and I will try to answer this one :
    You can use the "Get Metadata" activity to get a list of files (CSV) in a specific directory.

    Use a "ForEach" activity to iterate through each file.

    Within the ForEach activity, use an "IF Condition" activity to apply your logic for moving files. The expression could be similar to:

       @contains(item().name, 'LosAngeles')
    
    

    In the "True" activity path, you can have a "Copy Data" activity to move the file to Folder A. Similarly, in another "IF Condition" activity, you can check for other city names.

    Use "Copy Data" activities to copy the files from source to destination based on the conditions.

    Here's a rough pseudo-JSON structure:

    "ForEach": {
        "Items": "@activity('Get Metadata2').output.childItems",
        "BatchCount": 10,
        "Activities": [
            {
                "If Condition": {
                    "Expression": "@contains(item().name, 'LosAngeles')",
                    "True Activities": [
                        {
                            "Copy Data": {
                                "Source": ...,
                                "Destination": "Folder A"
                            }
                        }
                    ],
                    "False Activities": [
                        {
                            "If Condition": {
                                "Expression": "@contains(item().name, 'NewYork')",
                                "True Activities": [
                                    {
                                        "Copy Data": {
                                            "Source": ...,
                                            "Destination": "Folder B"
                                        }
                                    }
                                ],
                                ...
                            }
                        }
                    ]
                }
            }
        ]
    }
    

    If you need more advanced file manipulation, you might consider using Azure Functions with a Blob Trigger, or an Azure Logic App, which would allow you to write more complex code in a language like C# or JavaScript. If you need to authenticate against Azure AD, you would use the appropriate SDK or REST API calls for that service, such as the Microsoft Graph API.

    0 comments No comments

Your answer

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