C#으로 만든 파일 일괄처리 스크립트 c#

서버에 저장된 수백 개의 로그 파일들을 7zip으로 압축할 일이 생겼다. 그런데 문제는 파일들을 통째로 하나의 파일에 압축하는 것이 아니라 하나당 하나씩 압축파일을 생성해야 한다는 것이다. 이를테면 20091111.log, 20091112.log 가 있으면 20091111.7z, 20091112.7z 두 개의 파일이 나와야 한다.

7zip에는 그러한 옵션이 없었다. 만약 bash 환경이었다거나 cygwin이 깔려있거나 필자가 wsh를 밥먹듯 다루는 관리자였더라면 식은죽 먹기였겠지만 그렇지 못했다. 예전에 wsh로 잘 짜놓고 위키에 적어서 정리한 스크립트가 어딘가 있기는 한데 다시 건드리고 싶지는 않다. 별로 잘 쓰지도 않을 코드를 이해하는데 뭐하러 시간을 더 쏟겠는가? 그게 lisp이라도 된다면 즐겁게 배우겠지만 (...)

마침 c#으로 먹고사는 처지고 하여 되는대로 매우 간단한 스크립트를 하나 만들었다. 솔직히 부끄럽다. 내 손으로 짰다기보다 앞발로 짰다는 것이 맞을 듯 싶다. 누군가에게는 도움이 되길.

using System;
using System.IO;
using System.Diagnostics;

namespace _7ZipScript
{
    /// <summary>
    /// 현재 폴더에 있는 모든 .log 파일들을 압축한다.
    /// </summary>
    class Program
    {
        private static readonly string VERSION = "1.0";
        private static readonly string AUTHOR = "mmx900 (manalith.org)";
        private static readonly string zipLocation = @"C:\Program Files\7-Zip\7z.exe";
        private static readonly string targetExtension = ".log";

        static void Main(string[] args)
        {
            Console.WriteLine("파일 압축기 " + VERSION);
            Console.WriteLine(AUTHOR + "\n");

            string workingDirectory = Directory.GetCurrentDirectory();

            Process process;

            foreach (FileInfo f in new DirectoryInfo(workingDirectory).GetFiles())
            {
                if (targetExtension == f.Extension)
                {
                    Console.WriteLine(f.Name + "을 압축합니다.");

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.FileName = zipLocation;
                    startInfo.Arguments = "a " + Path.GetFileNameWithoutExtension(f.Name) + ".7z " + f.Name;
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                    process = Process.Start(startInfo);
                    process.WaitForExit();
                }
                else
                {
                    Console.WriteLine(f.Name + "는 처리 대상이 아닙니다.");
                }
            }
        }
    }
}

그나저나 왜 아래의 (실수로 생성한) 인용문은 안 지워지는 것이냐? textile의 버그이냐? 귀찮아서 둔다.

Tag :
, , ,

Leave Comments