How to Copy a file with the same name, without overwriting

I have an excel file that I need to save with a certain name in the end, whenever this certain file encounters an error. As example, let’s say file1 had and error, I’ll copy and savethat file with “With Error” in the end, and I would do that everytime it would encounter an error. Is there a way like to copy the file and save it as, file1withError then next one is file1withError(1) or file1withError_1

Hi

We can use Copy file activity

https://docs.uipath.com/activities/docs/copy-file

Cheers @Shinjid

I did but it says file already exists.

1 Like

Fine in that case

Let’s do it with couple of activity as it is not directly possible with single activity to save a already existing file with a renamed file
Rather naming it as 1 and 2 at last lest name or with timestamp which will surely save each file every time without any error

  • use a PATH EXISTS activity and mention the filepath you have and check whether that it gives true or false
  • get it with a Boolean variable
  • use a IF activity and pass the above Boolean variable like this
    bool_variable = True

-if true It means it exists so goes for THEN part where use a ASSIGN activity like this

Str_newfilepath = String.Join(“//“,Split(“your filepath,”//“).Take(Split(“your filepath,”//“).Count-1).ToArray())+”//“+Path.GetFileNameWithoutExtension(“your filepath”)+”_”+Datetime.ToString(“hh_mm”)+Path.GetExtension(“your filepath”)

This expression will save the file with hour and minutes at last which will make the filename unique

Cheers @Shinjid

It worked thank you. Btw, do you know how to remove all the sheets on the excel file? I was supposed to create this file1Witherror.xlsx file then copy the sheets where an error occured.

Hi

We can use delete sheet activity on this

Cheers @Shinjid

Thanks I’ll try this too.

1 Like

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