How can I delete files that don't match a wildcard?
For example, I can 开发者_开发知识库delete all .exe
files using the following wildcard:
del *.exe
How can I do the opposite, i.e. delete all files that do not end in .exe
?
You can try this.
FOR /R [directory] %%F IN (*.*) DO IF NOT "%%~xF" == ".[extension]" DEL /F /S "%%F"
Or, if you have only one .exe file, it’s even simpler.
for %i in (*.*) do if not %i == FILE.EXE del %i
There's no direct way, here's one scriptless way to do it:
- Create a subdirectory.
- Copy all
*.exe*
files to the subdirectory. - Delete all files (
*.*
) in the current directory. - Copy all files in the subdirectory to the current directory.
- Delete all files in the subdirectory.
- Remove (rmdir) the subdirectory
And here's the code:
C:\mydir> mkdir temp
C:\mydir> move *.exe temp
C:\mydir> del *.* /f /q
C:\mydir> move temp\*.exe .
C:\mydir> rmdir temp
Don't know if this will work for you (depends on if you have other read-only files on your directory) but this should work:
attrib +R *.exe
del *.*
attrib -R *.exe
Regarding your question, there's no such thing as "excluding" wildcards in DOS default commands
To enhance the response of the question (was about excluding wildcards): if you do not search for a specific extension, you can use this for-loop:
for /F "usebackq" %i IN (`dir /b *^|findstr /i /v <PATTERN>`) DO @echo %i
replace "echo" with "del"...
精彩评论