7z archive include directories matching regex

2

1

I would like to archive directories that match a regular-expression pattern (/[0-9]{4}/). Does 7z support this?

This doesn't find matching directories:

PS> 7z a -t7z C:\Users\<user>\Desktop\Archive.7z '/[0-9]{4}/'

Craig

Posted 2015-10-22T22:23:34.273

Reputation: 785

Answers

3

  1. 7Zip doesn't support regex, only wildcards. Quote from bundled manual:

7-Zip doesn't uses the system wildcard parser. 7-Zip doesn't follow the archaic rule by which . means any file. 7-Zip treats . as matching the name of any file that has an extension. To process all files, you must use a * wildcard.

  1. If you're using PowerShell, you could probably make it work this way:
# Get only objects with names consisting of 4 characters
[array]$Folders = Get-ChildItem -Path '.\' -Filter '????' |
                      # Filter folders matching regex
                      Where-Object {$_.PsIsContainer -and $_.Name -match '[0-9]{4}'} |
                          # Get full paths. Not really needed,
                          # PS is smart enough to expand them, but this way it's more clear
                          Select-Object -ExpandProperty Fullname

# Compress matching folders with 7Zip
& '7z.exe' (@('a', '-t7z', 'C:\Users\<user>\Desktop\Archive.7z') + $Folders)

beatcracker

Posted 2015-10-22T22:23:34.273

Reputation: 2 334

1I'm going to mark your answer as accepted--got me heading in the right direction. Actual syntax: 7z a -t7z C:\Users\<user>\Desktop\Archive.7z ( Get-ChildItem . | Where-Object { $_.Name -Match "^[0-9]{4}" }) – Craig – 2015-10-23T01:24:52.360