Solved

group files in a directory based on creation date

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now