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 /Sto recursively scan every.txtfile 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\becomesA01-B01). - We append this cleaned folder string to the original filename to create the new name.
2. Move Files to the Base Folder
- The
MOVEcommand 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
/Yflag to theMOVEcommand (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 withsort /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>NULattempts to delete each folder. The2>NULsuppresses 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
forloop.
内容的提问来源于stack exchange,提问作者Mustafa YILDIZ
相关产品推荐
相关产品推荐

