3

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.

7
  • @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. Commented Sep 22, 2016 at 18:24
  • 2
    You 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 White
    Commented 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 Commented Sep 22, 2016 at 18:32
  • @SkeletonBow See my answer below
    – Sam Denty
    Commented Sep 22, 2016 at 18:32
  • 2
    Sorry. 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 White
    Commented Sep 22, 2016 at 18:36

1 Answer 1

8

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"
2
  • 3
    Your 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 basic FOR command as that will not list folders.
    – Squashman
    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

Not the answer you're looking for? Browse other questions tagged or ask your own question.