How to move/copy each file in last month folder

Can show me how to copy each file in last month folder to current folder that I’ve created.

Hi @Muhamad_Izwan_Bin_Hamran

In copy file activity From section give CuurntFile.FullName and To section give destination folder location

“C:\Users\lrtetala\Documents\UiPath\Electricity Task_Excel\New Folder”

Hey @Muhamad_Izwan_Bin_Hamran Why don’t you try copying the folder itself
Eg: Copy the last month folder and paste it in a location and then use rename folder activity and rename it to current folder name
image

Hi @Muhamad_Izwan_Bin_Hamran

To achieve the task of copying each file from the last month’s folder to the current folder using UiPath, you can follow these steps:

  1. Get the last month’s folder path:

    • Use UiPath activities to get the current date.
    • Subtract one month from the current date to get the last month’s date.
    • Format the date as required to get the folder name (e.g., “YYYY-MM” format).
    • Combine the folder name with the base path to get the last month’s folder path.
  2. Create the current folder:

    • Use the “Create Folder” activity to create a folder with the desired name for the current month.
  3. Get the list of files from the last month’s folder:

    • Use the “Directory.GetFiles” or “Directory.GetFiles with options” activity to retrieve the list of file paths from the last month’s folder.
  4. Copy files to the current folder:

    • Use a For Each loop to iterate through the list of file paths obtained in step 3.
    • Inside the loop, use the “Copy File” activity to copy each file from the last month’s folder to the current month’s folder.

Here’s a step-by-step guide on how to implement this in UiPath:

  1. Initialize variables (if needed):

    • Create variables to store the current date, last month’s date, last month’s folder path, and the current month’s folder path.
  2. Get the last month’s folder path:

    • Use the “Assign” activity to calculate the last month’s date:
      currentMonth = DateTime.Now.Month
      lastMonth = If(currentMonth = 1, 12, currentMonth - 1)
      
    • Use the “Assign” activity to format the last month’s date as “YYYY-MM” (e.g., “2023-06”):
      lastMonthFolderName = DateTime.Now.AddMonths(-1).ToString("yyyy-MM")
      
    • Combine the folder name with the base path to get the last month’s folder path:
      lastMonthFolderPath = Path.Combine(baseFolderPath, lastMonthFolderName)
      
  3. Create the current folder:

    • Use the “Create Folder” activity to create a folder for the current month:
      currentMonthFolderName = DateTime.Now.ToString("yyyy-MM")
      currentMonthFolderPath = Path.Combine(baseFolderPath, currentMonthFolderName)
      Create Folder: currentMonthFolderPath
      
  4. Get the list of files from the last month’s folder:

    • Use the “Directory.GetFiles” or “Directory.GetFiles with options” activity to get the list of file paths from the last month’s folder:
      filesInLastMonth = Directory.GetFiles(lastMonthFolderPath)
      
  5. Copy files to the current folder:

    • Use a For Each loop to iterate through the list of file paths obtained in step 4:
      • Set the TypeArgument of the For Each loop to String.
      • Use the “Copy File” activity inside the loop to copy each file from the last month’s folder to the current month’s folder:
        Copy File: item (source) To: Path.Combine(currentMonthFolderPath, Path.GetFileName(item)) (destination)
        

Hope it helps!!

@Muhamad_Izwan_Bin_Hamran

Assign: lastMonthFolderPath = “C:\Path\To\LastMonthFolder”
Assign: currentFolderPath = “C:\Path\To\CurrentFolder”

For Each (item) in Directory.GetFiles(lastMonthFolderPath)
Copy File
Source: item
Destination: Path.Combine(currentFolderPath, Path.GetFileName(item))

@Muhamad_Izwan_Bin_Hamran

  1. Use the “Assign” activity to get the current date and calculate the date of the last month.

    Assign:
    currentDate = DateTime.Now
    lastMonthDate = currentDate.AddMonths(-1)

  2. Use the “Create Directory” activity to create a folder with the current date as the folder name.

    Create Directory:
    FolderPath: "C:\YourFolderPath" + lastMonthDate.ToString(“yyyy-MM”)

  3. Use the “Directory.GetFiles” activity to get a list of files from the last month’s folder.

    Directory.GetFiles:
    FolderPath: "C:\YourLastMonthFolderPath" // Replace with the path to the last month’s folder
    Result: filesList (List)

  4. Use a “For Each” activity to loop through the files in the “filesList.”

    For Each:
    TypeArgument: String
    Values: filesList

  5. Inside the “For Each” loop, use the “Copy File” activity to copy each file to the current month’s folder.

    Copy File:
    Source: item
    Destination: "C:\YourFolderPath" + lastMonthDate.ToString(“yyyy-MM”) + "" + Path.GetFileName(item)

I hope it helps!!

Hi,
You can try:



Regards,

Hi can show me in details with picture ?

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