I have several folders with the following pattern as name:
123 - 1234 - string1 - string2
and would like to rename them all like
string1 - string2
using a batch file.
I was searching for something like:
@echo off
setlocal EnableDelayedExpansion
for /D %%f in (C:\Users\*) do (
set string=%%f
for /f "tokens=1,2,3,4 delims=-" %%a in (%%f) do (set part1=%%a)&(set part2=%%b)&(set part3=%%c)&(set part4=%%d)
set newstring=part3 - part4
rename "string" "newstring"
)
Unfortunately it isn't working and I've no idea what's wrong... Do you have better ideas?
You must enclose the variable names in exclamation points to cause them to be expanded, as in !part3!
. This must be done every place you want the value of a variable. The exclamation points are used for delayed expansion within a FOR loop. You can use percents for normal expansion, but not within a loop that also sets the value.
Also, your inner FOR /F loop must use double quotes within the IN() clause. As currently written, it is attempting to open a file with the name of your folder.
But there is a simpler way in your case:
@echo off
for /d %%F in (c:\users\*-*-*-*) do for /f "tokens=2* delims=-" %%A in ("%%~nxF") do ren "%%F" "%%B"