0

I've the following task... I want to archive files with an Powershell script that have an specifice creation/edit date. Therefore I started to get a list of these files with the following statemant:

Get-ChildItem -Path C:\Data\... -Recurse | Where-Object { $_.LastWriteTime.Date -gt '2021.01.01' }

This seems to work correctly as I only get the requiered files listed. If I expand the statment to

Get-ChildItem -Path C:\DATA\... -Recurse | Where-Object { $_.LastWriteTime.Date -gt '2021.01.01' } | Compress-Archive -DestinationPath C:\Data\Delta\Archive.zip

the Files that are archived are doubled in the ZIP file. One is the correct set of Files and than all files (also those that are older than the specified date) are added to the archive again.

Can someone tell me what I'm missing?

Thanks in advance

Greatings

Alex

scarabeaus
  • 13
  • 7
  • Same’s like this behavior is normal, check this https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-7.1#example-8--how-recursion-can-affect-archives out . Maybe it will work when you ignore the folders (if you don’t need the folder structure), or if you first create a temp folder and then compress this full folder? – guiwhatsthat Nov 02 '21 at 10:14
  • Actually I do need the Folder structure. I thought about to create a Temp folder, but this my be tricky as there may be thousend of small files and copying the files first do take some time sometimes so I would like to avoid that... – scarabeaus Nov 02 '21 at 12:39
  • See [this answer](https://stackoverflow.com/questions/70171826/archive-folder-without-some-subfolders-and-files-using-powershell/70173927#70173927) for an example of using [system.io.compression.zipfileextensions.createentryfromfile](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfileextensions.createentryfromfile?view=net-6.0) to archive with stored paths. – Rich Moss Jan 10 '22 at 20:19

1 Answers1

0

If you want files that have that exact modified date, you need to change operator -gt in the Where-Object clause to -eq.

Also, you should always compare DateTime's to another DateTime object to make sure the string you put in now ('2021.01.01') is converted to a DateTime object properly (that depends very much on your systems culture..)

Then, since you are looking for files in path C:\DATA and you also create the zip file in that same root path, it is advisable to put brackets around Get-ChildItem to have the collecting of files finish before taking further action on the files. If you don't, the new zip file will also be enumerated.

Try

$refDate = (Get-Date -Year 2021 -Month 1 -Day 1).Date
# filter the files that have an specific last modified date
(Get-ChildItem -Path 'C:\DATA\...' -File -Recurse | Where-Object { $_.LastWriteTime.Date -eq $refDate }) | 
Compress-Archive -DestinationPath 'C:\Data\Delta\Archive.zip'

Or

$refDate = (Get-Date -Year 2021 -Month 1 -Day 1).Date
# filter the files that have an specific last modified date
$files = Get-ChildItem -Path 'C:\DATA\...' -File -Recurse | Where-Object { $_.LastWriteTime.Date -eq $refDate }
$files | Compress-Archive -DestinationPath 'C:\Data\Delta\Archive.zip'
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Hi, this seem to work. At least I have now only the required Files in that archive. How can I preserve the folder structure as well? – scarabeaus Nov 02 '21 at 12:36
  • @scarabeaus I'm afraid it's one or the other with Compress-Archive.. You can either filter the files that have that specific date and zip these, OR compress the folder structure including **all** files in them. If that is what you need, I'd suggest first copying all folders with only the files that match the LastWriteTime to a temporary filepath **outside** the rootfolder. Then do `Get-ChildItem -Path $rootFolder -Directory -Recurse | Compress-Archive -DestinationPath 'C:\Data\Delta\Archive.zip'` where `$rootFolder` is the folder where the copies are. After that, remove the temporary folder. – Theo Nov 02 '21 at 14:11