Extract Files by Wildcard in 7-Zip Scripts
Automating file extraction from compressed archives is a common
requirement in data processing pipelines and system administration. This
guide explains how to extract specific files matching wildcard patterns
using the 7-Zip command-line utility (7z.exe). You will
learn the core commands, proper wildcard syntax, directory recursion
options, and how to implement these commands within automated shell
scripts.
Basic Command Syntax
7-Zip provides two primary commands for extraction:
e: Extracts files from an archive, flattening the folder structure (all files go into a single directory).x: Extracts files with their full directory paths intact.
To extract files matching a specific pattern, append the wildcard
string to the end of the command or specify it using the include switch
(-i).
Basic structure:
7z x <archive_name> "<wildcard_pattern>" -o<output_directory> -y-o<output_directory>: Specifies the destination directory (note: no space between-oand the path).-y: Assumes "yes" to all prompts, which prevents scripts from hanging on overwrite warnings.
Wildcard Usage
7-Zip supports standard wildcards:
*: Matches zero or more characters.?: Matches any single character.
Extracting by File Extension
To extract all .csv files from an archive regardless of
where they are located:
7z x archive.zip *.csv -o"C:\ExtractedFiles" -r -yThe -r switch enables recursion, ensuring 7-Zip searches
through all subfolders inside the archive for matching .csv
files.
Extracting by Prefix or Pattern
To extract log files generated on a specific date format (e.g.,
log_2023_*.txt):
7z x logs.7z log_2023_*.txt -o"C:\Logs" -yUsing the Include Switch
(-i)
When scripts require multiple wildcard patterns or complex filtering,
use the include switch syntax (-i[r[-|0]]!pattern):
-i!*.xml: Includes all.xmlfiles at the root level.-ir!*.xml: Recursively includes all.xmlfiles throughout all subdirectories.-ir!*.jpg -ir!*.png: Extracts multiple matching file types in a single operation.
Example:
7z x data.tar.gz -ir!*.json -ir!*.xml -o"C:\Processed" -yAutomation Script Examples
Windows Batch Script
(.bat)
This script checks an incoming folder for archives and extracts only
.pdf files:
@echo off
set "SEVENZIP=C:\Program Files\7-Zip\7z.exe"
set "ARCHIVE_DIR=C:\Archives"
set "DEST_DIR=C:\Output"
for %%A in ("%ARCHIVE_DIR%\*.zip") do (
"%SEVENZIP%" x "%%A" *.pdf -o"%DEST_DIR%" -r -y
)Windows PowerShell Script
(.ps1)
PowerShell provides robust error handling for automated 7-Zip tasks:
$7zPath = "C:\Program Files\7-Zip\7z.exe"
$archives = Get-ChildItem -Path "C:\Archives" -Filter "*.zip"
$destination = "C:\Output"
foreach ($archive in $archives) {
$arguments = @(
"x",
"`"$($archive.FullName)`"",
"-ir!*report*.xlsx",
"-o`"$destination`"",
"-y"
)
Start-Process -FilePath $7zPath -ArgumentList $arguments -Wait -NoNewWindow
}Linux Shell Script
(.sh)
On Linux systems using p7zip:
#!/bin/bash
TARGET_DIR="/path/to/extracted"
mkdir -p "$TARGET_DIR"
for archive in /path/to/archives/*.7z; do
7z x "$archive" -ir!'*.sql' -o"$TARGET_DIR" -y
doneNote: In Linux environments, wrap wildcard patterns in single quotes to prevent the host shell from expanding the wildcards before passing them to 7-Zip.