Click here to Skip to main content
11,933,905 members (58,741 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Zip archiving files after a cutoff date, but preserving directory structure

, 14 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A small archiving utility

A basic little console app I threw together. You can simply drag a folder onto it to get it going. Great recursion example...

Uses the Ionic zip library for zip functions. (You will want to save the zip less often if dealing with large files.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
namespace MoveAndArchive
    class Program
        /// <summary>
        /// Application entry point.
        /// </summary>
        /// <param name="args">Arguments array.</param>
        static void Main(string[] args)
                DirectoryInfo root;
                DateTime cutoff;
                ZipFile destZIP = new ZipFile(Directory.GetParent(
                    System.Reflection.Assembly.GetExecutingAssembly().Location) + 
                    @"\" + DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".zip");
                if (args.Length > 0)
                    root = new DirectoryInfo(args[0]);
                    Console.WriteLine("Source Directory: " + root.FullName);
                    Console.Write("Enter Source Directory: ");
                    root = new DirectoryInfo(Console.ReadLine());
                Console.Write("Enter a cutoff (last) date to copy files from (Enter for all): ");
                string cutoffString = Console.ReadLine();
                cutoff = (cutoffString == string.Empty ? DateTime.MaxValue : DateTime.Parse(cutoffString));
                MoveAll(destZIP, cutoff, root, root);
            catch (Exception ex)
                Console.WriteLine("\nERROR: " + ex.Message);
        /// <summary>
        /// Recursively move all files to local ZIP file.
        /// </summary>
        /// <param name="Zip">The ZIP file to write to.</param>
        /// <param name="Cutoff">The last modify-date of files to transfer.</param>
        /// <param name="Root">The root directory being read from.</param>
        /// <param name="Dir">The current directory.</param>
        static void MoveAll(ZipFile Zip, DateTime Cutoff, DirectoryInfo Root, DirectoryInfo Dir)
            string zipPath = Dir.FullName.Replace(Root.FullName, string.Empty);
            if (zipPath != string.Empty)
                Console.WriteLine("Directory: " + zipPath);
            FileInfo[] files = Dir.GetFiles();
            for (int i = 0; i < files.Length; i++)
                if (files[i].LastWriteTime > Cutoff)  continue;
                Console.WriteLine(DateTime.Now.ToString() + " ZIP: " + files[i].FullName);
                Zip.AddFile(files[i].FullName, zipPath == string.Empty ? @"\" : zipPath);
            foreach (DirectoryInfo d in Dir.GetDirectories())  MoveAll(Zip, Cutoff, Root, d);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Useful tip Pin
ProEnggSoft24-Feb-12 21:07
memberProEnggSoft24-Feb-12 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Feb 2012
Article Copyright 2012 by Paladin2000
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid