I need to zip files and add a timestamp to the zip filename, or I don’t have a backup system (I just have a copy of the latest files whether or not they are correct). This is TOTALLY related to my article on scripting out my SQL table data contents. This is run as a scheduled task – but I had to take a copy of the solution on my LOCAL drive- not a network copy or else the scheduled tasks does not work. See my article to reduce frustrations on Scheduled Tasks.
Using c# – here is the code snibbet (read the whole article though)
[csharp]</pre>
if ((machineName = myargs.Item("scripttables")) != null)
{
DateTime filenameTimeStamp = DateTime.Now;
string fileTSstring = filenameTimeStamp.ToString("yyyyMMss_HHmmsstt");
string startPath = Directory.GetCurrentDirectory()+@"\data";// @".";
string zipPath = @"tablesScripted_" + db.Name + "_" + fileTSstring + ".zip";
Console.WriteLine("Completed ONLY scripting the tables and making the archive called ‘" + zipPath + "’");
System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipPath);
return;
}
<pre>[/csharp]
Before you use this code
- Microsoft has an easy example (read note about the reference – next point below)
You have to reference the following TWO (2!!!!!) dlls and put the “using System.IO.Compression” at the top in this directory. (I did say TWO dll’s right?)
- ~/Windows/Microsoft.NET/assembly/GAC_MSIL/System.IO.Compression.FileSystem/(Your .NET Version)/System.IO.Compression.FileSystem.dll
- ~/Windows/Microsoft.NET/assembly/GAC_MSIL/System.IO.Compression/(Your .NET Version)/System.IO.Compression.dll
- get the string pattern (great simple reference article) so we have a date like year, month, date hour, minute, second
- put ‘er all together, shake and stir – you are done