删除多个文件夹名称的最后n个单词,并用批处理保留剩余的最后一个单词

问题描述 投票:-1回答:1

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

word1 word2 word3 word4 word5 word6 word7 word8 word9

我需要一个删除带有多余空格的最后n个单词所有文件夹名称,并parenthesize最后一个剩余单词。如您所见,单词之间用空格隔开,并且它们的长度也不相同,例如,如果n = 3结果为:

word1 word2 word3 word4 word5 (word6)
batch-file directory namespaces word batch-rename
1个回答
0
投票
@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!"
  )
)
© www.soinside.com 2019 - 2024. All rights reserved.