Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to generate the filename of a copied file?

Does C# have any functions to produce a new name for a copied item ?
For example , if I have a string called "Folder" , I need a function to produce a string "Copy of Folder".... and given the string "Copy of Folder" the function should produce "Copy of Folder (1)" and so on ....

like image 611
MadSeb Avatar asked Dec 29 '22 02:12

MadSeb


1 Answers

You can write a loop, like this:

string baseName = @"C:\Parent\Copy of Folder", actualName = baseName;
int index = 0;

while(File.Exists(actualName) || Directory.Exists(actualName))
    actualName = baseName + " (" + (++index) + ")";

Depending on your use-case, you should probably put this in a static utility method.

like image 94
SLaks Avatar answered Jan 13 '23 17:01

SLaks