Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

group files in a directory based on creation date

Posted on 2009-04-06
3
Medium Priority
?
196 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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