Try Catch_file already exists

Hi All,

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).

Thx

Hi,

Hope the following helps you.

filePathTo = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filePathTo),  System.IO.Path.GetFileNameWithoutExtension(filePathTo)+"_backup"+System.IO.Path.GetExtension(filePathTo))

Regards,

1 Like

Before moving file use Path Exist
Then use If condition If Path Exist Output is true then use Copy File to renaming it

(Ex: C:/Abc.xlsx to C:/Abc_Backup.xlsx)

and then use Move file activity

1 Like

Hi Yoichi,

Thank you for your answering. I am not quite sure I can understand your capture. Can you explain more or let me know how to combine two

Hi,

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.

Regards,

1 Like

Hi Yoichi,

After followed your steps, I still got an Exception of “cannot create file when that file already exists”.

Can you help me to check. thx

Hi Pratham,

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.

Many thx

Hi,

Can you share your workflow file?

Regards,

Main.xaml (8.3 KB)

Hi Yoichi, Pls help me check. Appreciate

FileMove.xaml (7.5 KB)
check this

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?

11

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

I still didnt figure out by what you have said. :joy: :joy:

OK

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

  1. 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

1 Like

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”

Can you help me to solve this final problem.

image

You havent use \ after folder name DestinationTesting thats why

I see. many thx. It works now. Can I ask one more question for my information?

What if I run the 2nd time which “backup_testing1” already exists via 1st generation. How to handle this situation?