尝试使用子文件夹 C# 将文件从一个文件夹移动到另一个文件夹
Trying to move files from one folder to another with subfolders C#
我想将一个文件夹中的所有照片重新整理到另一个路径的子文件夹中,我想在其中创建以文件创建日期命名的新子文件夹。
示例:
photo1.png (creation date 12.02.2015)
photo2.png (creation date 12.02.2015)
photo3.png (creation date 13.02.2015)
--> create two subfolders: "12-feb-2015" with photo1.png and photo2.png and "13-feb-2015" with photo3.png
我编写了将照片复制到另一个文件夹并创建包含当前日期的子文件夹的代码。但是我不知道如何创建以文件创建日期命名的子文件夹。
public class SimpleFileCopy
{
static void Main(string[] args)
{
// Specify what is done when a file is changed, created, or deleted.
string fileName = "*.png";
string sourcePath = @"C:\tmp";
string targetPath = @"U:\";
// Use Path class to manipulate file and directory paths.
string sourceFile = Path.Combine(sourcePath, fileName);
//string destFile = Path.Combine(Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy") , fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!Directory.Exists("U:\" + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy"))))
{
Directory.CreateDirectory("U:\" + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy")));
}
else
// To copy a file to another location and
// overwrite the destination file if it already exists.
{
foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
try
{
file.CopyTo(e.FullPath.Combine(targetPath + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy")), file.Name));
}
catch { }
}
}
}
}
您无法接听许多 Directory.CreateDirectory
电话。只需枚举您的源文件夹文件,然后使用 file.CreationTime
获取日期,调用 Directory.CreateDirectory
(无论它是否已经存在)然后复制您的文件。
string fileName = "*.png";
string sourcePath = @"C:\tmp";
string targetPath = @"U:\";
foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
var targetFolderName = file.CreationTime.ToString("dd-MMM-yyyy");
var dir = Directory.CreateDirectory(Path.Combine(targetPath, targetFolderName));
file.CopyTo(Path.Combine(dir.FullName, file.Name), true);
}
我想将一个文件夹中的所有照片重新整理到另一个路径的子文件夹中,我想在其中创建以文件创建日期命名的新子文件夹。
示例:
photo1.png (creation date 12.02.2015)
photo2.png (creation date 12.02.2015)
photo3.png (creation date 13.02.2015)
--> create two subfolders: "12-feb-2015" with photo1.png and photo2.png and "13-feb-2015" with photo3.png
我编写了将照片复制到另一个文件夹并创建包含当前日期的子文件夹的代码。但是我不知道如何创建以文件创建日期命名的子文件夹。
public class SimpleFileCopy
{
static void Main(string[] args)
{
// Specify what is done when a file is changed, created, or deleted.
string fileName = "*.png";
string sourcePath = @"C:\tmp";
string targetPath = @"U:\";
// Use Path class to manipulate file and directory paths.
string sourceFile = Path.Combine(sourcePath, fileName);
//string destFile = Path.Combine(Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy") , fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!Directory.Exists("U:\" + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy"))))
{
Directory.CreateDirectory("U:\" + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy")));
}
else
// To copy a file to another location and
// overwrite the destination file if it already exists.
{
foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
try
{
file.CopyTo(e.FullPath.Combine(targetPath + Directory.CreateDirectory("U:\" + DateTime.Now.ToString("dd-MMM-yyyy")), file.Name));
}
catch { }
}
}
}
}
您无法接听许多 Directory.CreateDirectory
电话。只需枚举您的源文件夹文件,然后使用 file.CreationTime
获取日期,调用 Directory.CreateDirectory
(无论它是否已经存在)然后复制您的文件。
string fileName = "*.png";
string sourcePath = @"C:\tmp";
string targetPath = @"U:\";
foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
var targetFolderName = file.CreationTime.ToString("dd-MMM-yyyy");
var dir = Directory.CreateDirectory(Path.Combine(targetPath, targetFolderName));
file.CopyTo(Path.Combine(dir.FullName, file.Name), true);
}