Help on syntax for renaming file

I am trying to use a replace function to rename a file with yesterday’s date. I get an error stating “Compiler error(s) encountered processing expression. Identifier expected”. Here is my expression

file.Replace(“U:\clients\097700 ZZ KDW\0054 Practice M\DocketNavigatorReports\NEW CASE EREPORT - UIPath Report.xlsx”,“DocketNavigatorReport”+ datetime.now.AddDays(-1).ToString.(“MM-dd-yyyy”)+“.xlsx”)

Where did I go wrong? Thank you in advance for any help!

Hi, I assume “file” is your string variable and then you put a .Replace on it.
You are replacing the full path “U:[…].xlsx” with like half a path. That won’t work!

Also there is one dot ( . ) too much in your Date to String conversion - fixed that for you:
Now.AddDays(-1).ToString("MM-dd-yyyy")

Instead of using replace, consider putting your filepath together in parts. For example:
FilePath = FolderPath + FileName + FileExtension

Good luck and happy automating

So i should retrieve the original file path then create 3 variables of string (name, date, extension) and combine them into 1 string ?

I’m a little confused as to what you want to do. You have a file saved in a directory and then you want to rename it, yes?

Where and how does this Replace function come in?

Usually for renaming files we just take the Move File and define From as old folder + old name and define To as old folder + new name

There are lot of examples in the forum for this also :wink:

Yes. I have a file downloaded to a directory every day but it downloads with the same name each time. i want to rename it with a date in the name. When i tried to use the move file activity i got an error when adding in the date string. I’m sorry I am still very new to UiPath.

Ok, no worries!

You almost got it right. You don’t need to put the full filepath string into the replace function, just the part that you actually want to replace, which is usually the filename. If you really only want to add the date, you can do it like this:

NewFilePath = OldFilePath.Replace(".xlsx" , Now.AddDays(-1).ToString("MM-dd-yyyy") + ".xlsx"

This is exactly what i needed. Thank you!

1 Like

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