Batch script to delete .txt files in all subfolders except one

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
busy.bee.forest
Posts: 3
Joined: 05 May 2021 13:28

Batch script to delete .txt files in all subfolders except one

#1 Post by busy.bee.forest » 05 May 2021 13:34

I have a question regarding batch scripts. Suppose I have multiple levels of subfolders in my My Documents folder.
C:\Users\ABC\Documents
One of the Subfolders is C:\Users\ABC\Documents\Folder1\Subfolder1.

I would like to write batch script that deletes all XML files in My Documents except the ones in Subfolder1.

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

Re: Batch script to delete .txt files in all subfolders except one

#2 Post by aGerman » 05 May 2021 15:19

I guess something like that should do the trick:

Code: Select all

for /f "delims=" %%i in ('dir /ad /b /s "%userprofile%\Documents"^|find /i /v "\Documents\Folder1\Subfolder1"') do del /f /s /q "%%i\*.xml"
Steffen

busy.bee.forest
Posts: 3
Joined: 05 May 2021 13:28

Re: Batch script to delete .xml files in all subfolders except one

#3 Post by busy.bee.forest » 06 May 2021 06:27

My goal is more complicated. Subfolder1 will possess its own set of subfolders which will vary with time. I would like to write batch script that will not delete XML files in all these subfolders.

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

Re: Batch script to delete .txt files in all subfolders except one

#4 Post by aGerman » 06 May 2021 14:10

Remove option /S from the DEL command. My fault.
The script will exclude all paths which contain string "\Documents\Folder1\Subfolder1". This integrates the exclusion of any subfolder structure underneath "Subfolder1". If this is not the intended behavior, I still don't get it.

Steffen

busy.bee.forest
Posts: 3
Joined: 05 May 2021 13:28

Re: Batch script to delete .txt files in all subfolders except one

#5 Post by busy.bee.forest » 07 May 2021 06:54

I was successfully able to apply this solution. Thank you for the help.

Post Reply