Solved

group files in a directory based on creation date

Posted on 2009-04-06
3
190 Views
Last Modified: 2012-05-06
I' am doing a project (as a console application in Vb)

I have about 12 folders (for each month of an year) e.g. 2006-09 (sep).
Each folder contain about 20,000 xml files.

1. Now I want to group these files by their creation date. e.g. all files created on the 2006-09-20 belong to one group, files created on  2006-09-21 belong to another group and so on.

2. Then I want to compress and zip these collections (one by one) , so a zip file would look like ..\2006-09 (sep)\2006-09-20.zip

3. Then delete  all the files (for whom a zip has been created).
0
Comment
Question by:manivineet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24083592
Where is your question?
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 500 total points
ID: 24083909
Ok.. I got poc for you should work:
RootDirectory is HardCoded, you should be able tweak as per ur requirements.

using System.Linq;
using ICSharpCode.SharpZipLib.Zip;

namespace ConsoleApplication4
{
    internal class Program
    {
        private static int bytesRead;

        private static void Main(string[] args)
        {
            var dir = new DirectoryInfo("c:\\RootFolder");
            DirectoryInfo[] directories = dir.GetDirectories();
            foreach (DirectoryInfo directory in directories)
            {
                IEnumerable<FileInfo> fileList = directory.GetFiles();
                IOrderedEnumerable<IGrouping<string, FileInfo>> queryGroupByCreationDate =
                    from file in fileList
                    group file by file.CreationTime.ToShortDateString()
                    into fileGroup
                        orderby fileGroup.Key
                        select fileGroup;

                foreach (var infos in queryGroupByCreationDate)
                {
                    var name = infos.ElementAt(0).DirectoryName;

                    DateTime time = Convert.ToDateTime(infos.Key);
                    var zipFileName = time.Year +"-" +time.Month +"-"+ time.Day;

                    FileStream fZip = File.Create(name + "\\" + zipFileName+".zip");
                    ZipOutputStream zipOutputStream = new ZipOutputStream(fZip);

                    foreach (var info in infos)
                    {
                        ZipEntry entry = new ZipEntry(info.Name);
                        zipOutputStream.PutNextEntry(entry);
                        FileStream fs = File.OpenRead(info.FullName);
                        try
                        {
                            var transferBuffer = new byte[1024];
                            do
                            {
                                bytesRead = fs.Read(transferBuffer, 0, transferBuffer.Length);
                                zipOutputStream.Write(transferBuffer,0,bytesRead);

                            } while (bytesRead>0);
                        }
                        finally
                        {
                            fs.Close();
                        }
                    }
                    zipOutputStream.Finish();
                    zipOutputStream.Close();

              }

            }

            foreach (var directory in directories)
            {
                var files = directory.GetFiles("*.xml");
                foreach (var file in files)
                {
                    File.Delete(file.FullName);
                }
            }
            Console.ReadKey();
        }
    }
}

Note: you need to have ICSharpCode.SharpZipLib.dll
Can get it from : http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx 
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24084073
Shouldn't be a problem to convert to VB
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question