I would like to delete all files that are less than a specific size in a directory. Does anyone know if there is a Windows command that will do this? something like de
There isn't a switch in the Del command which will delete files based on filesize.
Here is a different approach using robocopy and its filter capabilities. Here is an excerpt of the File Selection Options
shown when robocopy /?
is typed into the command prompt window:
/MAX:n :: MAXimum file size - exclude files bigger than n bytes. /MIN:n :: MINimum file size - exclude files smaller than n bytes. /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date. /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date. /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n. /MINLAD:n :: MINimum Last Access Date - exclude files used since n. (If n < 1900 then n = n days, else n = YYYYMMDD date).
Hence the /MIN
and /MAX
options can be applied here. Since we do not want to copy any files, use the /L
option to list all the items that would be copied without the switch, then parse the returned list by a for /F
loop, which holds the actual deletion command del
in the body:
set "TARGETDIR=."
set "FILES=*.pdf"
for /F "tokens=*" %%F in ('
robocopy "%TARGETDIR%" "%TARGETDIR%" "%FILES%" ^
/L /IS /FP /NC /NS /NDL /NP /NJH /NJS ^
/MIN:0 /MAX:3071
') do (
ECHO del "%%F"
)
After having tested, remove the upper-case ECHO
from the script to actually delete files.
Besides /MIN
, /MAX
and /L
, there are several other options defined in the robocopy
command line, most of which care for the needed output, namely a simple list of full paths of matching files, without any extra information like headers, footers or summaries.
The source and destination directories are both set to our target directory. Normally this would not work, of course (you cannot copy files onto themselves), but since /L
is stated, the file list is generated, but only if the switch /IS
is given too (meaning that "same files" are to be regarded).
I am not sure how to do this from DOS, but this is something we have used in the past; it uses VBScript, so you would call this filesoversize.vbs.
Dim strPath
Dim lngSize
Dim fso
Dim fold
Dim files
Dim file
lngSize = 10000 ' The threshold in bytes (this is 100k)
strPath = "C:\temp\" 'The folder you want to start in
Set fso = CreateObject("scripting.filesystemobject")
Set fold = fso.GetFolder(strPath)
Set files = fold.files
For Each file In files
If file.Size <= lngSize Then file.Delete True
Next
Ok so I used AtomicParsley in a cmd script to add artwork to all my MP4 movies in over 400 sub folders with the --overWrite option. It sometimes barfs and the orignal file is left at less than 2k. I needed a way to find all these messed up MP4 files so I can redownload them.
This is what I used from the command line using Mrchief's example and it works like a charm in windows 7.
@for /f "usebackq delims=;" %A in (`dir /s /b *.mp4`) do @If %~zA LSS 2048 echo "%A"
and this is the output after processing 32 of the files
D:\Movie>@for /f "usebackq delims=;" %A in (`dir /s /b *.mp4`) do @If %~zA LSS 2048 echo "%A"
"D:\Movie\Action\Deadland (2009)\Deadland (2009).mp4"
"D:\Movie\Science Fiction\Alien3 (1992)\Alien3 (1992).mp4"
D:\Movie>
you could replace the echo with del and change the 2048 to search for a different size.
BTW: Using PowerShell (on Windows 10) was the easier way for me:
I only did this on the directory where I wish to delete files smaller than 20MB:
ls | where {$_.Length -lt 20mb} | Remove-Item
Try this from a batch script:
@echo off
setlocal
for /f "usebackq delims=;" %%A in (`dir /b *.pdf`) do If %%~zA LSS 3145728 del "%%A"