I wanted to sort files and create dynamic folder according the filename

I am new to Uipath.
I have many files of different extensions in a folder. I wanted to sort the files according to the date(ddmmyyyy) which the file names have and create a dynamic folder according to the year(yyyy) which the files name has and move the file to the particular folder.
If the file name has no date I wanted to create a dynamic folder with the current date and move that file to the respective folder.

Below are the example of file names:

  1. abc_report_10102018
  2. xyz_report_04052019
  3. Report

Hi Kinjal,

You have to perform following steps to achieve this.

1-Read the parent directory where all files are present. It will return collection.
2-use for each loop for the collection then inside loop get file name as string.
3-Now create a string variable and get substring. like this str.Substring(str.Length -4)
It will give you year for example File= 1. abc_report_10102018 it will return 2018
4- Apply check if variable value that you have created at point no 3 is null or empty then that mean file doesn’t have date.
5- Use create folder activity to create folder.
6-Use move file activity to move the file to that specific folder.

Hope it will work

Can you please show it in flow.

Thank you in advance.

Here you go Sort Folders.zip (3.4 KB)

The flow runs fine for all the files except .xlsx file
The problem I am facing is for the excel file the substring gives output the file as 018 instead of 2018.
Can you help me with this, please?

Take @anupamck solution and create an IF in the process, so when the extension is “.xlsx” or has 4 characters, do file.Substring (file.Length -9, 4)

Modified the workflow. Now it should handle files of any extension. Sort Folders_2.zip (5.5 KB)

Thank you for the modified workflow. It works well for any extension.

Hello I am still a bit confused, I am still a beginner myself, can I send you my workflow and just check it out for me please. I want to sort out annnual files, for instance, if the file is from the year 2019, then a 2019 document should be filed under a 2019 folder(that I would have to create within the workflow as well) I am not sure if I am making sense.

Thank you

How does one find out if the file is from the year 2019? Using the name? Or using properties such as date created / modified?

Hello I am not following your question?

Could you give me examples of the files you want to sort out? Or describe the task you want to perform with examples?

My biggest challenge is with the matches activity, I am struggling with filtering the right format,thats step 5 from the screengrab

I wouldn’t solve this with Regex - the code turns really messy because the expression need to capture text after the second “_” and before the “.”. Also, some files need not be in this format.

The solution I have posted actually handles this task pretty well. Alternatively, you could use the following approach

  • Check if string has two “_” characters (else get year from current date)
  • Split string and check if text after second “_” has at least 8 characters (else get year from current date)
  • Get substring of the characters after the second “_” and check if it is a year (else get year from current date)

You see? This approach is quite messy too, but is still cleaner than the regex approach.

Thank you, a little demonstration with a workflow if you may.
thank you


Thank you very much for everything, have yourself a blessed day further

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.