多个文件,同名 [英] Multiple files, same name

查看:76
本文介绍了多个文件,同名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存一些具有相同名称的文件。我想做一些名称类似的事情:file.extension文件[1] .extension文件[2] .extension我已经尝试过此 http://www.naspinski.net/post/保存多个文件-same-name.aspx 但这对我不起作用。

I am trying to save a few files that all have the same name. I want to do something where the names do something like this: file.extension file[1].extension file[2].extension I have tried this http://www.naspinski.net/post/Saving-multiple-files-of-the-same-name.aspx but it didn't work for me.

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

here is some code to look at(not the entire thing, just the relevant parts),

        {
            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;


推荐答案

这太麻烦了。另外,如果 .extension出现在文件名中的其他位置(而不是末尾),则会中断(因此,使您的字符串处理比示例代码更聪明)。如果需要,可以使用 Path.GetExtension(path)

This is off the top of my head. Also, if ".extension" occurs somewhere in the filename other than at the end, this would break (so make your string handling a bit smarter than the sample code). If needed, you can get the extension using 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
}

这篇关于多个文件,同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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