0

I want to copy files to a directory and rename to a particular format. but if the filename already exists, it should append {1}, {2} or {3} before the file extension.

My code renamed and copied the file and named it to my desired format say filename.pdf, when it checked for duplicate it renamed it to filename1.pdf. but when it tried copying again, it gave an error "file already exists" but i wanted it to have named it to filename02.pdf.

Pls can someone help me out.

Here is the code i have written so far.

    {
        string fileSource, filesToCopy, target, nextTarget;

        string sourceDir = @"C:\HCP_PDFs";            

        string destinationDir = @"C:\RenamedHcpPdfs";

        DirectoryInfo di = new DirectoryInfo(destinationDir);

        // create the directory if it dosnt exist

        if (!Directory.Exists(destinationDir))
        {
            Directory.CreateDirectory(destinationDir);
        }

        foreach (string myFiles in lstBoxFilenames.Items)
        {

            filesToCopy = myFiles;
            fileSource = Path.Combine(sourceDir, filesToCopy);         

            //Extract only HCP Name by splitting , removing file Extension and removing HCP ID 
            string hcp = filesToCopy.Split('_')[0];
            string hcpCd = filesToCopy.Split('_')[1];
            string hcpID = filesToCopy.Split('_')[2];
            string hcpName = String.Format((filesToCopy.Split('_')[3]).Replace(".pdf", ""));           

            //combine the HCP ID, HCP name and date                                                
            target = Path.Combine(destinationDir, hcp + "{" + hcpCd + "~" + hcpID + "}" + hcpName + "{2013_03_14}" + ".pdf");

            // if file exists in directory then rename and increment filename by 1
            int i = +1 ;

            nextTarget = Path.Combine(destinationDir, hcp + "{" + hcpCd + "~" + hcpID + "}" + hcpName + "{2013_03_14}" + i + ".pdf");

            if (File.Exists(target))
            {
                File.Copy(fileSource, nextTarget);
                break;
            }

            //if file does not exist, rename              
            else
            {
                File.Copy(fileSource, target);
            }          
        }            
    }
Richie
  • 3
  • 1
  • 2

2 Answers2

1

Try this :

string target = Path.Combine(destinationDir, hcp + "{" + hcpCd + "~" + hcpID + "}" + hcpName + "{2013_03_14}.pdf");
while(File.Exists(target))
        {
            i++;
            target = Path.Combine(destinationDir, hcp + "{" + hcpCd + "~" + hcpID + "}" + hcpName + "{2013_03_14}" + i + ".pdf");

        }

File.Copy(fileSource, target);
break;
Pouki
  • 1,654
  • 12
  • 18
  • 1
    Thx, please click my post as "answer resolved post" for another people ;) (the check in front of my answer) – Pouki Mar 27 '13 at 12:41
  • Just did that, was looking for it when i was able to solve it using your suggestion. – Richie Mar 27 '13 at 13:29
  • It's also a good practice to check the length of the full path that results from adding on the additional characters to the file name, in case it hits the MAX_PATH length for the system. See: [https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry](https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry). – wopr_xl Dec 22 '22 at 16:51
  • Also see [this discussion](https://stackoverflow.com/questions/3406494/what-is-the-maximum-amount-of-characters-or-length-for-a-directory) on SO about the MAX_PATH. – wopr_xl Dec 22 '22 at 19:52
0

Do this

while(File.Exists(target)) {i++;}

now declare your target path.

Chris
  • 185
  • 1
  • 1
  • 7
  • use the while loop to iterate through until a file is not found with target + i.ToString() , then rename your target to target + i.ToString() – Chris Oct 29 '14 at 20:34