Page 1 of 1

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

Posted: 05 May 2021 13:34
by busy.bee.forest
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.

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

Posted: 05 May 2021 15:19
by aGerman
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

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

Posted: 06 May 2021 06:27
by busy.bee.forest
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.

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

Posted: 06 May 2021 14:10
by aGerman
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

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

Posted: 07 May 2021 06:54
by busy.bee.forest
I was successfully able to apply this solution. Thank you for the help.