Page 1 of 1

Rename all files in all subfolders except some folders

Posted: 10 Apr 2021 06:10
by Weshuggah
Hello,

I want to rename all .txt files to .old in all subfolders of a main folder except for subfolders called "archive" and "new".

I see how to rename files in all subfolders but I don't know how to exclude some folders from the script.

Code: Select all

For /R MainFolder\ %%G in (*.txt) do REN "%%G" "%%~nG.old"
Any help appreciated! :roll:

Re: Rename all files in all subfolders except some folders

Posted: 10 Apr 2021 10:59
by aGerman
You just need a filter. FINDSTR has an option /V for exclusion.

Code: Select all

for /f "delims=" %%G in ('dir /ad /b "MainFolder\*"^|findstr /vxi /c:"folder x" /c:"folder y"') do (
  for /r %%H in ("MainFolder\%%G\*.txt") do REN "%%H" "%%~nH.old"
)
"folder x" and "folder y" are excluded.

Steffen

Re: Rename all files in all subfolders except some folders

Posted: 10 Apr 2021 13:48
by Weshuggah
Thanks a lot, exactly what I was looking for.