How can I find and delete all video files from all drives using batch file

How can I find and delete all video files from all drives using batch file

I want to search and delete *.mp4 *.mpeg, *.mkv, *.flv, *.avi (all video files) from all drives (C,D,E,F) or anywhere in my computer in any directory or subdirectory, using BATCH File. My operatin system is windows XP.

答案1

for %%drive in (C D E F) do (
    for /R %drive:\ %%f in (*.mp4 *.mpeg *.mkv *.flv *.avi) do (
        @echo del "%f"
    )
)

(Documentation)

I have no means to test this right now. Just run it and see, if it does everything right. Afterwards remove the @echo and run it again.

答案2

First of all, it could be quite dangerous to blindly delete files with a batch file because it would wipe out anything and everything. And while this is seemingly your goal, you have to be aware that it would also destroy files that are part of the operating system (Windows comes with a few videos here and there), as well as any programs and games that have video files. If your goal is to free up space, then there are better ways to do that (besides, not all videos are large).

Anyway, after my initial version magically stopped working for no apparent reason (yes, I tried to account for everything), I ended up using a work-around. The only draw-back to using pushd is that if you abort the batch-file, you’ll end up in whatever drive it was in when you pressed Ctrl+Break.

Instead of just blindly deleting everything, I made it so that it builds a secondary batch-file (delmovie.bat) which you can scan through to make sure there isn’t anything you want to keep. Then you can just run the generated batch-file to delete them. It takes a few extra seconds but is much safer.

@echo off
echo @echo off > delmovie.bat
for %%i in (C:\Users C:\Videos D:\ E:\ F:\) do (
  pushd %%i
  for /r %%j in (*.mp4 *.mpeg *.mkv *.flv *.avi) do (
    echo del "%%j" >> %~dp0delmovie.bat
  )
  popd
)

This version excludes system directories with a white-list, not a black-list, so instead of excluding directories you want to ignore, it adds the ones you want to scan (doing the other way around would be a little more complex and I can provide a script to do that if you need). I added c:\videos as an example of how you can add your own directories if needed (it doesn’t hurt anything to leave c:'videos in there if you don’t have such a directory).

相关内容