Share via


Powershell script to archive files

Question

Monday, April 23, 2018 3:10 PM

I've got the following script which searches a directory for files older than a month and moves these to a directory. It then compresses the directories and creates zip archives of the equivalent directories. This works fine, however i want to adapt it to then add files to existing zip files. So for example if a zip file has been created for March then new files are added for March after the original archive is created it will add these files to the March zip archive. I'm struggling to get this bit completed, any advice?

$Source = "C:\Archive\
$Destination1 = "C:\Archive\archive1\
$Destination2 = "C:\Archive\archive2\
#$datepart = Get-Date (Get-Date).AddMonths(-1) -Format yyyyMM
 
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")

        $files = get-childitem $Source *.* | Where-Object { <#$_.BaseName -match 'ARCH_' -and#>  $_.LastWriteTime -lt (get-date).AddMonths(-1) }

        #Create the destination folder if it does not exist
        if (!(Test-Path $Destination1))
        {
            New-Item $Destination1 -type directory
        }

        foreach ($file in $files) 
        {
            $Directory = $Destination1 + "\ + $file.LastWriteTime.Date.ToString('yyyy - MM - MMM')

            #Create the directory if it does not exist
            if (!(Test-Path $Directory))
            {
                New-Item $directory -type directory
            }

            Move-Item $file.fullname $Directory 
        }

$folders = get-childitem "$Destination1" -Recurse -directory

Foreach ($s in $folders)

 {

  $destination = Join-path -path $Destination2 -ChildPath "$($s.name).zip"

 
                If (Test-Path "$Destination"){
                Write-Host "Archive file already exists"
                exit
            }
            Else {
                # Compress the files
                [System.IO.Compression.ZipFile]::CreateFromDirectory($s.FullName,$Destination) }}

            
                # Remove the temporary folders
              [System.IO.Directory]::GetDirectories("$Destination1") | 
 
foreach { 
 remove-item $_  -Recurse
 } 
            
            

All replies (3)

Tuesday, April 24, 2018 1:37 AM | 1 vote

Hi,

For PowerShell 5.0 and above, you can use Compress-Archive cmdlet with Update parameter to add files to an existing archive.

If you need further help, please feel free to let us know.

Best Regards,
Albert

Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]


Thursday, April 26, 2018 2:02 AM

Hi,

Just checking in to see if the information provided was helpful.

Please let us know if you would like further assistance.

Best Regards,
Albert

Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]


Thursday, April 26, 2018 9:53 AM

Thanks it was useful yes i didnt know about the compress-archive function.

I'm struggling with a feature I want to implement whereby i have 2 directories, the first containing files in folders and the second containing zip files created for months containing files created in that month inside them. I want to update the zip files based on the last write time of the file in directory 1 into the equivalent month zip file in directory 2.