Problem with saving a word document with a duplication

Hello,

I need to save a word document after I replace some words. For some reasons I need to conserve the original file so what I’m doing is creating a copy of the document and working on the copy. My problem comes when I want to do an If activity because I want to save the file with “-2”, “-3”, etc. if the copy 1 is already done but I don’t know how to formulate the if expression.

You need do it in a while-loop. Generate the filename, check if it exist and keep looping until you find a filename that does not exist.

filename = "C:\path\test.docx"
onlyName = Path.GetFileNameWithoutExtension(filename)
count = 0

while (File.Exists(filename)) 
{ 
    count = count+1
    filename = Path.Combine(Path.GetDirectoryName(filename),  onlyName + "-" + count.ToString() + Path.GetExtension(filename))
}
1 Like

Thank you so much!!

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