0

I have a list of logfiles which have their creation-date in their filename. I'm trying to zip the files together by date. Obviously I'm doing something wrong. How can I achieve this? This program is going to be a scheduled executable that zip log files every day, hence the currentDate variable. It could be 1 or several logfiles so the program should not depend on a number of files to zip.

These are the names of the log files:
Log_2021-06-17_1.txt,
Log_2021-06-17_2.txt,
Log_2021-06-17_3.txt

string currentDate = DateTime.Today.ToString("yyyy-MM-dd");
            var logDir = new DirectoryInfo(@"C:\Users\user\Desktop\LogFilesTEST\In\");
            foreach (FileInfo logFile in logDir.GetFiles())
            {
                if (logFile.Name.Contains(currentDate))
                {
                    string startPath = @"C:\Users\user\Desktop\LogFilesTEST\In\" + logFile.Name;
                    string zipPath = @"C:\Users\user\Desktop\LogFilesTEST\Archive\" + logFile.Name + ".zip";

                    ZipFile.CreateFromDirectory(startPath, zipPath);
                }              
            }

Edit: if someone has the same problem here is what i did in my Console application:

Program Class

 class Program
    {
        static void Main(string[] args)
        {
            string currentDate = DateTime.Today.ToString("yyyy-MM-dd");
            var logDir = new DirectoryInfo(@"C:\Users\user\Desktop\LogFilesTEST\In\");
            List<FileInfo> LogFileList = new List<FileInfo>();
            foreach (FileInfo logFile in logDir.GetFiles())
            {
                if (logFile.Name.Contains(currentDate))
                {
                    LogFileList.Add(logFile);
                }              
            }

            Zipper.CreateZipFile(LogFileList, @"C:\Users\user\Desktop\LogFilesTEST\Archive\Logs_" + currentDate + ".zip");

            foreach(var logfile in LogFileList)
            {
                logfile.Delete();
            }

        }
    }

Zipper Class:

class Zipper
    {       
        public static void CreateZipFile(List<FileInfo> files, string archiveName)
        {
            using (var stream = File.OpenWrite(archiveName))
            using (ZipArchive archive = new ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
            {
                foreach (var item in files)
                {
                    archive.CreateEntryFromFile(item.FullName, item.Name, CompressionLevel.Optimal);
                }
            }
        }
    }
WhoAmI
  • 1,188
  • 6
  • 17
  • 47
  • 1
    Get list of files [using mask](https://stackoverflow.com/q/1584711/1997232) and [add each to zip](https://stackoverflow.com/a/27971816/1997232). – Sinatr Jun 17 '21 at 12:55
  • 1
    `ZipFile.CreateFromDirectory` is to create a zip from all the files in a directory. Use [ZipArchive](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view=net-5.0) instead. Also, it's better to use `Path.Combine` instead of creating paths by appending. – Magnetron Jun 17 '21 at 12:57
  • 1
    Does this answer your question? [How to zip multiple files using only .net api in c#](https://stackoverflow.com/questions/1243929/how-to-zip-multiple-files-using-only-net-api-in-c-sharp) – Barns Jun 17 '21 at 13:19
  • @Magnetron - Thank you, this explains the problem i'm currently having in the output. – WhoAmI Jun 18 '21 at 10:58
  • @Barns Perhaps, I'll check it out. Thanks – WhoAmI Jun 18 '21 at 10:59
  • @Barns . Yes this solved my problem. Post your link as an answer and i'll mark this question as answered – WhoAmI Jun 18 '21 at 13:46
  • Just go up vote the other answer (if you have not already) that way it will become more visible to others. Glad the suggestion helped you along. Happy Coding! – Barns Jun 18 '21 at 15:18

0 Answers0