Windows Server 2012 R2批量解压目录及子目录下特定命名Zip文件求助
Bulk Unzip
dumpdata_yymm_as_tap.zip Files to Dedicated Folders Hey there! Since you're new to PowerShell and batch scripts, let's walk through two easy-to-follow methods to handle your bulk unzipping task. Both will automatically find all your matching ZIP files and extract each to a unique target folder under f:\.
Method 1: PowerShell (Recommended for Paths with Spaces)
PowerShell handles spaces in file/folder names smoothly, making it a great choice here. Here's a script you can use right away:
# Set the root path where your ZIP files are stored $sourceRoot = "Z:\abc" # Set the root path for your target extraction folders $targetRoot = "f:\" # Find all ZIP files matching the pattern: dumpdata_yymm_as_tap.zip $zipFiles = Get-ChildItem -Path $sourceRoot -Filter "dumpdata_*_as_tap.zip" -Recurse # Loop through each ZIP file one by one foreach ($zip in $zipFiles) { # Use the ZIP's base name (without .zip) as the target folder name $targetFolderName = $zip.BaseName # Keep the original subfolder structure (like data1/data2) in the target path $relativePath = $zip.DirectoryName.Substring($sourceRoot.Length) $targetFolder = Join-Path -Path $targetRoot -ChildPath "$relativePath\$targetFolderName" # Create the target folder if it doesn't exist yet if (-not (Test-Path -Path $targetFolder)) { New-Item -ItemType Directory -Path $targetFolder | Out-Null } # Extract the ZIP content to the target folder (overwrites existing files if present) Expand-Archive -Path $zip.FullName -DestinationPath $targetFolder -Force Write-Host "Extracted $($zip.Name) to $targetFolder" }
How to use this:
- Open PowerShell (search for "PowerShell" in Windows Start menu)
- Copy-paste the script above, adjust
$sourceRootand$targetRootif your paths are different - Press Enter to run the script
Quick notes:
- The
-Forceparameter will overwrite existing files in the target folder (remove it if you don't want this behavior) Expand-Archiveis built into PowerShell 5.1 and later (most modern Windows systems have this pre-installed)
Method 2: Batch Script (Using 7-Zip)
If you prefer batch scripts, you'll need 7-Zip installed (it's free and widely used for handling archives). First, install 7-Zip, then note its installation path (usually C:\Program Files\7-Zip\7z.exe).
Here's the batch script:
@echo off setlocal enabledelayedexpansion :: Set your source and target root paths set "sourceRoot=Z:\abc" set "targetRoot=f:\" set "7zipPath=C:\Program Files\7-Zip\7z.exe" :: Find all matching ZIP files recursively for /r "%sourceRoot%" %%f in (dumpdata_*_as_tap.zip) do ( :: Get the relative subfolder path from the source root set "relativeDir=%%~dpf" set "relativeDir=!relativeDir:%sourceRoot%=!" :: Get the ZIP file's base name (without .zip extension) set "folderName=%%~nf" :: Build the full target folder path set "targetFolder=%targetRoot%!relativeDir!!folderName!" :: Create the target folder if it doesn't exist if not exist "!targetFolder!" mkdir "!targetFolder!" :: Extract the ZIP file with 7-Zip (automatically confirms overwrite prompts) "%7zipPath%" x "%%f" -o"!targetFolder!" -y echo Extracted "%%~nxf" to "!targetFolder!" ) endlocal pause
How to use this:
- Open Notepad, copy-paste the script, adjust the paths (
sourceRoot,targetRoot,7zipPath) as needed - Save the file with a
.batextension (e.g.,BulkUnzip.bat) - Double-click the
.batfile to run it
Quick notes:
- Replace
7zipPathwith your actual 7-Zip installation path if it's different - The
-yparameter skips overwrite confirmation prompts (remove it if you want to be asked before overwriting)
Beginner Pro Tips:
- Always test with a small number of files first (copy a few ZIPs to a test folder and run the script there)
- Make sure you have write permissions for the
f:\target directory - If you see errors, double-check the paths in the script for typos (especially spaces in folder names)
内容的提问来源于stack exchange,提问作者aksr
相关产品推荐
相关产品推荐

