-1

I'm new here. I appreciate your help. I have multiple folder in current directory named like this:

word1 word2 word3 word4 word5 word6 word7 word8 word9

I need a to remove last n words with extra spaces of all folder's names and parenthesize last remaining word. As you see words are separated by space and their lengths are not the same for example if n=3 result in this:

word1 word2 word3 word4 word5 (word6)
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Welcome to SO. As this is not a free code writing service, you are expected to show your own effort before your question can be answered. So please post what you have tried so far (i.e. your code) and where it failed. – Binarus Dec 08 '19 at 15:35

1 Answers1

0
@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!"
  )
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • tnx anyway but not what i meant the words are unidentified as i mentioned – Rostam Dastan Dec 08 '19 at 13:15
  • They *have* to be defined somehow. Here it's by `set "string="...`. To process folders, you can use a `for /d` loop instead (see `for /?` for details) – Stephan Dec 08 '19 at 13:38
  • :/ Off course they're defined. words are trapped by spaces for example for n=3 it needs to count three spaces and then cut it. – Rostam Dastan Dec 08 '19 at 15:24
  • great improvement tnx so much it worked but not completely. tested it for n=3, foldername=word1 word2 word3 word4 it returned: ~1sub1) and n=1 , foldername= New folder it also returned: ~1sub1).The code can't handle more than 20 folders in a single run. also it can't handle names containing parenthesis. – Rostam Dastan Dec 08 '19 at 20:32
  • You are right about the parentheses (there were none mentioned in your question). I can't replicate the `~1sub1)` issue and there should be nothing to limit it to 20 folders only. – Stephan Dec 08 '19 at 21:18