使用批处理删除多个文件夹名称的最后 n 个单词并用括号括住剩余的最后一个单词



我是新来的。我感谢您的帮助。我在当前目录中有多个文件夹,命名如下:

word1 word2 word3 word4 word5 word6 word7 word8 word9

我需要一个批处理文件来删除最后 n 个单词,其中包含所有文件夹名称的额外空格,并用括号括住最后一个剩余的单词。如您所见,单词由空格分隔,并且它们的长度不同,例如,如果n=3会导致:

word1 word2 word3 word4 word5 (word6)
@echo off
setlocal enabledelayedexpansion
set n=3
for /d %%d in ("*") do (
call :reformat "%%d" %n%
echo --!newstring!--
ren "%%d" "!newstring!"
)
goto :eof
:reformat
set "newstring="
set i=0
for %%a in (%~1) do (
set /a i+=1
set "sub!i!=%%a"
)
set /a x=i-%2
for /l %%a in (1,1,%x%) do (
if %%a == %x% (
set "newstring=!newstring:~1! (!sub%%a!)"
) else (
set "newstring=!newstring! !sub%%a!"
)
)

最新更新