在C#中复制文件时检查重复的文件名 [英] check for duplicate filename when copying files in C#

查看:694
本文介绍了在C#中复制文件时检查重复的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件复制到目录并重命名为特定格式.但是如果文件名已经存在,则应在文件扩展名之前附加{1},{2}或{3}.

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.

我的代码重命名并复制了文件,并将其命名为我想要的格式,即filename.pdf,在检查是否重复时将其重命名为filename1.pdf.但是当它再次尝试复制时,它给出了一个错误文件已存在",但我希望它将其命名为filename02.pdf.

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.

请有人帮帮我.

这是我到目前为止编写的代码.

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);
            }          
        }            
    }

推荐答案

尝试一下:

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;

这篇关于在C#中复制文件时检查重复的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆