多个文件,同名
本文关键字:同名 文件 | 更新日期: 2023-09-27 18:32:25
我正在尝试保存一些具有相同名称的文件。我想做一些名称做这样的事情: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;
这是我的
头顶。 此外,如果".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
}
插入
"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);
}
试试这个。简单得多。
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);
}
可能需要使用该Path.GetExtension(string path)
才能获得文件的扩展名,然后只需剪切文件名,在其上附加迭代编号并再次扩展名。
您不能将具有相同名称和扩展名的文件放在同一个文件夹中,因此您有一个选项,例如:
-
将它们全部放在具有有意义名称的单独目录中(即使文件具有相同的名称,内容也应该被挖掘)
-
在前面使用日期时间戳,就像每个文件前面的 YYYYMMDD 一样(但它违反了"相同的文件名"规则)
-
这同样可以应用于扩展
我不禁觉得这些年来我这样做了太多次。 :)
尝试以下操作:
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);
}
下面是一个如何帮助自己的示例:
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数组中有文件,你也可以这样做。