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 に答える