Get elements from folder names and find maximum element

Hi,

I have directory with folders, whcih is actually number of months.
Months num
I need to find the last month to create folder for next month that should be 09.2020
Can you advise hoa can I do it?
I created process as it is shown on the pic:


it workd fine for month_f varaible
item.ToString.Split(“"c)
and does not work for month_o variable
month_f.ToString.Split(”."c)
All I need is to find the last month from the folder names and create folder with next month

@Apple1

Please check below thread to find out the last created directory.

@lakshman - I don’t think that is what the OP wants. If a user were to accidentally create a new folder in there, then it would just give that folder name. OP wants to read the directory names, convert it to a date in MM.yyyy format, then get the next month.

@Apple1 You are on the right track! Now that you have an array (of string) of all the directory names, you should convert it into an array of datetime variables instead. This will allow you to use various datetime methods and properties which will help you achieve your goal. I have attached a workflow showing how to do it and described it below as well. Please let me know if you have questions.

Directory.GetDirectories returns the full path rather than just the name of the directory (e.g. C:\temp\01.2020 instead of only 01.2020). To remedy this, we will use linq to pull out only the directory name property instead. Directory.GetDirectories(YourPath).Select(function(x) new DirectoryInfo(x).Name).ToArray()

NOTE: We could build this next piece into our above linq, but i prefer to use built-in activities where possible instead.

Next, we use a for each loop to convert each item in the string array to a datetime variable. We can do this using the ‘add to collection’ activity (after initializing a list of datetime), and converting the string to datetime like so: Datetime.ParseExact(item,"MM.yyyy",System.Globalization.CultureInfo.InvariantCulture)

Next, we will use the .Max() function on our list of datetimes to find the latest date, add 1 month with the .AddMonth() method, and convert it back into a string with the .ToString() method and formatting it the way we like in MM.yyyy format. NameListDates.max().AddMonths(1).tostring("MM.yyyy")

And finally, we use the ‘create directory’ activity to actually create the directory. Use the path.combine() method to combine the original path of the directory (I called it YourPath) and the newly found date (I called it NewDateFolder). Path.Combine(YourPath,NewDateFolder)

delete.xaml (7.1 KB)

1 Like

Hi @Dave,

Thank you for your response!
I used your robot placing my folder path in YourPath variable, but getting error when run script:


what should I change?

It looks like at least one of the folder names in YourPath is not in MM.yyyy format (e.g. January 2010 would be 01.2010). If you’re sure they are all the right format, then double check that the value for YourPath is correct (easy way is to copy this and paste it into the ‘URL’ portion of windows explorer)

If you want to skip those, you could use a try-catch around the ‘add to collection’ activity. You could also alter your linq to add a .where() statement, but the try-catch would be easier and more straightforward

1 Like

@Dave
Thank you very much!
The path was not correct. I put right path and it works perfect!

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