Reputation: 494
I have lots of logs in a folder, I would like to only grab files that have todays date and put them in the zip file.
Here is my code:
static void Main(string[] args)
{
//Specify todays date
DateTime todaysDate = DateTime.Today;
//Create a zip file with the name logs + todays date
string zipPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd") + ".zip";
string myPath = @"C:\Users\Desktop\LOG SEARCH";
var files = System.IO.Directory.GetFiles(myPath, "*" + todaysDate.ToString("yyyyMMdd") + "*");
foreach (var file in files)
{
Console.WriteLine(file);
}
}
How do I zip files
?
Upvotes: 2
Views: 2403
Reputation: 2923
So what you can do is create a temporary folder and then add each file that matches the current date in it. Once that's done you can ZipFile.CreateFromDirectory
and then delete the temporary folder
DateTime todaysDate = DateTime.Today;
//Create a zip file with the name logs + todays date
string zipPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd") + ".zip";
string myPath = @"C:\Users\Desktop\LOG SEARCH";
string tempPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd");
var files = System.IO.Directory.GetFiles(myPath, "*" + todaysDate.ToString("yyyyMMdd") + "*");
Directory.CreateDirectory(tempPath);
foreach (var file in files)
{
File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file));
}
ZipFile.CreateFromDirectory(tempPath, zipPath);
Directory.Delete(tempPath, true);
Upvotes: 3
Reputation: 73
You need to use System.IO.Compression; and use
ZipFile.CreateFromDirectory(myPath, zipPath);
Upvotes: 0