I have a OneDrive folder, and at the end of each bot run, I need to upload the report for the current day into that folder. However, if the bot runs multiple times in a single day, it creates multiple folders for the same day—essentially duplicates. Each new folder contains all the files from the previous one, resulting in unnecessary repetition.
The upload process to OneDrive is done through UI automation. What would be the best solution to handle this scenario efficiently?
Hie @rahul.rajendran so you are able to upload file .the issue you are facing is if bot run multiple time then do not create a new one folder.so you can create folder structure in ddMMyy format and then you can check the folder path before upload so bot first check if the folder is present then simply add file into that folder else it will create a new folder in the then branch then upload file.
Get Today’s Date – Get the current date in the format yyyy-MM-dd.
Check for Existing Folder – Search for a folder with the current date as the name.
Create Folder if Not Exists – If the folder does not exist, create it.
Upload Report – Move the report file to the folder for the current day.
1. Get Today’s Date
We need to get today’s date in the format yyyy-MM-dd to use it as the folder name.
Activity: Assign
To: todayDate
Value: Now.ToString("yyyy-MM-dd")
2. Check if Folder Exists
Now, we will check if the folder for today already exists in the target location (e.g., your OneDrive folder). You can do this using Directory.GetDirectories or UiPath’s OneDrive Activities if you’re using OneDrive directly.
Using Directory.GetDirectories (for local folder):
Here we are checking if a folder with today’s date already exists in the Reports directory.
For OneDrive (If Integrated in UiPath):
If you’re working directly with OneDrive using UiPath’s Office365 Activities, you can check for an existing folder in OneDrive.
Activity: Get Files (OneDrive) (Office 365 activities)
Folder Path: Reports (the directory in OneDrive where your reports are stored)
Result: existingFolders (Variable of type List<File>)
3. Create Folder if It Doesn’t Exist
Next, we need to create the folder if it does not exist.
Activity: If
Condition: existingFolders.Count = 0Inside the Then branch (if folder doesn’t exist), we use Create Directory:
Activity: Create Directory
Path: Path.Combine("C:\Users\<YourUsername>\OneDrive\Reports", todayDate)Or, if you’re using OneDrive directly, you can use Create Folder in Office365 activities.
4. Move the Report to the Correct Folder
Finally, move or copy the report file to the folder for the current day. You can use Move File or Copy File depending on your needs.
-Open Browser: Use Open Browser activity to navigate to your OneDrive URL.
-Locate Folder: Use Find Element to locate the folder on the page, based on its name or icon.
Condition Check:
If Find Element detects the folder (folder exists), proceed with your next steps (e.g., upload file).
If Find Element does not find the folder (folder doesn’t exist), use Click to open the “New Folder” option and create it.
You can also try to use Element Exists for a simpler check.