Filter Files by Date Using 7-Zip Command Line

7-Zip does not feature a dedicated native command-line switch for filtering files by date ranges during archive creation. To accomplish date-based filtering, you must combine 7-Zip’s @listfile parameter with shell commands—such as Windows PowerShell, Command Prompt's forfiles, or Linux's find—to identify files matching your desired date range and pass them directly to the 7z executable.


Understanding the 7-Zip Listfile Parameter

Because 7-Zip lacks built-in switches like before or after for timestamps, it relies on an external list of files.

The syntax to compress files from a pre-filtered list is:

7z a archive.7z @listfile.txt

The @ symbol instructs 7-Zip to read file paths line by line from listfile.txt and compress only those entries.


PowerShell provides precise control over date ranges by inspecting file metadata (LastWriteTime).

One-Line Command

Get-ChildItem -Path "C:\SourceFolder" -File | Where-Object { $_.LastWriteTime -ge (Get-Date "2023-01-01") -and $_.LastWriteTime -le (Get-Date "2023-12-31") } | ForEach-Object { $_.FullName } | Out-File -FilePath ".\filelist.txt" -Encoding utf8; & "C:\Program Files\7-Zip\7z.exe" a -t7z "archive.7z" "@filelist.txt"; Remove-Item ".\filelist.txt"

Step-by-Step Breakdown

  1. Find files within range:
    $files = Get-ChildItem -Path "C:\SourceFolder" -File -Recurse | Where-Object {
        $_.LastWriteTime -ge [datetime]"2023-01-01" -and $_.LastWriteTime -le [datetime]"2023-12-31"
    }
  2. Export to a temporary text file:
    $files.FullName | Out-File -FilePath "filelist.txt" -Encoding utf8
  3. Run 7-Zip:
    & "C:\Program Files\7-Zip\7z.exe" a "backup.7z" "@filelist.txt"
  4. Clean up:
    Remove-Item "filelist.txt"

Method 2: Using Windows Command Prompt (forfiles)

If restricted to CMD, use forfiles to filter files modified on or after a specific date. Note that forfiles natively filters by a single threshold date rather than an exact start-and-end bracket.

Archive Files Modified on or After a Specific Date

forfiles /P "C:\SourceFolder" /S /D +01/01/2023 /C "cmd /c echo @path" > filelist.txt
"C:\Program Files\7-Zip\7z.exe" a archive.7z @filelist.txt
del filelist.txt
  • /P: Specifies the target search path.
  • /S: Instructs recursive searching into subdirectories.
  • /D +01/01/2023: Selects files with a last modified date greater than or equal to (+) the specified date (MM/DD/YYYY format).
  • /C: Executes the echo command for each matching file.

Method 3: Using Linux Bash (find)

On Linux environments using p7zip or standard 7z, the find command can isolate files within an exact date range using the -newermt flag.

# Generate file list between two dates
find /path/to/source -type f -newermt "2023-01-01" ! -newermt "2023-12-31" > filelist.txt

# Archive using 7-Zip
7z a archive.7z @filelist.txt

# Remove temporary list
rm filelist.txt
  • -newermt "2023-01-01": Matches files modified after January 1, 2023.
  • ! -newermt "2023-12-31": Excludes files modified after December 31, 2023.