开发者

How can I delete files that don't match a wildcard?

开发者 https://www.devze.com 2023-02-09 20:13 出处:网络
For example, I can 开发者_开发知识库delete all .exe files using the following wildcard: del *.exe

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:

  1. Create a subdirectory.
  2. Copy all *.exe* files to the subdirectory.
  3. Delete all files (*.*) in the current directory.
  4. Copy all files in the subdirectory to the current directory.
  5. Delete all files in the subdirectory.
  6. 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"...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号