Create folder based on previous month or current month

Hi All,

I have a date in string to check str_last_exported_date.

If the last exported date is current month need to create current month folder . for example if last exported date is 02/15/2025 that is february. It should create Feb folder.

If the last exported date is 01/15/2025 it should create folder Jan. How my logic will be

Hi @dutta.marina

You can use the below expression,

- Assign -> FolderName = DateTime.ParseExact(str_last_exported_date, "MM/dd/yyyy", System.Globalization.Cultureinfo.InvariantCulture).toString("MMM")

Then you can use the Create folder activity and pass this variable to create with month name.

Check the below for your reference,

Hope it helps!!

Hi @dutta.marina

You could use an IF with one of the following condition

DateTime.ParseExact(str_last_exported_date, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).Month = DateTime.Now.Month
DateTime.ParseExact(str_last_exported_date, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).toString("MMM") = Now.toString("MMM")

Under the Then part, use a Create Folder activity with the Folder name property set to the

"......your path...." + Now.toString("MMM")

Do not use absolute path. Use Relative path

This should create a folder with month name only if the str_last_exported_date is equal to current month.

You have not mentioned what to do if it is not equal to current month.

I hope this helps.

Thank You
Happy automation :star_struck:

1 Like

Hey @dutta.marina kindly use the below screenshot

and if you having a specific folder just pass the folder location and a slash before the datetime method .

cheers

1 Like

To create a folder based on the last exported date:

Convert the string date to a datetime object.
Extract the month name using strftime(‘%b’) (e.g., ‘Jan’, ‘Feb’).
Check if the folder exists; if not, create it using os.makedirs().
Example code:

from datetime import datetime
import os

def create_folder(str_last_exported_date):
    date = datetime.strptime(str_last_exported_date, '%m/%d/%Y')
    month = date.strftime('%b')
    folder_path = f'./{month}'
    os.makedirs(folder_path, exist_ok=True)
create_folder("02/15/2025")  # Creates 'Feb' folder
create_folder("01/15/2025")  # Creates 'Jan' folder

This logic will create a folder like “Feb” for February and “Jan” for January.

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