C#: Get All Files From a Specific Date and Save It To a Folder [closed]

房东的猫 提交于 2019-12-25 18:57:06

问题


Is there a way to get all the files (last modified on a specific date), and save them into a new folder (with the name of the specific date).

Example files:

Inside C:/

Image File     Date Modified

Image001.jpg   11/12/2012
Image002.jpg   11/12/2012
Image003.jpg   11/13/2012
Image004.jpg   11/12/2012

Output should be:

C:/20121112/

Image001.jpg
Image002.jpg
Image004.jpg  

C:/20121113

Image003.jpg

I am currently using the syntax below:

var mydirectory = new DirectoryInfo("C:/");
DateTime from_date = DateTime.Now.AddDays(-1);
DateTime to_date = DateTime.Now;
var files = directory.GetFiles()
  .Where(file=>file.LastWriteTime >= from_date && file.LastWriteTime <= to_date);

But I don't how save to save var files to another folder name.


回答1:


Perhaps:

var files = Directory.EnumerateFiles(path, "*.jpg", SearchOption.TopDirectoryOnly)
                     .Select(fn => new FileInfo(fn));
var fileDateGroups = files.GroupBy(fi => fi.LastWriteTime.Date);
foreach (var dateGroup in fileDateGroups)
{
    string dir = Path.Combine(@"C:\", dateGroup.Key.ToString("yyyyMMdd"));
    Directory.CreateDirectory(dir);
    foreach (var file in dateGroup)
    {
        string newPath = Path.Combine(dir, file.Name);
        File.Copy(file.FullName, newPath, true);
    }
}

Edit: If you want to search for multiple file extensions you need to filter them manually:

var allowed = new[]{ ".png", ".jpg" };
var files = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly)
                     .Where(fn => allowed.Contains(Path.GetExtension(fn)))
                     .Select(fn => new FileInfo(fn));



回答2:


There is definitely a way. Look into Path and Directory classes for iterating over files and directories as well as creating new ones. Some simple googling should be able to turn up how to read in a file's modified date once you're inside a folder and then you just need to queue them up to be written to your new location. Let us know if any of this is confusing for you and try posting some code and showing some effort on your part. Usually people will be a lot nicer and downvote you less :)




回答3:


System.IO.FileInfo which provides methods for creation, deletion, and opening of files may be used for this purpose. To get the date this file was last modified, you may use FileInfo.LastWriteTime which returns a DateTime

Example

string FileName = @"D:\Resources\Image001.jpg"; //Initializes a new string of name FileName as D:\..\Image001.jpg
FileInfo Information = new FileInfo(FileName); //Initializes a new instance of FileInfo to wrap a particular file 
DateTime DateModified = Information.LastWriteTime; //Initializes a new DateTime as Information.LastWriteTime

You may then use System.IO.Directory.CreateDirectory(string path) to create a particular directory given its name as path

Notice: You can not create a directory which contains invalid characters such as /, \, ?, :, *, etc... This means that you can not create a directory of name 11/12/2012

Thanks,
I hope you find this helpful :)



来源:https://stackoverflow.com/questions/13346775/c-get-all-files-from-a-specific-date-and-save-it-to-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!