Replace document extension

I have one document Name : RPA.docx (or) RPA.doc … then I have to raplce name like RPA_1234.docx (or) RPA_1234.doc

Condition: we don’t know particular document extension

Hi @shaik.muktharvalli1

You can use

Assign
Variable=Path.getextension(filename)

Then use
Variable.replace(“oldvalue”,“new value”)

if document either .docx or .doc then condition?

variable=path.getfilewithoutextension
if vaiable=RPA
assign
Then Assign filename = Path.GetFileNameWithoutExtension(filename).replace(variable,“RPA_1234”) + “.docx”
Else Assign filename = Path.GetFileNameWithoutExtension(filename).Replace(varaible,“RPA_1234”) + “.doc”

H! @shaik.muktharvalli1

you can use this expression for your output

str=RPA.docx (or) RPA.doc

variable= System.Text.RegularExpressions.Regex.Replace(str,“RPA”,“RPA_1234”)

for the reference you can see the screenshot

if Path.GetExtension(filename) = “.doc”
Then Assign filename = Path.GetFileNameWithoutExtension(filename) + “.docx”
Else Assign filename = Path.GetFileNameWithoutExtension(filename) + “.doc”

This will change the extension to .doc if it’s currently .docx or change it to .docx if it’s currently .doc

Hi,

Can you try Rename file activity with Keep extension option as the following?

image

Regards,

Hi @shaik.muktharvalli1

Try with this below approach

  1. Assign Activity:
FileName = Path.GetFileNameWithoutExtension("C:\Path\to\RPA.docx")
  1. Assign Activity:
New_FileName= Path.GetFileNameWithoutExtension(FileName) + "_1234"+ Path.GetExtension(FileName)
  1. Assign activity
NewFilePath = Path.Combine(Path.GetDirectoryName(FileName), New_FileName)
  1. Invoke Method
Target Object: `new System.IO.FileInfo(OriginalFilePath)`
MethodName: MoveTo
Parameters: NewFilePath

Or

You can try with Rename File activity

Regards
Gokul

@Yoichi RPA name may be dynamically changing

This accounts for the filename being anything.

how to add suffix to filename and how to save file after add suffix to file

Path.GetFileNameWithoutExtension(filename).replace(variable,“RPA_1234”) + “.docx

  • “.docx” and + “.doc” add the extension.

Do you really need to save the file, or just rename it? If you’re just trying to rename it, use the Rename File activity and put this in the filename:

if(Path.GetExtension(filename) = “.doc”,Path.GetFileNameWithoutExtension(filename) + “.docx”,Path.GetFileNameWithoutExtension(filename) + “.doc”)

The replaceing of .doc by.doc in the else condition is of course a wee bit redundant.

Also note that changing a file’s extention also changes the way application read that file. Be sure you don’t corrupt your documents simply by changing the excention. (Seen so many people convering .csv files into .xlsx files which gives… weird results)

If your goal is to transfor the filetype from .doc to .docx, you may need to open it in the proper applicatio and use save-as functionalities to have the application change the mime-type of the file, as well as the extention.