Author Posts

January 4, 2018 at 9:09 am

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

Thanks In advance for any help
Sohail

January 4, 2018 at 9:28 am

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. 😉

January 4, 2018 at 10:02 am

HI OLaf,
I don't think just the command line for 7zip is enough.

To zip the folder using 7zip command line is just

7za a -tzip 20171227.7zip 20171227

This is not my issue.
I don't know how to target the folder dated from the previous days date.

January 4, 2018 at 11:21 am

Okay so I've managed to coble this script together however it's failing at my $yesterdaysfolder variable

$pathTo7zip = "C:\Users\gustasoh\Desktop\7z1701-extra\x64\7za.exe"
$sourcefolder = "C:\Users\gustasoh\Desktop\7z1701-extra\x64"
$yesterdaysfolder = "(Get-Date).AddDays(-1).ToString('yyyyMMdd')"
$Destination1 = "C:\Users\gustasoh\Desktop\ZIPPED\"


C:\Users\gustasoh\Desktop\7z1701-extra\x64\7za.exe a -tzip *.7zip $sourcefolder\$yesterdaysfolder

#Check destination path
if (Test-Path $Destination1)
 {
  #then copy
  Copy-Item $Source *.7zip $Destination1
 }

January 4, 2018 at 1:52 pm

$yesterdaysfolder should be like this:

$yesterdaysfolder = "$((Get-Date).AddDays(-1).ToString('yyyyMMdd'))"

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:

$sourceFolder = "Insert\Source\Folder\path\here"
$Date = "$((Get-Date).AddDays(-1).ToString('yyyyMMdd'))"
$archivePath = "D:\usr\sap\gkretail\ucon\dataexchange\export_channel\results\export\EXPORT_SUCCESS"
$archivePath2 = "\\SOL000483\domain.com\GK_Transaction_Archive"


Get-ChildItem "$sourcefolder\$Date" -Recurse | Compress-Archive -DestinationPath "$archivePath\$Date.zip"

if (Test-Path "$archivePath\$Date.zip")
 {
  Copy-Item -Path "$archivePath\$Date.zip" -Destination "$archivePath2\$Date.zip" 
 }

Just fill out the $sourceFolder variable: the path to the $Date folders.

January 4, 2018 at 2:01 pm

And looking at this after my first sip of coffee, I realized that your Get-Date was already converting it to a string, so the quotes weren't needed.

$yesterdaysfolder = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
$Date = (Get-Date).AddDays(-1).ToString('yyyyMMdd')

January 4, 2018 at 2:23 pm

Hi Jeremy,
Thanks for the help.
I have tried the mods you suggested and I get this error.

Compress-Archive : The term 'Compress-Archive' is not recognized as the name of a cmdlet,

January 4, 2018 at 3:00 pm

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.

January 4, 2018 at 3:15 pm

This is what I'm using

PS C:\Windows\system32> $PSVersionTable.PSVersion
Major Minor Build Revision
—– —– —– ——–
4 0 -1 -1
==================================================
PS C:\Windows\system32> get-host

Name : Windows PowerShell ISE Host
Version : 4.0
InstanceId : dc6b8173-9809-4383-9df7-43d55b5482b9
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-GB
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace

I'm running version 4

January 4, 2018 at 3:33 pm

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.

January 4, 2018 at 10:35 pm

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

January 5, 2018 at 11:38 am

@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.

January 5, 2018 at 12:11 pm

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.

January 5, 2018 at 12:18 pm

@Olaf,
No that's not rude Olaf.
I understand but I'm just not that good when it comes to scripting.

I'll have a further play and see where I get.

Thanks for all the help all you guys have given me here.

January 10, 2018 at 12:19 pm

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.

Add-Type -AssemblyName 'System.IO.Compression.FileSystem'

$Source = 'C:\7z1701-extra\x64'
$Destination = 'C:\7z1701-extra\x64\ZIPPED'

$d = (get-date).adddays(-1).tostring('yyyyMMdd')
$SourceFolder = Join-Path -Path $Source -ChildPath $d

Write-Verbose "Archiving $SourceFolder"
[IO.Compression.Zipfile]::CreateFromDirectory($SourceFolder, "$Destination\$d.zip")

Move-Item -Path $Destination\*.zip -Destination \\server.domain.cloud\GK_Transaction_Archive

Thanks everyone