I'm trying to zip up log using DotNetZip and powershell. The files are in C:\user\temp\logs When I loop through the logs in the directory and add them to the zip file, I end up with the folder hierarchy and the log files when I only want the log files.
So the zip ends up containing:
-user
└temp
└logs
└log1.log
log2.log
log3.log
When I want the zip file to contain is:
log1.log
log2.log
log3.log
Here is the script I'm running to test with:
[System.Reflection.Assembly]::LoadFrom("c:\\\User\\bin\\Ionic.Zip.dll");
$zipfile = new-object Ionic.Zip.ZipFile("C:\user\temp\logs\TestZIP.zip");
$directory = "C:\user\temp\logs\"
$children = get-childitem -path $directory
foreach ($o in $children)
{
if($o.Name.EndsWith(".log")){
$e = $zipfile.AddFile($o.FullName)
}
}
$zipfile.Save()
$zipfile.Dispose()
There is an AddFile where you can override the filename in the archive:
Try this:
It is also possible that this does what you want:
Not tested, but I think this should work.