多个文件,相同的名称
我想保存几个文件都具有相同的名称。我想做一些事情,名字做这样的事情: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
}
我会尝试一下,这是填写最少15个字符。 – shred1894 2012-04-27 19:12:13
也许你需要为了得到文件的扩展名使用Path.GetExtension(string path)
,之后只是削减的文件名再次添加它,扩展迭代次数得到了扩展。
你不能把文件具有相同的名称和扩展名相同的文件夹,所以你必须像一个选择:
把他们都在seprate目录王氏有意义的名称(即使文件具有相同在命名每个文件前面的内容应该是digferent)在前面
使用日期时间戳,如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数组所拥有的文件。
插入“sPathAndFileName”并使用输出的“sSaveName”来保存您的文件。
你会看到友好增加的文件名称,如:Test.txt的,文[1] txt文件,测试[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);
}
我忘记了为什么我这样做(也许故意错误地解析出一个错误的文件名),但你可以通过将sSaveName声明更改为这个更简单:string sSaveName = sPathAndFileName; – MikeTeeVee 2013-01-04 18:40:09
试试这个。 简单得多。
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);
}
我没有任何代码可以分享,主要是因为它充满了例外和废话,让我发疯。 – shred1894 2012-04-27 19:07:31
那么到底什么是你的问题? – 2012-04-27 19:08:51
我想保存这些文件,而不会丢失它们,将会有大约400个不同的文件夹,每个文件夹中有大约20个不同的文件,所以我不能手动完成。 – shred1894 2012-04-27 19:10:52