Exclude files if specific folder name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Exclude files if specific folder name

#1 Post by Weshuggah » 05 Feb 2021 05:43

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

Squashman
Expert
Posts: 4240
Joined: 23 Dec 2011 13:59

Re: Exclude files if specific folder name

#2 Post by Squashman » 05 Feb 2021 08:49

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
)
Last edited by Squashman on 05 Feb 2021 09:21, edited 2 times in total.
Reason: Fixed true/false logic

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

Re: Exclude files if specific folder name

#3 Post by Weshuggah » 05 Feb 2021 09:11

Hmm for some reason with this nothing happens, even if subfolder name is different, no files are being renamed.

Squashman
Expert
Posts: 4240
Joined: 23 Dec 2011 13:59

Re: Exclude files if specific folder name

#4 Post by Squashman » 05 Feb 2021 09:15

I had the logic backwards. Remove the NOT.

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

Re: Exclude files if specific folder name

#5 Post by Weshuggah » 05 Feb 2021 09:30

Right, it works now, thank you!

Squashman
Expert
Posts: 4240
Joined: 23 Dec 2011 13:59

Re: Exclude files if specific folder name

#6 Post by Squashman » 05 Feb 2021 09:38

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.

Post Reply