I am using ‘Move File’ activity to move a number of files from one folder to another. I am getting an exception that if the file already exists that the file can not be moved.
What I want to do is to save the new same name file as XXX_backup (the existing file name is XXX). Can anyone know how to do it? Should I use Try catch Activity as I have another condition to “move file”, which is file containing (xlsx, xls, csv).
Path Exists activity returns True if file which you input exists.
If fileExist is false, simply execute Move File Activity and set exit flag=true.
If fileExist is true, we need to rename filename as the following.
For example, let’s assume filePathTo is "c:\temp\test.xlsx" System.IO.Path.Combine method combines 2 or more path name.
System.IO.Path.GetDirectoryName(filePathTo) returns directory of filePathTo like "c:\temp"
System.IO.Path.GetFileNameWithoutExtension(filePathTo) returns filename without extension like "test"
System.IO.Path.GetExtension(filePathTo)) returns extention like ".xlsx"
As a result, System.IO.Path.GetFileNameWithoutExtension(filePathTo)+"_backup"+System.IO.Path.GetExtension(filePathTo) returns "test_backup.xlsx"
Finally we can get "c:\temp\test_backup.xlsx"
However, there is still a possibility "c:\temp\test_backup.xlsx" already exists in the folder.
So we need to go back to loop start and check again if there is not.
I have encountered the error after combined your ans with Yoichi. The file still cannot be saved when “that file already exists”. could you please help and see how can I modify.
In path exist variable
DestinationPath=“C:\Users\mason_wang\Desktop\Uipath training\Destination”
and ypu want to chose file path to check
then u need to give file name also in that path
Many thx to your solution. But thing is the file name is unknown in temp file. How can I assign the unknown file name and check if this file name exists in destination folder.
Here is my project. could you pls check. Main.xaml (8.3 KB)
Are your unknown files are in a temp folder which all you want to move another folder ryt?
As You are iterating that files with path u will get to know abt that file name using
Path.GetFilename(File)
Do you mean that I can use Assign Activity to get all file names in Temp folder (Path.GetFilename(file)), and using it to replace the specific file path showing in From?
U will get all files when u use Directory.GetFiles(FolderPath)
Use it in For Each
So each iterate gives you file path say: D:\Prathamesh\Data\Abc.xlsx
To get File name use Path.GetFileName(Item.Tostring) it will give u Abc.xlsx
So u can use to rename it using move File
You have files which u want to move are in a folder right?
If yes
Then do this
Use For Each : Directory.GetFiles
Then Use
Path Exist For: DestinationFolderPath+Path.getfilename(Item.ToString).ToString
If Path Exist
Then Use Move File
For existing File Renaming
From: DestinationFolderPath+Path.getfilename(Item.ToString).ToString
To: DestinationFolderPath+“Backup_”+Path.getfilename(Item.ToString).ToString
2.For new one
From:Item.Tostring
To: DestinationFolderPath+Path.getfilename(Item.ToString).ToString
Else
Move File
From:Item.Tostring
To: DestinationFolderPath+Path.getfilename(Item.ToString).ToString
Thank yo so much Pratham. I have followed your steps and the only problem is the backup file is not been saved in destination folder and the filename has prefix “DestinationTesting”