views:

354

answers:

3

IT has been tasked with reducing the file-server usage rate so I'd like to do my part my compressing old files(ie Excel/Access/Txt).

We have some folders that contain thousands of files so I don't want to just zip the whole directory into one large file - it would be preferrable to have a number of smaller files so that it would be easier for a user to find the data 'bucket' they are looking for.

Is there a way using C# to read through a directory and zip the files into year-month groups (all files from year-month placed together in one zip)?

Or would it be better to use a script like AutoIT?

Or are there programs already existing to do this so I don't have to code anything?

+2  A: 

Surely you can do this with a bit of C# and libraries like 7-zip or SharpZipLib.

Peter Lillevold
+2  A: 

You could use System.IO.Directory.GetFiles() to loop through the files on each directory, parsing out by file name and adding them to a 7-zip or SharpZipLib object. If it's thousands of files it might be best to throw it in a service or some kind of scheduled task to run overnight so as not to tax the fileshare.

Good luck to you !

EDIT: As an addendum you could use a System.IO.FileInfo object for each file if you need to parse by created date or other file attirbutes.

Scott Vercuski
+1 It could simply be a low priority thread with couple of Sleeps every now and then to keep the disk I/O low. I would (personally) prefer coding this instead of using a 3rd party solution as it is a nice C# exercise (which shouldn't take you more than 15 min once you have downloaded SharpZipLib). :)
Groo
+3  A: 

Im not sure if your question is about zipping, selecting files from particular year/month or both.

About zipping Peter already mentioned 7-zip and SharpZipLib. I have personally only experience with the latter but its all positive, easy to work with.

About grouping your files it could be done by iterating all the files in the folder and group them by either there created date or last modified date.

pseudo:

var files = new Dictionary<DateTime, IList<string>>();
foreach (var file in Directory.GetFiles(...)) {
   var fi = new FileInfo(file);
   var date = fi.CreatedDate();
   var groupDate = new DateTime(date.Year, date.Month);

   if (!files.ContainsKey(groupDate)) files.Add(groupDate, new Collection<string>());

   files[groupDate].Add(file);
}

now your should have a dictionary containing distinct year/month keys and foreach key a list of files belonging to that group. So for zipping

pseudo:

foreach (var entry in files) {
   var date = entry.Key;
   var list = entry.Value;

   // create zip-file named date.ToString();
   foreach (var file in list) {
      // add file to zip
   } 
}
BurningIce