How to create folder according to last month title?

image
R:\CHARTERERS REPORTS\KCSM,KSL,KCSSG - Aging Report

@Muhamad_Izwan_Bin_Hamran

Hi,

Try this Syntax

Assign the current date to a variable using the Now function. For example, create a variable called currentDate and assign it the value Now

previousMonth=currentDate.AddMonths(-1)

. For example, if you want the folder name in the format “MMMM-yyyy”, you can use previousMonth.ToString(“MMMM-yyyy”)

Thanks

1 Add the Invoke Code activity to your UiPath main workflow and write the code mentioned below

using System;
using System.IO;

public void CreateLastMonthFolder()
{
DateTime currentDate = DateTime.Now;
DateTime lastMonthDate = currentDate.AddMonths(-1);

string folderName = lastMonthDate.ToString("yyyy-MM");

string fullPath = Path.Combine("C:\\Path\\to\\your\\directory", folderName); // Replace with your desired directory path

if (!Directory.Exists(fullPath))
{
    Directory.CreateDirectory(fullPath);
}

}

Replace "C:\\Path\\to\\your\\directory" with the actual path where you want to create the folder.

Hi @Muhamad_Izwan_Bin_Hamran

- Assign: folderPath = "C:\Path\To\Your\Folder"
- Assign: lastMonthName = ""

- Assign: fileNames = Directory.GetFiles(folderPath)

For Each: item in fileNames
  - Assign: fileName = Path.GetFileName(item)
  - Assign: fileExtension = Path.GetExtension(fileName)
  - Assign: fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName)
  - Assign: fileDateTime = DateTime.ParseExact(fileNameWithoutExtension, "MMMM",
CultureInfo.InvariantCulture)
   
   If: fileDateTime > lastFileDateTime
     - Assign: lastMonthName = fileDateTime.ToString("MMMM")
     - Assign: lastFileDateTime = fileDateTime

Make sure to Import these namespaces from the Imports Panel.
System.IO
System.Globalization

Hope it helps!!

use create folder activity and pass in this

Path.Combine("R:\CHARTERERS REPORTS\KCSM,KSL,KCSSG - Aging Report\", now.AddMonths(-1).toString("MMMM yyyy"))

e.g. if current month is July, it will create
“June 2023” folder
@Muhamad_Izwan_Bin_Hamran

Thanks for the advise.

1 Like

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