Page 1 of 1

Exclude files if specific folder name

Posted: 05 Feb 2021 05:43
by Weshuggah
Hello,

This code below changes .dll files into .bak files in BaseFolder and its subfolders.
I just want to exclude files whose folder containing them has a specific name, let's say there is a subfolder called "DoNotModify".

Code: Select all

set folder=My\BaseFolder\
pushd "!folder!"
set "folderpath=!cd!"
if not "!folder:~-1!"=="\" set "folder=!folder!\"
	
	for /R %%f in (*.dll) do (
		ren "%%f" *.bak
	)
	popd

Re: Exclude files if specific folder name

Posted: 05 Feb 2021 08:49
by Squashman
Use string replacement to test if the folder exists within the folder path.

Code: Select all

for /R %%G in (*.dll) do (
     set "foldercheck=%%~dpG"
     IF  "!foldercheck:DoNotModify=!"=="!foldercheck!"  ren "%%~G" *.bak
)

Re: Exclude files if specific folder name

Posted: 05 Feb 2021 09:11
by Weshuggah
Hmm for some reason with this nothing happens, even if subfolder name is different, no files are being renamed.

Re: Exclude files if specific folder name

Posted: 05 Feb 2021 09:15
by Squashman
I had the logic backwards. Remove the NOT.

Re: Exclude files if specific folder name

Posted: 05 Feb 2021 09:30
by Weshuggah
Right, it works now, thank you!

Re: Exclude files if specific folder name

Posted: 05 Feb 2021 09:38
by Squashman
You should probably change the rename to

Code: Select all

ren "%%~G" "%%~nG.bak"
The "n" modifier is the file name only. No path no extension.