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

批量处理子文件夹文件:重命名后移至父文件夹并删除子文件夹的实现方法

批量处理子文件夹文件:重命名后移至父文件夹并删除子文件夹的实现方法

Hey there! I get exactly what you're trying to do—cleaning up that nested folder structure by moving files up and renaming them to include their parent subfolder name. Let's break down how to pull this off on different operating systems, since the tools vary a bit.

方法1:Linux/macOS(Shell脚本)

This script will loop through your main folders (like alpha/beta), then handle each subfolder inside them, rename files with the subfolder prefix, move them up, and delete the empty subfolders.

First, save this as cleanup.sh (replace /path/to/Project with your actual project folder path):

cd /path/to/Project

# Loop through each top-level folder (alpha, beta, etc.)
for main_dir in */; do
    # Only proceed if it's a valid directory
    if [ -d "$main_dir" ]; then
        # Loop through each subfolder inside the top-level folder
        for sub_dir in "$main_dir"*/; do
            if [ -d "$sub_dir" ]; then
                # Grab the subfolder name (e.g., "alpha1" from "alpha/alpha1/")
                sub_name=$(basename "$sub_dir")
                # Loop through every file in the subfolder
                for file in "$sub_dir"*; do
                    # Skip any nested directories (we only want files)
                    if [ -f "$file" ]; then
                        filename=$(basename "$file")
                        # Define the new file path with subfolder prefix
                        new_path="$main_dir${sub_name}_$filename"
                        # Move and rename the file
                        mv "$file" "$new_path"
                        echo "Processed: $file -> $new_path"
                    fi
                done
                # Delete the now-empty subfolder
                rmdir "$sub_dir"
                echo "Removed empty folder: $sub_dir"
            fi
        done
    fi
done

To run it:

  1. Open your terminal
  2. Make the script executable: chmod +x cleanup.sh
  3. Run it: ./cleanup.sh

Pro tip: Always back up a copy of your files first before running bulk operations—mistakes happen!

方法2:Windows(PowerShell脚本)

If you're on Windows, PowerShell is your friend here. This script does the exact same job as the Shell version, tailored for Windows paths.

Save this as Cleanup.ps1 (replace C:\path\to\Project with your actual project path):

$projectPath = "C:\path\to\Project"

# Get all top-level directories (alpha, beta, etc.)
Get-ChildItem -Path $projectPath -Directory | ForEach-Object {
    $mainDir = $_
    # Get all subdirectories inside each top-level folder
    Get-ChildItem -Path $mainDir.FullName -Directory | ForEach-Object {
        $subDir = $_
        $subName = $subDir.Name
        # Process every file in the subfolder
        Get-ChildItem -Path $subDir.FullName -File | ForEach-Object {
            $file = $_
            $newFileName = "$subName`_$($file.Name)"
            $newFilePath = Join-Path -Path $mainDir.FullName -ChildPath $newFileName
            # Move and rename the file (force overwrites if duplicates exist)
            Move-Item -Path $file.FullName -Destination $newFilePath -Force
            Write-Host "Processed: $($file.FullName) -> $newFilePath"
        }
        # Delete the empty subfolder
        Remove-Item -Path $subDir.FullName -Force
        Write-Host "Removed empty folder: $($subDir.FullName)"
    }
}

To run it:

  1. Open PowerShell as Administrator (or adjust execution policy if needed)
  2. Navigate to where you saved the script: cd C:\path\to\script\location
  3. Run it: .\Cleanup.ps1

方法3:GUI工具(适合不想写脚本的人)

If you prefer a point-and-click approach, tools like Total Commander or FreeCommander have built-in bulk rename and move features:

  1. Use the search function to find all files in your subfolders
  2. Select all the files, open the bulk rename tool
  3. Add the parent subfolder name as a prefix (most tools let you pull this from the file path)
  4. Move the renamed files to their respective top-level folders
  5. Delete all the empty subfolders manually (or use the tool's "delete empty folders" function)

Whichever method you pick, take a minute to test it on a small copy of your folder structure first to make sure it works exactly how you want.

备注:内容来源于stack exchange,提问作者Jerome Stephan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 11:52:40