批量重命名RAR分卷文件并保留分卷编号的技术问询
Solution for Batch Rename of RAR Split Files
To achieve your desired renaming pattern—replacing the original prefix while preserving the numeric suffix and split identifier—here's the adjusted batch script with step-by-step explanations:
Adjusted Batch Code
SETLOCAL EnableDelayedExpansion REM Set your target directory here CD "D:\test\rename" FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "*.rar" /B /A:-D`) DO ( REM Extract the filename without the .rar extension (e.g., "File-01.part1") SET "FullNameNoExt=%%~nA" REM Split the name into two parts using the dot before "part" as the delimiter FOR /F "tokens=1,2 delims=." %%B IN ("!FullNameNoExt!") DO ( REM %%B = original base name (e.g., "File-01"), %%C = split identifier (e.g., "part1") REM Extract the numeric portion from the original base name (everything after the hyphen) SET "OriginalBase=%%B" SET "NumericSuffix=!OriginalBase:*=-!" REM Construct the new filename and rename the file RENAME "%%A" "Newfile__!NumericSuffix!.%%C%%~xA" ) ) ENDLOCAL
Key Changes & Explanations
- Splitting the Filename: We first split the filename (excluding the
.rarextension) into two parts using the dot beforepartas a delimiter. This separates the original base name (likeFile-01) from the split identifier (likepart1). - Extracting the Numeric Suffix: Using batch variable substitution
!OriginalBase:*=-!, we capture everything after the first hyphen in the original base name. This pulls out the01fromFile-01cleanly. - Building the New Name: We combine your fixed prefix
Newfile__, the extracted numeric suffix, the split identifier, and the original.rarextension to form the exact target filename you want.
Notes
- If your original base names use a different separator (not a hyphen), adjust the substitution pattern. For example, if the original was
File_01.part1.rar, change!OriginalBase:*=-!to!OriginalBase:*=_!. - Always test the script on a copy of your files first to confirm it behaves as expected.
- The script only processes
.rarfiles in the specified directory and ignores subdirectories.
内容的提问来源于stack exchange,提问作者Hung PD
相关产品推荐
相关产品推荐

