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
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).