Solved

group files in a directory based on creation date

Posted on 2009-04-06
3
188 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Release Dynamically Allocated Memory in C# 3 37
VB.Net. Reading xml value 6 29
jquery tab header text 1 22
VB.net Duplicating a table - primary key not created 3 31
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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