2

我正在尝试保存一些都具有相同名称的文件。我想做一些名字做这样的事情:file.extension file[1].extension file[2].extension 我试过这个http://www.naspinski.net/post/Saving-multiple-files- of-the-same-name.aspx但它对我不起作用。

这是一些要查看的代码(不是全部,只是相关部分),

        {
            string thepathoflife = Path.GetFullPath(file);
            //CreatetheFolder(file)
            string filetocopy = file;
            object bob = file.Clone();
            string bobby = bob.ToString();
            string location = file;
            bool b = false;
            string extension = Path.GetExtension(file);
            string thenameofdoom = Path.GetFileNameWithoutExtension(file);
            string filename = Path.GetFileName(file);
            ////bobby.Move(@"\\TEST12CVG\Public\Posts\Temporaryjunk" + filename);
            // string oldlocation = filename+extension;

            if (extension == ".pst" ||
              extension == ".tec" ||
              extension == ".pas" ||
              extension == ".snc" ||
              extension == ".cst" ||
              extension == ".xml")
            {
                b = true;
            }

            if (thenameofdoom == "Plasma" ||
              thenameofdoom == "Oxygas" ||
              thenameofdoom == "plasma" ||
              thenameofdoom == "oxygas" ||
              thenameofdoom == "Oxyfuel" ||
              thenameofdoom == "oxyfuel")
            {
                b = false;
            }


            if (b == true)
            // System.IO.File.WriteAllText(newlocation, bobby);
            {
                //string rootpath = (@"\\sigmatek.net\Documents\Customers\A");
                var findLevel = 6;
                var path = @thepathoflife;
                var levels = path.Split(Path.DirectorySeparatorChar);
                var second = levels.Length > findLevel ? levels[findLevel] : null;

                Directory.CreateDirectory(@"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom);
                string newlocation = (@"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom);
                string newPath = System.IO.Path.Combine(newlocation, second);
                System.IO.Directory.CreateDirectory(newPath);
                string newlocationb = Path.GetFullPath(newPath);

                    string newb = System.IO.Path.Combine(newlocationb, filename);

                        while (File.Exists(newb))
                            {
                               int number = 1;
                               bool found = false;
                               do
                               {
                                   string candidate = newb.Replace(extension, "[" + number++ + "]"+ extension);
                                   if (!File.Exists(candidate)) found = true;

                                {
                                    File.Copy(thepathoflife, candidate);
                              }
                                   // Candidate has a valid file name


                               }
                               }
                    //File.Move(@"\\TEST12CVG\Public\Posts\Test\", @"\\TEST12CVG\Public\Posts\Test\" + thenameofdoom + second);
                    System.Console.WriteLine("Success: " + filename + "--" + thepathoflife);
                    b = false;
4

7 回答 7

1

这不是我的想法。此外,如果“.extension”出现在文件名中的某个位置而不是末尾,这会中断(因此使您的字符串处理比示例代码更智能)。如果需要,您可以使用Path.GetExtension(path)

if (File.Exists(fn))
{
   int number = 1;
   bool found = false;
   do
   {
       string candidate = fn.Replace(".extension", "[" + number++ + "].extension");
       if (!File.Exists(candidate)) found = true;
   }
   // Candidate has a valid file name
}
于 2012-04-27T19:10:53.393 回答
1

插入“ sPathAndFileName ”并使用输出的“ sSaveName ”来保存您的文件。
您将看到友好的递增文件名,例如:Test.txt、Text[1].txt、Test[2].txt 等...

    int index = 0;
    string sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                     + Path.GetFileNameWithoutExtension(sPathAndFileName)
                     + Path.GetExtension(sPathAndFileName);
    while (File.Exists(sSaveName) == true)
    {
        sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                  + Path.GetFileNameWithoutExtension(sPathAndFileName)
                  + "[" + (++index).ToString() + "]"
                  + Path.GetExtension(sPathAndFileName);
    }
于 2012-10-09T23:29:42.500 回答
1

尝试这个。简单得多。

string filename = "C:\bla.txt";
string filenameNoPath = Path.GetFileNameWithoutExtension(filename);
string temppath = Path.GetDirectoryName(filename);
string extension = Path.GetExtension(filename);

if (!File.Exists(path))
{
     File.WriteAllBytes(filename, file);
}
else
{
     do
     {
           counter++; // we're here, so lets count files with same name
           string path = temppath + "\\" + filenameNoPath + "(" + counter.ToString() + ")" + extension;
     } while (File.Exists(path));

     File.WriteAllBytes(path, file);
 }
于 2015-06-24T09:16:36.450 回答
0

可能您需要使用 Path.GetExtension(string path)来获取文件的扩展名,然后只需剪切文件名,在其上附加迭代编号并再次扩展。

于 2012-04-27T19:11:43.150 回答
0

您不能将具有相同名称和扩展名的文件放在同一个文件夹中,因此您有如下选项:

  • 将它们全部放在具有有意义名称的单独目录中(即使文件具有相同的名称,内容也应该是不同的)

  • 在前面使用日期时间戳,例如在每个文件前面使用 YYYYMMDD(但它违反了“相同文件名”规则)

  • 同样可以应用于扩展

于 2012-04-27T19:11:57.947 回答
0

我不禁觉得这些年来我这样做了太多次。:)

尝试以下方式:

        List<string> filesToSave = new List<string>();
        var fileName = "myfile.ext";
        var baseName = Path.GetFileNameWithoutExtension(fileName);
        var extension = Path.GetExtension(fileName);

        int counter = 1;
        foreach (var fileToSave in filesToSave)
        {
            var tempFileName = fileName;
            while (File.Exists(tempFileName))
            {
                tempFileName = string.Format("{0}{1}.{2}", baseName, counter, extension);
                counter++
            }

            File.WriteAllText(tempFileName, fileToSave);
        }
于 2012-04-27T19:12:49.903 回答
0

这是一个如何帮助自己的示例:

        string[] files = { "file.txt", "file.txt", "file.txt" };
        for(int i=0;i<files.Length;i++)
        {
            string fileName = Path.GetFileNameWithoutExtension(files[i]);
            string extension = Path.GetExtension(files[i]);
            fileName = fileName + (i + 1);
            //if you would like to aqdd square brackets, you can change upper line to:
            fileName = string.Format("{0}{1}{2}{3}", fileName, "[", (i + 1), "]");
            files[i] = String.Concat(fileName, extension);
        }

如果您在 FileInfo 数组中有文件,您也可以这样做。

于 2012-04-27T19:17:29.773 回答