如何根据上次修改日期月份的文件列表创建文件夹 c#
How to create folders from a list of files by last modified date month wise c#
我想从文件列表中按月创建文件夹。
我试过下面的代码。
var files = directory.GetFiles()
.Where(file => file.LastWriteTime.Month == date.Month -1);
//create folder for the files (With MonthName)
var year = files.Select(j => j.LastWriteTime.Year).FirstOrDefault();
var month = files.Select(j => j.LastWriteTime.Month).FirstOrDefault();
var newFolderPath = year.ToString() + month.ToString();
var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);
foreach (var f in files)
{
// var destFile = System.IO.Path.Combine(directory, destinationDirec);
var path = Path.Combine(destinationDirec.FullName, f.Name);
if (!File.Exists(path))
{
System.IO.File.Move(f.FullName, path);
}
}
以上代码给出了上个月的文件列表。但我想为早于当月的文件创建文件夹。
谁能给我一个解决方案?
您可以试试这个代码。也许有一些变化。
//Group files by month. Later you can skip some groups if needed
var fileGroups = directory.GetFiles()
.GroupBy(file => file.LastWriteTime.Month);
foreach (var fileList in fileGroups)
{
var year = fileList.First().LastWriteTime.Year;
var month = fileList.First().LastWriteTime.Month;
var newFolderPath = year.ToString() + month.ToString();
var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);
//move files
foreach (var file in fileList)
{
var path = Path.Combine(destinationDirec.FullName, file.Name);
if (!File.Exists(path))
{
System.IO.File.Move(file.FullName, path);
}
}
}
如果您有很多不同年份的文件,也许值得修改 GroupBy
条件。
例如你可以使用这个条件:
GroupBy(file => (397 * file.LastWriteTime.Year) ^ file.LastWriteTime.Month)
这应该会有所帮助,根据需要构建 fullNewDir 值。
String fullSourceDir = "G:\Tmp\Test";
foreach (var fullFileName in Directory.EnumerateFiles(fullSourceDir)){
DateTime lastWriteTime = File.GetLastWriteTime(Path.Combine(fullSourceDir, fullFileName));
String fullNewDir = Path.Combine(fullSourceDir, lastWriteTime.ToString("yyyy-MM-dd_HH-mm"));
if (!Directory.Exists(fullNewDir)){
Directory.CreateDirectory(fullNewDir);
}
String fileName = Path.GetFileName(fullFileName);
System.IO.File.Move(fullFileName, Path.Combine(fullNewDir, fileName));
}
我想从文件列表中按月创建文件夹。
我试过下面的代码。
var files = directory.GetFiles()
.Where(file => file.LastWriteTime.Month == date.Month -1);
//create folder for the files (With MonthName)
var year = files.Select(j => j.LastWriteTime.Year).FirstOrDefault();
var month = files.Select(j => j.LastWriteTime.Month).FirstOrDefault();
var newFolderPath = year.ToString() + month.ToString();
var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);
foreach (var f in files)
{
// var destFile = System.IO.Path.Combine(directory, destinationDirec);
var path = Path.Combine(destinationDirec.FullName, f.Name);
if (!File.Exists(path))
{
System.IO.File.Move(f.FullName, path);
}
}
以上代码给出了上个月的文件列表。但我想为早于当月的文件创建文件夹。 谁能给我一个解决方案?
您可以试试这个代码。也许有一些变化。
//Group files by month. Later you can skip some groups if needed
var fileGroups = directory.GetFiles()
.GroupBy(file => file.LastWriteTime.Month);
foreach (var fileList in fileGroups)
{
var year = fileList.First().LastWriteTime.Year;
var month = fileList.First().LastWriteTime.Month;
var newFolderPath = year.ToString() + month.ToString();
var destinationDirec = System.IO.Directory.CreateDirectory(directory + newFolderPath);
//move files
foreach (var file in fileList)
{
var path = Path.Combine(destinationDirec.FullName, file.Name);
if (!File.Exists(path))
{
System.IO.File.Move(file.FullName, path);
}
}
}
如果您有很多不同年份的文件,也许值得修改 GroupBy
条件。
例如你可以使用这个条件:
GroupBy(file => (397 * file.LastWriteTime.Year) ^ file.LastWriteTime.Month)
这应该会有所帮助,根据需要构建 fullNewDir 值。
String fullSourceDir = "G:\Tmp\Test";
foreach (var fullFileName in Directory.EnumerateFiles(fullSourceDir)){
DateTime lastWriteTime = File.GetLastWriteTime(Path.Combine(fullSourceDir, fullFileName));
String fullNewDir = Path.Combine(fullSourceDir, lastWriteTime.ToString("yyyy-MM-dd_HH-mm"));
if (!Directory.Exists(fullNewDir)){
Directory.CreateDirectory(fullNewDir);
}
String fileName = Path.GetFileName(fullFileName);
System.IO.File.Move(fullFileName, Path.Combine(fullNewDir, fileName));
}