如何使用Windows批处理按文件名中的非固定数字归档文件?
Alright, let's solve this dynamic file archiving problem for your Windows batch script. Here's a robust solution that automatically groups files by their 4-digit middle code and creates the required zip archives—no manual input needed for daily changing codes:
Dynamic File Archiving Batch Script
This script will scan your C:\files directory, identify files sharing the same 4-digit middle code, and package them into matching zip archives, regardless of what the daily code is.
Full Script Code
@echo off setlocal enabledelayedexpansion set "sourceDir=C:\files" :: Iterate over all PDF files (each acts as a marker for a unique file group) for %%f in ("%sourceDir%\*.pdf") do ( :: Grab the filename without path or extension set "baseName=%%~nf" :: Extract the 4-digit code from the filename :: Step 1: Remove everything before the first underscore (e.g., abc_m2611somthing → m2611somthing) set "tempStr=!baseName:*_=!" :: Step 2: Grab characters 2-5 to get the 4-digit code (skips the leading letter like 'm' or 's') set "fileCode=!tempStr:~1,4!" :: Define the full path for the output zip archive set "zipFile=%sourceDir%\!baseName!.zip" :: Skip if the archive already exists to avoid redundant work if not exist "!zipFile!" ( :: Use 7-Zip to package all files containing the 4-digit code :: If 7-Zip isn't in your PATH, replace "7z" with the full path, e.g., "C:\Program Files\7-Zip\7z.exe" 7z a -tzip "!zipFile!" "%sourceDir%\*!fileCode!*" echo Successfully created archive: !zipFile! ) else ( echo Skipping existing archive: !zipFile! ) ) endlocal echo All archiving tasks finished! pause
Key Details & Explanations
- Delayed Expansion:
setlocal enabledelayedexpansionis critical here—it lets us modify and read variables inside theforloop, which is essential for handling dynamic filenames. - 4-Digit Code Extraction:
!baseName:*_=!strips off everything up to and including the first underscore, leaving us with the part of the filename containing the code.!tempStr:~1,4!slices the string starting at the 2nd character (to skip the letter before the digits) and takes the next 4 characters—perfectly grabbing the target code.
- Compression Tool: The script uses 7-Zip (a free, widely available tool). If you don't have it in your system PATH, just replace
7zwith the full path to7z.exe(e.g.,"C:\Program Files\7-Zip\7z.exe"). - Avoid Duplicates: We check if the zip archive already exists before creating it, so you won't waste time re-packaging files that are already archived.
- Daily Code Adaptability: The script doesn't hardcode any 4-digit values—it automatically detects whatever code is present in the filenames each day.
Alternative (Windows Built-In Tool)
If you don't want to use 7-Zip, you can use Windows' native makecab tool to create CAB archives (note: this isn't a ZIP, but it's a valid archive format). Replace the 7-Zip line with:
makecab /D CabinetName1="!zipFile:.zip=.cab!" /D DiskDirectory1="%sourceDir%" "%sourceDir%\*!fileCode!*"
内容的提问来源于stack exchange,提问作者Oleg
相关产品推荐
相关产品推荐

