Solved

group files in a directory based on creation date

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

621 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