Reputation: 45
I need to delete content of a folder recursively except specific extensions to prevent it from being overflowed with non-office files.
I tried this:
@echo off
SET ROOTDIR=E:\Testdel\
for /f %%F in ('dir %ROOTDIR% /b /a-d ^| findstr /vile ".xls .xlt .xlm .xlsx .xlsm .xltx .xltm .ppt .pot .pps .pptx .pptm .potx .ppam .ppsx .sldx .sldm .pdf .xls .doc .dot .docx. .html .docm .dotb .docb .tiff .jpg .png .cdr .cpt .psd"') do del "%%F"
But it outputs the error:
Could Not Find C:\Users\username\New
Any idea why?
Upvotes: 0
Views: 1132
Reputation: 261
This is because the default delimiter is delim=<tab><space>
so it splits file names by spaces. Also, if you want to recursively delete the files then you have to specify the /s
flag for dir
. Try this:
@echo off
set ROOTDIR=E:\Testdel\
for /f "delims=" %%F in ('dir %ROOTDIR% /s /b /a-d ^|findstr /vile ".xls .xlt .xlm .xlsx .xlsm .xltx .xltm .ppt .pot .pps .pptx .pptm .potx .ppam .ppsx .sldx .sldm .pdf .xls .doc .dot .docx .html .docm .dotb .docb .tiff .jpg .png .cdr .cpt .psd"') do del "%%F"
Note:
Wikibooks - Windows Batch Scripting: Switches mentions that generally, switches cannot be accumulated behind a single slash. It also mentions that the findstr
command is an exception and allows /v /i /l /e
to be combined into /vile
as a shortcut. This also means that order here doesn't matter and /vile
= /live
= any other ordering (this was pointed out by Mofi). There may be other commands that support this, however since they are exceptions, its a good idea to separate the switches as this would work in all cases and would cause less confusion.
Upvotes: 4