如何压缩文件夹中前缀相同的成对文件(含7z与文档文件)
Solution to Pair and Compress Matching Files
Hey there! Let's get your paired files compressed together smoothly. Since you already have matching sets like File Name 1.7z and File Name 1 Directory.doc in your C:\destination folder, we can use a batch command to iterate through each pair and package them into a single compressed file.
Batch Command (Save as .bat file)
Create a new text file, paste the code below, and save it with a .bat extension (e.g., pair_compress.bat):
@echo off setlocal enabledelayedexpansion :: Set your destination folder path here set "dest=C:\destination" :: Iterate through all .7z files in the destination folder for /f "delims=" %%i in ('dir /b "%dest%\*.7z"') do ( :: Extract the base name (without .7z extension) set "prefix=%%~ni" :: Define the corresponding .doc file path set "doc_file=%dest%\!prefix! Directory.doc" :: Check if the matching .doc file exists if exist "!doc_file!" ( :: Compress both files into a new combined .7z "C:\Program Files\7-Zip\7z.exe" a "%dest%\!prefix!_combined.7z" "%dest%\%%i" "!doc_file!" -stl -mx=5 -mhe -mtc -bb :: Optional: Uncomment the lines below to delete original files after compression :: del "%dest%\%%i" :: del "!doc_file!" ) else ( echo Warning: No matching .doc file found for %%i ) ) echo Done processing all file pairs! pause
Key Details & Explanations
setlocal enabledelayedexpansion: Needed to handle variables with spaces and update them correctly inside the loop.dir /b "%dest%\*.7z": Lists all .7z files in the destination folder in bare format (only filenames, no extra metadata).%%~ni: Extracts the base name of the .7z file (e.g.,File Name 1fromFile Name 1.7z).- 7-Zip Command: We reuse your original compression parameters (
-stl,-mx=5, etc.) for consistency. The new compressed file will be named likeFile Name 1_combined.7z. - Optional Cleanup: If you want to remove the original .7z and .doc files after creating the combined package, uncomment the
dellines.
Quick Notes
- Make sure the path to
7z.exeis correct (the example uses the default installation path; adjust if you installed 7-Zip elsewhere). - If you want to run this directly in Command Prompt instead of a batch file, replace all
%%with%in the command.
内容的提问来源于stack exchange,提问作者MikeS
相关产品推荐
相关产品推荐

