Folder check/ creation logics

Hello all,
Im trying to the following
Check folder C:\2024 with year DateTime.Parse(DateTime.Now.ToString()).Year.ToString()

if exist or not, if not create a folder
Same for month and day, C:\2024\02 \01
Im trying to avoid so many if else

is there any way to avoid many if else!
if there is better logic about it


@Hazem_Saleh ,

No need to check the parent folder availability. Give the extreme inner folder name and bot will create the parent folder hierarchy if not exists.

Ashok :slight_smile:

Ideally thats all we do need:

it will create the folder and will not fail, when folder already exists

1 Like

To check for the existence of folders for the current year, month, and day and create them if they don’t exist, without using many If activities in UiPath, you can streamline the process with a loop or by sequentially checking each part of the date. Here’s a short version of how to do it:

Assign Activity: Assign the base folder path to a variable, e.g., basePath = "C:".

Build Date Strings: Create strings for the year, month, and day folders.

yearFolder = basePath + DateTime.Now.Year.ToString()
monthFolder = yearFolder + "" + DateTime.Now.Month.ToString(“00”)
dayFolder = monthFolder + "" + DateTime.Now.Day.ToString(“00”)

Check and Create Folders: Use a loop or a sequence to check if each folder exists and create it if it doesn’t. You can use the Path Exists activity followed by the Create Directory activity for each folder.
Here’s an example of the sequence without many If activities:

’ Check Year Folder ’
If Not Directory.Exists(yearFolder) Then
End If

’ Check Month Folder ’
If Not Directory.Exists(monthFolder) Then
End If

’ Check Day Folder ’
If Not Directory.Exists(dayFolder) Then
End If

This way, you only use If statements directly related to the folder creation task and avoid nested If activities. You can use Invoke Code activity to run the above VB.NET code or use separate If and Create Directory activities in a sequence.

1 Like

Thanks every one found a very nice solution only one code stage :slight_smile:

Happy to share

// Get the current date
        DateTime currentDate = DateTime.Now;
        // Path to the year folder in C drive
        string yearFolderPath = Path.Combine("C:\\", currentDate.Year.ToString() + " RPA");

        string monthFolderPath = Path.Combine(yearFolderPath, currentDate.ToString("MM") + " RPA");

        dayFolderPath = Path.Combine(monthFolderPath, currentDate.ToString("dd") + " RPA");

            if (!Directory.Exists(yearFolderPath))
                Console.WriteLine($"Year folder '{currentDate.Year} RPA' created.");

            if (!Directory.Exists(monthFolderPath))
                Console.WriteLine($"Month folder '{currentDate.ToString("MM")} RPA' created.");

            if (!Directory.Exists(dayFolderPath))
                Console.WriteLine($"Day folder '{currentDate.ToString("dd")} RPA' created.");

            Console.WriteLine("Folders checked and created successfully.");
        catch (Exception ex)
            Console.WriteLine($"An error occurred: {ex.Message}");

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