我是新来的。我感谢您的帮助。我在当前目录中有多个文件夹,其名称如下:
word1 word2 word3 word4 word5 word6 word7 word8 word9
我需要一个batch-file来删除带有多余空格的最后n个单词所有文件夹名称,并parenthesize最后一个剩余单词。如您所见,单词之间用空格隔开,并且它们的长度也不相同,例如,如果n = 3结果为:
word1 word2 word3 word4 word5 (word6)
@echo off
setlocal enabledelayedexpansion
set n=3
set "string=word1 word2 word3 word4 word5 word6 word7 word8 word9"
call :reformat "%string%" %n%
echo --%newstring%--
goto :eof
:reformat
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!"
)
)