编写PowerShell脚本:依据文件名关键词批量移动文件至目标目录
PowerShell Script to Move Files by Keyword in Filename
Got it, let's build a script that handles exactly what you need—scanning files in your source directory, matching the keyword in their filenames, and moving them to the right target folder. Here's a robust, easy-to-modify solution:
# Define the source directory where your files are stored $sourceDir = "\\oltnas6uk\sd.test.com$\PROJECTS\Account Management\Test- AMM - Report" # Create a hashtable to map keywords to their target folders # Add more key-value pairs here if you need to handle other keywords later $keywordToTarget = @{ "AMM" = "\\ammnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" # Example for another keyword: # "FIN" = "\\nas\path\to\Finance\Reports" } # Optional: Set whether to overwrite existing files (true/false) $overwrite = $false # Get all files in the source directory (adjust -Filter if you only want specific file types, e.g., *.pdf) $files = Get-ChildItem -Path $sourceDir -File foreach ($file in $files) { Write-Host "Processing file: $($file.Name)" # Check each keyword to see if it exists in the filename (case-insensitive match) foreach ($keyword in $keywordToTarget.Keys) { if ($file.Name -match [regex]::Escape($keyword)) { $targetDir = $keywordToTarget[$keyword] # Create the target directory if it doesn't exist if (-not (Test-Path -Path $targetDir)) { New-Item -Path $targetDir -ItemType Directory -Force | Out-Null Write-Host "Created target directory: $targetDir" } # Define the full target path for the file $targetPath = Join-Path -Path $targetDir -ChildPath $file.Name # Handle file conflicts if (Test-Path -Path $targetPath) { if ($overwrite) { Write-Host "Overwriting existing file: $targetPath" Move-Item -Path $file.FullName -Destination $targetPath -Force } else { # Rename the file by appending a number if it already exists $counter = 1 do { $newFileName = "$($file.BaseName)_$counter$($file.Extension)" $targetPath = Join-Path -Path $targetDir -ChildPath $newFileName $counter++ } while (Test-Path -Path $targetPath) Write-Host "File exists, renaming to: $newFileName" Move-Item -Path $file.FullName -Destination $targetPath } } else { # No conflict, just move the file Move-Item -Path $file.FullName -Destination $targetPath Write-Host "Moved file to: $targetPath" } # Exit the inner loop once we find a matching keyword break } } } Write-Host "File processing complete!"
Key Features & Customization Tips
- Keyword-Folder Mapping: The
$keywordToTargethashtable lets you easily add more keywords and their corresponding folders. Just add a new line like"KEYWORD" = "TARGET_PATH". - Case Control: The
-matchoperator uses case-insensitive matching by default. If you need exact case matching, replace-matchwith-cmatch. - Conflict Handling: Choose to overwrite existing files (set
$overwrite = $true) or auto-rename duplicates with a counter (e.g.,Test-AMM - Report_1.docx). - Auto-Create Target Folders: The script will automatically make the target directory if it doesn't already exist.
- Filter File Types: If you only want to process specific file types (like PDFs or Excel docs), modify the
Get-ChildItemline to include-Filter "*.pdf"(replace with your desired extension).
How to Use
- Update the
$sourceDirvariable to point to your actual source directory. - Add/modify entries in
$keywordToTargetto match your keywords and target folders. - Adjust the
$overwritesetting based on your preference for handling duplicate files. - Save the script as
MoveFilesByKeyword.ps1and run it in PowerShell.
内容的提问来源于stack exchange,提问作者dcraven
相关产品推荐
相关产品推荐

