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

Windows批处理脚本需求:递归追加文件夹名至TXT并迁移整理

Alright, let's fix this up properly. The script you found only handles part of the renaming, so we'll build out a complete solution that covers all three of your requirements: renaming with parent folder names, moving files to the base folder, and cleaning up subfolders.

Complete Batch Script Solution

Here's the full script that implements every feature you need:

@echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

:: Replace this with your actual base folder path
SET "BaseFolder=C:\BaseFolder"

:: 1. Recursively rename and move all .txt files to the base folder
for /F "delims=" %%G in ('dir /B /S "%BaseFolder%\*.txt"') do (
    :: Get the directory path relative to the base folder
    SET "RelativePath=%%~dpG"
    SET "RelativePath=!RelativePath:%BaseFolder%=!"
    
    :: Replace backslashes with hyphens, and clean up extra hyphens
    SET "FolderSuffix=!RelativePath:\=-!"
    SET "FolderSuffix=!FolderSuffix:-=!"
    
    :: Generate the new file name
    IF NOT "!FolderSuffix!"=="" (
        SET "NewName=!FolderSuffix!-%%~nxG"
    ) ELSE (
        SET "NewName=%%~nxG"
    )
    
    :: Move and rename the file to the base folder
    :: Add /Y at the end (like MOVE ... /Y) to auto-overwrite duplicate files
    MOVE "%%~fG" "%BaseFolder%\!NewName!"
)

:: 2. Delete all empty subfolders (starting from the deepest level)
for /F "delims=" %%D in ('dir /B /AD /S "%BaseFolder%" ^| sort /R') do (
    RD "%%~fD" 2>NUL
)

ENDLOCAL
pause

Breakdown of Each Feature

1. Rename Files with Parent Folder Names

  • We use dir /B /S to recursively scan every .txt file in and under your base folder.
  • Delayed variable expansion (enabled with EnableDelayedExpansion) lets us manipulate path strings inside the loop:
    • First, we strip out the base folder path from the file's directory to get a relative path.
    • We replace all backslashes (\) in that relative path with hyphens (-), then clean up any leading/trailing hyphens (so \A01\B01\ becomes A01-B01).
    • We append this cleaned folder string to the original filename to create the new name.

2. Move Files to the Base Folder

  • The MOVE command handles both renaming and moving in one step, which is more efficient than renaming first then moving.
  • If you expect duplicate filenames and want to auto-overwrite them without prompts, add the /Y flag to the MOVE command (e.g., MOVE "%%~fG" "%BaseFolder%\!NewName!" /Y).

3. Delete All Subfolders

  • We list all subfolders with dir /B /AD /S, then sort them in reverse order with sort /R—this ensures we delete the deepest folders first (since you can't delete a parent folder if it still contains child folders).
  • RD "%%~fD" 2>NUL attempts to delete each folder. The 2>NUL suppresses error messages for non-empty folders, so only empty subfolders get deleted (which matches your requirement).

Quick Notes

  • Always test this script on a copy of your files first to avoid accidental data loss!
  • The script works even if your base folder path contains spaces, since we wrap all paths in quotes.
  • Delayed variable expansion is critical here—it allows us to modify and use variables dynamically inside the for loop.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:53:23