[英]Can't move multiple files in a directory
I am trying to move multiple files in a directory to an archive sub folder. 我正在尝试将目录中的多个文件移动到存档子文件夹。 I used a foreach loop to do the idea.
我用一个foreach循环来实现这个想法。 Unfortunately, It can only move a file if there is only one file in the directory.
不幸的是,如果目录中只有一个文件,它只能移动一个文件。 But when I put multiple in the directory the Directory.move();
但是当我在目录中放置多个Directory.move()时; won't work.
将无法正常工作。 Can anyone help me?
谁能帮我?
static string antJsonSerializer(){
#region KDI SALES
string[] allfiles = Directory.GetFiles(@"C:\xml\"); // Put all file names in root directory into array.
string sourceDirectory = @"C:\xml\";
string destinationDirectory = @"C:\xml\Archive\";
// Check if directories are existing -- Working
bool xmlRoot = System.IO.Directory.Exists(sourceDirectory);
if (!xmlRoot) System.IO.Directory.CreateDirectory(sourceDirectory);
bool xmlArchive = System.IO.Directory.Exists(destinationDirectory);
if (!xmlArchive) System.IO.Directory.CreateDirectory(destinationDirectory);
AntHelpers drone = new AntHelpers();
foreach (string name in allfiles)
{
try
{
drone.xmltosql(@name.Trim());
//File.Move(@name, destinationDirectory + (DateTime.Now.Year).ToString() + (DateTime.Now.Month).ToString().PadLeft(2, '0') + (DateTime.Now.Day).ToString().PadLeft(2, '0') + (DateTime.Now.Hour).ToString().PadLeft(2, '0') + ".html"); //Not working
Directory.Move(@name, destinationDirectory + (DateTime.Now.Year).ToString() + (DateTime.Now.Month).ToString().PadLeft(2, '0') + (DateTime.Now.Day).ToString().PadLeft(2, '0') + (DateTime.Now.Hour).ToString().PadLeft(2, '0') + ".html");
//Directory.Move(sourceDirectory, destinationDirectory); //Not working
}
catch (Exception e)
{
//Console.WriteLine("Main Process Catch ERR: " + e.Message);
//ErrLogtoDB(string TRNTYPE, string extserial, string texttowrite, string logfilename)
AntHelpers.ErrLogtoDB("SALES", @name, "Ant JSON Serializer Failed: " + e.Message,
"LeafCutterLogFile_JSONSerializer_" + (DateTime.Now.Year).ToString() + (DateTime.Now.Month).ToString().PadLeft(2, '0') + (DateTime.Now.Day).ToString().PadLeft(2, '0') + (DateTime.Now.Hour).ToString().PadLeft(2, '0') + ".html");
}
//drone.ExtractSQLSendAntHill(); //For testing: OFF
#endregion
return " !!!! Work Complete !!!! ";
}
Directory.Move takes a source and destination directory, not a file path. Directory.Move采用源目录和目标目录,而不是文件路径。 Try this:
尝试这个:
Directory.Move(sourceDirectory, destinationDirectory);
Also, it can be run at the very end - after the foreach loop. 而且,它可以在foreach循环之后在最后运行。
You are trying to save a file with year + month + day + hour.html as "NAME". 您正在尝试将年+月+天+ hour.html的文件另存为“ NAME”。 If multiple files are there, then how can you save it with same file name?
如果有多个文件,那么如何用相同的文件名保存它? Instead you should add seconds and/or milliseconds, or use something else to distinguish the file and make it a unique name.
相反,您应该添加秒和/或毫秒,或使用其他方式来区分文件并使其具有唯一名称。 Otherwise, take file name without the extension then add year, month, day, and hour.
否则,请使用不带扩展名的文件名,然后添加年,月,日和小时。 That is why you are not able to move multiple files at a time, because when try to move the second file an exception will be thrown say "unable to move an existing file."
这就是为什么您不能一次移动多个文件的原因,因为当尝试移动第二个文件时,将引发异常,例如“无法移动现有文件”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.