Zip/compress files modified in the last day

2.1k views Asked by At

I am really new to Powershell and scripting and would really appreciate if anyone is able to help.

I am trying to create a PowerShell script which does the following to backup any changes on a daily basis in a set number of directories.

  • Recursively find any new/modified files or directories from multiple preset locations only for the last 24 hours.

  • Compress all the resulting entries in a zip file

  • while keeping the folder structures

I have managed to find get the first bit working using the following script:

Get-ChildItem $SRC | Where {$_.PsIsContainer} | foreach-object {
  Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue | 
  Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} |
  Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode
} >> C:\temp\LastModified-$TimeStamp.log

I have another script which zips up a given directory but I have been struggling to use the results from the script above to either robocopy the files to a location which I can then Zip or if there is a way to create a zip file without having to copy the files somewhere first without loosing the folder structure for each file?

I have the following script which can Zip a given directory (but I need to zip multiple directories into one zip file unless I robocopy the files somewhere first & how do I get robocopy to only copy only the files modified in the last 24 hours)

$Source =  'C:\Users\Downloads\RobocopyLocation\'
$Destination = 'D:\OneDrive\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '

Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($Source, $ZipName)

When I run the Script below I get and error message complaining about the path argument being null.

$F1 =  'C:\Users\Downloads\Folder A'
$F2 =  'C:\Users\Downloads\Folder B'
$F3 =  'C:\Users\Downloads\Folder C'
$SrcFolders = ($F1, $F2, $F3); 
$BackupDrive = "D:\OneDrive"
$DestFolders = ("$BackupDrive\Folder A", "$BackupDrive\Folder B", "$BackupDrive\Folder C"); 
$Destination = 'C:\Users\SysAdmin\Downloads\PLSWork\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
$Option = "{$_.PsIsContainer} | foreach-object { Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue | Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} | Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode}"


Foreach ($SrcFolders in (Get-ChildItem $SrcFolders | where $Option)){


Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($SrcFolders, $ZipName)
}
2

There are 2 answers

2
Esperento57 On

modify your line

$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '

with

$ZipName = "$Destination$TimeStamp" + '_Backup.zip '
0
dbso On

How about something like this?

$RootPath = "P:\PSZip\"
$Folders = (
    "Folder A",
    "Folder B",
    "Folder C"
)
$BackupFolder = "P:\PSZip\Destination\"
$Files = @()

foreach($Folder in $Folders) { 
    $Files = Get-ChildItem ("{0}{1}\" -f $RootPath, $Folder) | Where {$_.PSIsContainer} | ForEach-Object { Get-ChildItem $_.FullName -Recurse | Where { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } } 
    $ZipName = ("{0}{1}\{2}_Backup.zip" -f $BackupFolder, $Folder, (Get-Date -f yyyyMMdd_HHmmss))
    Compress-Archive -Path $Files.FullName -DestinationPath $ZipName
}