Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 



JSI Tip 7739. How can I determine the size and count of files in a folder from a command prompt or batch?

RSS
Subscribe to Windows IT Pro | See More Tips Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!


7739 » How can I determine the size and count of files in a folder from a command prompt or batch? 10-Feb-04


I have scripted SizeCnt.bat to determine the size and count of files in a folder, and optionally in all sub-folders.

The syntax for using SizeCnt.bat is:

SizeCnt Folder [/S]

where Folder is the path to the folder you wish to measure, and /S is an optional switch that will also include the size and count of the files in all of Folder's sub-folders.

The output is displayed on the CMD console, but you can pipe it to a file using the following syntax:

SizeCnt Folder [/S]>FileName

You can import the size and count into environment variables in your script, as in:

for /f "Tokens=1,2" %%s in ('SizeCnt Folder [/S]') do set /a FldSize=%%s&set /a fileCnt=%%t

SizeCnt.bat contains:

@echo off
setlocal
If {%1}=={} goto Syntax
set folder=%1#
set folder=%folder:"=%
set folder=%folder:\#=%
set folder=%folder:#=%
if not exist "%folder%\*.*" goto Syntax
if {%2}=={} goto begin
set xx= %2
set xx=%xx:"=%
if /i "%xx%" NEQ " /s" goto Syntax
:begin
set /a cnt=0
set /a tot=0
pushd "%folder%"
call :doit>nul 2>&1
popd
@echo %tot% %cnt%
endlocal
exit /b 0
:doit
for /f  "Tokens=*" %%s in ('dir /b /as /ah /a-d%xx%') do call :addsize "%%s"
goto :EOF
:Syntax
@echo Syntax: SizeCnt Folder [/S]
endlocal
exit /b 1
:addsize
set /a sz=%~Z1
set /a tot=%tot% + %sz%
set /a cnt=%cnt% + 1



End of Article



Reader Comments
Hi, this is an excellent tool that I've used regularly and have recently added a folder count variable using
*** for /f "usebackq" %%a in (`dir /ad /s /b ^| find /c ":\"`) do set Foldercnt=%%a ***
to the :doit section.

However I've just discovered that it doesn't work if the file or folder structure is larger than 2147483648 bytes (2GB).
If you add a single byte to the stored variable it wraps around to -2147483647 and increases in size until it reaches 2147483648 again when it wraps again.
I've been able to use vbscript to generate the same info but can't find a way to obtain the number of folders.

alunklrodgers September 10, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Microsoft: Save Money ... By Paying for Software

Microsoft this week adopted an interesting tactic in its long-running battle with open source software: Businesses looking to save money over the long haul should simply pay for software instead of moving to free, open source solutions. The rationale? ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing