You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写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 $keywordToTarget hashtable lets you easily add more keywords and their corresponding folders. Just add a new line like "KEYWORD" = "TARGET_PATH".
  • Case Control: The -match operator uses case-insensitive matching by default. If you need exact case matching, replace -match with -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-ChildItem line to include -Filter "*.pdf" (replace with your desired extension).

How to Use

  1. Update the $sourceDir variable to point to your actual source directory.
  2. Add/modify entries in $keywordToTarget to match your keywords and target folders.
  3. Adjust the $overwrite setting based on your preference for handling duplicate files.
  4. Save the script as MoveFilesByKeyword.ps1 and run it in PowerShell.

内容的提问来源于stack exchange,提问作者dcraven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:31:44