Rename all files in all subfolders except some folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Weshuggah
Posts: 7
Joined: 16 Dec 2020 11:56

Rename all files in all subfolders except some folders

#1 Post by Weshuggah » 10 Apr 2021 06:10

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:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Rename all files in all subfolders except some folders

#2 Post by aGerman » 10 Apr 2021 10:59

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

Weshuggah
Posts: 7
Joined: 16 Dec 2020 11:56

Re: Rename all files in all subfolders except some folders

#3 Post by Weshuggah » 10 Apr 2021 13:48

Thanks a lot, exactly what I was looking for.

Post Reply