删除所有设置为隐藏或只读或系统的文件

Delete all files that are set to hidden or read only or system

我想删除当前文件夹和所有子文件夹中的所有jpgini和更多类型,然后删除所有空文件夹(递归)。其中一些文件要么是只读的,要么是隐藏的,甚至设置为系统文件,因此 del /s *.jpg 不会删除它们。问题是当我这样做时,语法似乎正在使用逻辑 anddel /a:h /a:r /s *.jpg 所以只删除只读文件和隐藏文件,而不仅仅是隐藏文件。有没有办法让它使用逻辑 or 来代替?

如果不复制粘贴相同的行并做一些小的改动,我找不到让它工作的例子。

关于rmdir,我必须对当前文件夹做cd吗?因为它说下面的代码中存在语法错误:

del /s *.jpg
del /a:h /a:r /s *.jpg
rmdir /s /q
pause

EDIT3:我认为现在它删除了所有内容:del /s /f /a:h /a:a *.jpg

我发现这个用于删除空文件夹,但如果文件夹设置为只读则不起作用:

https://superuser.com/a/39679/451485

未经测试,但我相信以下方法会起作用:

@echo off

:: Remove readonly / hidden / system attributes from all files of interest
attrib -r -h -s *.jpg /s
attrib -r -h -s *.ini /s
rem etc...

:: Delete the files of interest
del /s *.jpg *.ini

:: for each folder, sorted descending by full path (children come before parent)
for /f "delims=" %%F in ('dir /b /ad /s *^|sort /r') do (
  REM check if folder is empty
  dir /b /a "%%F" | findstr "^" >nul || (
    REM remove directory with /S /Q works, even if folder is read only
    rd /s /q "%%F"
  )
)