Hi,
I need a script to run every day and zip the folder that was created the previous day.
I then need the zipped version to be moved to a network location.
I was thinking of using 7zip to do the zipping as this is installed on the servers but I’m open to alternatives if there is a better way\method\tool.
Any help will be greatly appreciated.
Here are some additional details.
The folders to zip are named in this format:
20171228
20171229
20171230
I need to move the zipped version from
D:\usr\sap\gkretail\ucon\dataexchange\export_channel\results\export\EXPORT_SUCCESS
To
SOL000483\domain.com\GK_Transaction_Archive
As this is not a free script shop here you would have to write a script by yourself. But if I’m not wrong you don’t even need a sophisticated complex script for that. A proper command line for 7zip should be enough. Addditional information you can find here: 7Zip Command Line Syntax.
If you have a question to a Powershell script you wrote you can post here the relevant part of the script we will try to help you as good as we’re able to.
As for a “better way/method/tool” to 7zip, your requirements don’t really scream out “I need to use 7zip”. Can you try using the Compress-Archive cmdlet?
Something like this should do the job:
You need a minimum version of PowerShell 3, so you are probably running 2 or lower. So back to using 7zip. Just adjust your $yesterdaysfolder to the format that I specified above.
If I’m not wrong Compress-Archive has been released with version 5. So either you update to a current version or you use your 7zip command line instead.
No matter what version of PoSh you are using. You can compress with PoSH and .Net.
So taken your block and tweaking it a bit – Try this approach, nothing extra needed
Setup source and destination and criteria
Clear-Host
$Source = 'D:\Temp'
$Destination = 'D:\Temp\ArchiveDestination'
$ArchiveDate = (Get-Date).AddDays(-1)
"`nShowing archive source folders"
Get-ChildItem -Path $Source -Directory -Recurse | Select Name,LastWriteTime | Select -First 9
"`nCount of current archives to date of the source folders: " + (Get-ChildItem -Path $Destination -Filter '*.zip').Count + "`n"
Instantiate compression class
Validate ArchiveDate, compress and archive if true, else take no actions
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
ForEach ($SourceFolder in (Get-ChildItem -Path $Source -Directory))
{
If ($SourceFolder.LastWriteTime -lt $ArchiveDate)
{
"The folder named – $($SourceFolder.Name) – matches archive date. Archiving Now."
[IO.Compression.Zipfile]::CreateFromDirectory($SourceFolder.FullName, "$Destination\$($SourceFolder.Name).zip")
}
Else {Write-Warning -Message "$($SourceFolder.Name) checked does not met archive date. No actions taken."}
}
"`nCount of current and new archives: " +
(Get-ChildItem -Path $Destination -Filter '*.zip').Count
Results
Showing archive source folders
WARNING: ArchiveDestination checked does not met archive date. No actions taken.
WARNING: ArchiveSource checked does not met archive date. No actions taken.
Name LastWriteTime
—- ————-
ArchiveDestination Thu 04 Jan 02018 14:28:21
ArchiveSource Thu 04 Jan 02018 13:40:01
diff Mon 01 Jan 02018 15:34:13
Downloads Fri 18 Aug 02017 18:55:59
Duplicates Mon 06 Feb 02017 22:17:04
JPG Tue 26 Dec 02017 18:55:26
LRV Tue 26 Dec 02017 18:57:49
MoveItems Mon 02 Oct 02017 23:25:34
test Mon 01 May 02017 00:02:30
Count of current archives to date of the source folders: 0
The folder named – diff – matches archive date. Archiving Now.
The folder named – Downloads – matches archive date. Archiving Now.
The folder named – Duplicates – matches archive date. Archiving Now.
The folder named – JPG – matches archive date. Archiving Now.
The folder named – LRV – matches archive date. Archiving Now.
The folder named – MoveItems – matches archive date. Archiving Now.
The folder named – test – matches archive date. Archiving Now.
The folder named – THM – matches archive date. Archiving Now.
Count of current and new archives: 8
@postanote,
Thanks a lot for helping with this.
It works very well.
There is one snag.
I only need it to zip yesterdays data. this seems to all the data in the source again.
What part of the code can I change to only zip yesterdays daya.
I intend to set this as a scheduled task to run early morning to archive sales data from only the previous day.
Sorry if that sounds rude for you but don’t you think you could put a little effort by yourself? And Jeremy even answered this already. The code you get here does not have a copyright - you’re welcome to change it to your needs. Feel free to play a little with it and learn something while you do so - that’s how we all do it. And of course you can always ask your prefered internet search engine to help you out.
Hi all,
I have managed to put together a working script now and wanted to thank all of you guys for your time and assistance.
I the spirit of sharing I thought I would post it here in case it helps somebody else.