Is there any way to delete all files in a specific folder that are smaller than x MB
using a batch file?
I looked at the forfiles command, but it seems like with it you can only delete files that are older than x days
.
-
@KenWhite I saw that post, but I had absolutely no idea what it was, and how I could modify it to fit my own needs.– Skeleton BowCommented Sep 22, 2016 at 18:24
-
2You modify it to suit your needs by changing the single number that indicates the file size and changing the path used to point to your folder. Why is that difficult to modify for you?– Ken WhiteCommented Sep 22, 2016 at 18:27
-
@KenWhite Because I've never used Batch/DOS before, and just wanted to use it for deleting all these small files without having to learn it... sorry– Skeleton BowCommented Sep 22, 2016 at 18:32
-
@SkeletonBow See my answer below– Sam DentyCommented Sep 22, 2016 at 18:32
-
2Sorry. We're not here to be your code writing service. If you don't want to learn it, hire a contractor to write it for you.– Ken WhiteCommented Sep 22, 2016 at 18:36
|
Show 2 more comments
1 Answer
This is possible by using a for /f
statement. The below script will all delete files below 100KB
(100,000 bytes) Try this:
@echo off
setlocal
:: Size is in bytes
set "min.size=100000"
for /f "usebackq delims=;" %%A in (`dir /b /A:-D *.*`) do If %%~zA LSS %min.size% del "%%A"
-
3Your
DIR
command will also list folders with periods in them. Use the/A-D
option to exclude folders from being listed. Personally I would just use a basicFOR
command as that will not list folders. Commented Sep 22, 2016 at 18:26 -
Thanks for the answer! It works really well. I couldn't find out how it assumes to carry out the command in the current folder (is it the
dir /b
?). It deletes itself as well, but that doesn't really matter – I'll be copying and pasting the file to any folder I want to carry it out in. Commented Sep 22, 2016 at 18:35