RorySrdn
RorySrdn

Reputation: 11

Searching for folders of a size range in cmd

Hope someone can shed a little light, I've been googling every possible way to do this and i am stuck. built this using bits and pieces i found on the web. the end goal here is the automatically search a folder located on a server for folders that are 30kb or more, and at the very least print the folder names and locations to a log file. if i could get it to copy and paste those matching folders to another location that would be great but that's a little further down the road. here is the block i have so far, I'm aware that the "find" command wont work and i have looked into using "forfiles" but i cant seem to figure out how to get it to work.

TL;DR how do i search for folders 30kb or more and then print them to a log file? am i even close?

dir \\myserverblahblahblah

find -size +30kb -type d -printf "%p %s\n"

folder1\file.txt . > XM8imports.log
rename file.txt filenew.txt >> XM8imports1.log

exit /b 0

Thank you for your help!

Upvotes: 1

Views: 578

Answers (1)

dbenham
dbenham

Reputation: 130819

It depends on your definition of folder size. If you only include files that are directly in the folder (disregard sub-folders), than it is not too bad with pure batch:

I believe this solution works regardless what language you are using. I rely on the fact that after the first 4 lines of a DIR listing, only the file summary line can have exactly 4 space delmited tokens. The size is in the 3rd token. I use the /-C option to make sure the size does not include thousands delimiters.

The size is in bytes, so 30kb = 30*1024 = 30720 bytes.

Note that batch only supports signed 32 bit numbers, so the IF statement can only properly work with sizes < 2 gigabytes. IF will treat any value greater than 2 gigabytes as equal to 2 gigabytes. This is not a problem if your size threshold is < 2 gigabytes. It only becomes an issue if you are looking for folders with say 3 gigabytes in them.

Substitute your actual root path for "yourRootPathHere".

@echo off
>30k_folders.txt (
  for /d /r "yourRootPathHere" %%F in (*) do (
    for /f "skip=4 tokens=3-5" %%A in (
      'dir /a-d /-c "%%F" 2^>nul'
    ) do if "%%B" neq "" if "%%C" equ "" if %%A gtr 30720 echo %%F
  )
)
type 30k_folders.txt

The problem is much more difficult if you want to include sub-folder content when you measure the size of a folder. This is not a practical problem for a pure batch solution. One of the issues is you would have to sum up the size of all sub-folders, and the 2GB math limit can quickly become a problem.

The problem is easily solved in something like VB script, or JScript, or PowerShell.

My JREN.BAT utility can be hacked to easily give your desired result.

JREN.BAT is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. It is primarily used to rename files/folders using regular expression search and replace. But it has a /LIST mode that can be used to get your desired info. It has built in functionality to get the cumulative size of folders, and it is not limited to 2GB.

@echo off
call jren "^.*" "( size() > 30*1024 ) ? path() : ''" /p "YourRootPathHere" /d /list /j /s | findstr . >30k_folders.txt
type 30k_folders.txt

Upvotes: 1

Related Questions