Sorting Files based on Name & Creating new folders

Hey,

I’m having a requirement where I should sort files based on name, for example:
If the file name follows the format of “CustomerName_Report_DDMMYYYY.xlsx/.pdf ” sort this into a folder based on the year (YYYY )
If it doesn’t follow the above format I should create a new folder with today’s date (format MMDDYYYY ) and move them there.

I know I should be using a for each loop, but am unsure how to filter based on name and create new folders.

Appreciate any support.

Hi @MHarakeh

You can try this
image

Regards
Sudharsan

Hi @Sudharsan_Ka

Thanks for answering, but how can I filter based on name for example I want all folders that start with CustomerName - DDMMYYYY to be moved to a new folder (that’ll be created within the automation) based on the Year.
and if the folder doesn’t follow the following naming format then it should be moved to a folder based on today’s date

Hi @MHarakeh ,

Could you maybe provide us with some visuals (Screenshots) of the files with that particular format and where will the each file be placed after the operation, this is asked just to avoid small confusions that would arise when understanding your initial post.

Also does this mean, you would want to move the file to a Folder which has that respective year ?

Maybe something in the below manner ? But it does open some other questions as well.
image

Hi @supermanPunch ,

If file name starts with customer name and then date Example(“JaneDoe_Report_28102019.pdf ” then we create a file based on the year mentioned (2019) and place the folder in it.

If the file name doesn’t follow this format ( “JaneDoe_Report_28102019.pdf ”), then we create a folder with today’s date and place those file’s in.

I hope my explanation is clear.

@MHarakeh ,

In that case, Maybe the below was the required Skeleton/workflow :
image

In the If Activity the condition used is using the Regex.IsMatch method to check if there is a file matching the format specified. If there is a Match then we capture the Year value only from it and created a Folder in it’s name and place the file in that folder.

If the File name doesn’t match we create a Folder with Today’s Date in the format ddMMyyy and place the file in it.

Condition used in If Activity:

Regex.IsMatch(Path.GetFileNameWithoutExtension(CurrentFile.FullName),"CustomerName_Report_\d{8}",RegexOptions.IgnoreCase)

Fetching the Year from file name :

YearFromFile = Split(Path.GetFileNameWithoutExtension(CurrentFile.FullName),"_")(2).Substring(4,4)

Hi @supermanPunch,

Thanks for your time, should I be declaring any variable to use the RegEx.Ismatch & RegExOptions ?
As am getting an error that they’re not declared…
image

@MHarakeh ,

Perform an import on System.Text.RegularExpressions to resolve that error :
image

@supermanPunch

When I executed the whole thing it’s throwing an error, that cannot create file if file already exists.
Attached you can find the workflow.
Download-Unzip.File.xaml (18.6 KB)

@supermanPunch

I figured out to fix some errors, now the workflow is executing well but the following condition:
Regex.IsMatch(Path.GetFileNameWithoutExtension(CurrentFile.FullName),“CustomerName_Report_\d{8}”,RegexOptions.IgnoreCase)

Is not working as intended as no folders are matching this condition. (File name example: Chandler Bing_Report_25072019) this file should match the written condition but it’s not.

Any idea ?

@MHarakeh

If you know the name of the customer try links this

Regex.IsMatch(Path.GetFileNameWithoutExtension(CurrentFile.FullName),customernameVariable+“Report\d{8}”,RegexOptions.IgnoreCase)

Regards
Sudharsan

_ will be there @MHarakeh

Regex.IsMatch(Path.GetFileNameWithoutExtension(CurrentFile.FullName),Customer Variable “Report\d{8}”,RegexOptions.IgnoreCase)

@MHarakeh

Condition should Like the above one some how the underscore is replaced as italic formats

Regards
Sudharsan

Thanks @Sudharsan_Ka

Definitely the customer name is dynamic thus it can’t be known, but the file name will always be something like that “FName LName_Report_25072019”.
Will the mentioned condition in the image work in that case?

Yes

You need to assign the dynamic customer name in the variable CustomernameVariable

That is

CustomernameVariable= Row(“Customername”).ToString

Or
CustomenameVariable= Chandler Bing

Regards
Sudharsan

Didn’t get that: Row(“Customername”).Tostring

Can you please explain ?

Thanks for your help @Sudharsan_Ka.

Even if trying to assign custname = row(“customername”).ToString

Facing an error that ROW isn’t declared

You haven’t used for each row in datatable?

I thought you have used for each row in datatable so I said that row (“current folder”).ToString

Okay leave that

Regards
Sudharsan

No no, my files are getting downloaded into a folder and I have to sort them based on the condition mentioned above…

Thanks @Sudharsan_Ka

@MHarakeh ,

Apologies for the late reply.

If the Customer Name is dynamic, then we could for the below Expression :

Regex.IsMatch(Path.GetFileNameWithoutExtension(CurrentFile.FullName),"*_Report_\d{8}",RegexOptions.IgnoreCase)

Let us know if this works.