Needing to move/rename a file into a folder, with a file path checker

Hi guys

I’m needing to move files into a folder, but if the file name already exists, I need to add the next sequential digit into the file name to make it unique.

EG:

If the file “cat” exists in the destination folder, make the new file name being moved into it “cat 1”
If “cat 1” exists, make the new file name coming into it “cat 2”

Etc.

Can anyone give me some ideas on how to achieve this?

You can achieve with Do While loop -

Counter = 0
fileExists = True    
Do While
{
    if(file.Exists(path))
    {
       counter = counter + 1
       path = directory + filename + counter.ToString
    }
    else
    {
       fileExists = False
    }
}
condition
    fileExists = True

Regards,
Karthik Byggari

2 Likes

Perfect, thanks so much!

1 Like

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