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

寻求批量重命名文件匹配父文件夹名称的通用方案

Solution to Rename Files to Parent Folder Name (Keep Extension)

Got it, here's a robust cross-platform solution that exactly matches your requirements—no fixed file types, no hardcoded paths, and it only targets direct subfolders of your current directory:


PowerShell (Windows)

This script works on all modern Windows versions (Windows 10/11, Server 2016+). It handles spaces and special characters in folder/file names, and includes a preview mode to avoid mistakes.

# Step 1: Open PowerShell in your target root directory (the one containing your subfolders)
# Step 2: Run this script with -WhatIf first to preview changes, then remove -WhatIf to apply

Get-ChildItem -Directory | ForEach-Object {
    $folderName = $_.Name
    # Get all files in the current subfolder (ignore subfolders inside it)
    Get-ChildItem -Path $_.FullName -File | ForEach-Object {
        $newFileName = "$folderName$($_.Extension)"
        # Preview mode: remove -WhatIf to actually rename files
        Rename-Item -Path $_.FullName -NewName $newFileName -WhatIf
    }
}

Enhanced Version (Handle Duplicate Extensions)

If a subfolder has multiple files with the same extension (e.g., two .jpg files), this version adds a sequential number to avoid conflicts:

Get-ChildItem -Directory | ForEach-Object {
    $folderName = $_.Name
    $fileCounter = 1
    Get-ChildItem -Path $_.FullName -File | ForEach-Object {
        $baseName = $folderName
        if ($fileCounter -gt 1) {
            $baseName = "$folderName-$fileCounter"
        }
        $newFileName = "$baseName$($_.Extension)"
        Rename-Item -Path $_.FullName -NewName $newFileName -WhatIf
        $fileCounter++
    }
}

Bash Script (Linux/macOS)

This script works on any Unix-like system. It also includes a preview mode and handles special characters properly.

#!/bin/bash
# Step 1: Save this as rename-to-folder.sh
# Step 2: Make it executable: chmod +x rename-to-folder.sh
# Step 3: Run it in your target root directory (preview mode first)

for dir in */; do
    # Remove trailing slash from folder name
    folder_name="${dir%/}"
    # Loop through all files in the subfolder (skip nested subfolders)
    for file in "$dir"*; do
        [ -f "$file" ] || continue
        # Extract file extension
        file_ext="${file##*.}"
        # Build new file name
        new_name="$dir$folder_name.$file_ext"
        # Preview mode: replace echo with mv to apply changes
        echo "Preview: Renaming '$file' to '$new_name'"
        # mv "$file" "$new_name"
    done
done

Enhanced Version (Handle Duplicate Extensions)

For subfolders with multiple files of the same type, this adds a number suffix to prevent overwrites:

#!/bin/bash
for dir in */; do
    folder_name="${dir%/}"
    count=1
    for file in "$dir"*; do
        [ -f "$file" ] || continue
        file_ext="${file##*.}"
        new_name="$dir$folder_name.$file_ext"
        # Add sequential number if file already exists
        while [ -f "$new_name" ]; do
            new_name="$dir$folder_name-$count.$file_ext"
            ((count++))
        done
        echo "Preview: Renaming '$file' to '$new_name'"
        # mv "$file" "$new_name"
    done
done

Example Before/After

Original Structure

MyRootFolder/
├─ SummerVacation2024/
│  ├─ IMG_001.jpg
│  └─ video_clip.mp4
└─ ProjectProposal/
   ├─ draft_v2.docx
   └─ meeting_notes.txt

After Running Script

MyRootFolder/
├─ SummerVacation2024/
│  ├─ SummerVacation2024.jpg
│  └─ SummerVacation2024.mp4
└─ ProjectProposal/
   ├─ ProjectProposal.docx
   └─ ProjectProposal.txt

Key Notes

  • No Recursion: Only processes direct subfolders of your current directory—won't touch files in the root, or nested subfolders inside your target subfolders.
  • Any File Type: Works with all extensions, no hardcoding required.
  • Safe Preview: Always run the preview mode first (-WhatIf in PowerShell, echo in Bash) to verify changes before applying.
  • Special Characters: Handles spaces, accents, and special symbols in folder/file names correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:23:33