0

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.

abramlimpin
  • 5,027
  • 11
  • 58
  • 97
  • 6
    `Is there a way` Translation: Write it for me – L.B Nov 12 '12 at 15:44
  • Break the problem down into smaller steps. If you are stuck on a step, ask a more specific question and include the code that you have tried. – Jon B Nov 12 '12 at 15:44
  • 1
    I think it is a fair question. He didn't ask "how do you do it?" He asked, "is it there a way?" (i.e. is it possible). No point in trying to figure it out if it isn't possible. – davids Nov 12 '12 at 15:46
  • 1
    @davids then I give the answer: Yes it is possible. – L.B Nov 12 '12 at 15:47
  • @L.B. You should have put it in the answer section so it could be marked as the answer to the question. – davids Nov 12 '12 at 15:48
  • 1
    System.IO holds the magic baby! http://msdn.microsoft.com/en-us/library/system.io.aspx – Hardrada Nov 12 '12 at 15:52

3 Answers3

7

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));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

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 :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
1

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 :)

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • Noted. I noticed that I wasn't able to include the codes when I submitted it. Only took 5 minutes to receive so many downvotes. Oh well. – abramlimpin Nov 12 '12 at 15:53