Question

Number of Files and Folder Size (Batch File)

Asked by: gisvpn

Hello,

Is it possible to specify another folder on the computer and work out the number of files within it (even in subfolders) and its size and output it to screen ?

GISVPN

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-30 at 07:24:28ID24277355
Tags

Batch file

Topic

MS DOS

Participating Experts
2
Points
500
Comments
27

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Want output of a batch file
    I run a batch file (in a DOS window in WIN98-SE), called filecopy.bat Essentially it copies lots of files to a backup directory. The batch file sends to the screen various messages of output, like "1 file copied" "8 files copied" "0 file copied&qu...
  2. Redirect screen outputs to a file
    I have a batch file which I want to run on a periodic basis. The commands and programs in the batch file will print to the screen. Is there any way to redirect the screen outputs to a single file? I want the outputs to be appended to the file every time the batch file is run....
  3. delete subfolder created by date in batch file
    I have a folder which is used as receptacle for folders which are named by their creation date(%yyyy%-%mm%-%dd%-%hh%-%mn%). And I want a batch file who can delete all folders except the last 40 folders.This operation will be done on WinXP who didn't contain the deltree comman...
  4. change batch screens color
    hi how to alter color of batch screen please. rubberdi-
  5. do ICC profiles influence print output, and if so, batch co…
    hello, I just finished converting about 11.000 photo's from EPS to TIFF using the Adobe Photoshop CS2 Script Processor. I just the following settings : (Checkbox) save to TIFF don't resize don't use LZW compression (Checkbox) INCLUDE ICC profiles. The question is now h...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: QlemoPosted on 2009-03-30 at 07:33:37ID: 24019313

The following line
 robocopy /njh /l /is . . /S /nfl /ndl
will give you a list like (which can be processed further if you request):


               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :      2605         0      2605         0         0         0
   Files :     16984     16984         0         0         0         0
   Bytes :   1.058 g   1.058 g         0         0         0         0
   Times :   0:00:18   0:00:00                       0:00:00   0:00:18

                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: gisvpnPosted on 2009-03-30 at 07:35:50ID: 24019334

Do you have a little more information on how this works ?

 

by: QlemoPosted on 2009-03-30 at 07:54:41ID: 24019548

Of course, it was just a "teaser" ;-)

robocopy is used to copy or move big amounts of data between two locations (folders, computers). It is able to compare for date change, even date (older than x days, for example).

Here it is just used to collect statistics about the files (the order of options is not relevant):
. .         . is current folder, and it is used both as source and target,
/is        because this tells robocopy to include same files (which it would omit
           without that switch). While comparing . with . all files are the same, obviously.
/njh     No Job Headers (source folder, destination folder, resulting switches used)
/L        List only, do not copy or move
/S        with subfolders
/nfl /ndl  No File List, No Directory List

The combination of the no flags (/njh /nfl /ndl) will result in only the summary shown.


 

by: gisvpnPosted on 2009-03-30 at 07:57:13ID: 24019586

ok thanks, with regards to the question I would like to compare two folders and show the status for both (the number of files and size of the folder).

What would the command be for this ?

Thanks,

GISVPN

 

by: QlemoPosted on 2009-03-30 at 08:03:07ID: 24019655

What do you mean by "compare"? Just compare the numbers, or regard file name/data differences?

 

by: gisvpnPosted on 2009-03-30 at 08:11:50ID: 24019742

just the numbers... If I have two folders it could say something like

Folder X = 13 Files and 456 Bytes
Folder Y = 12 Files and 322 Bytes

:)

 

by: QlemoPosted on 2009-03-30 at 08:14:12ID: 24019773

That will need some kind of processing. Stay tuned.

 

by: gisvpnPosted on 2009-03-30 at 08:34:28ID: 24019968

:)

 

by: QlemoPosted on 2009-03-30 at 08:40:04ID: 24020015

Looks a bit weird, and it does no pretty printing of the numbers. The X and Y in the call statements are used as prefix to the generated environment variables, so you can access those values at your likings. The "subbatch" :count could echo out the numbers, if you do not need them outside.

@echo off
setlocal
 
call :count FolderX X
call :count FolderY Y
echo FolderX: %X-folders% folders with %X-files% files, %X-fsize% Bytes
echo FolderY: %Y-folders% folders with %Y-files% files, %Y-fsize% Bytes
 
exit /b
 
 
:count
set var=%2
set folder=%1
 
for /F "skip=5 tokens=2" %%L in ('robocopy /njh /l /is %folder% %folder% /S /nfl /ndl /bytes') do (
  if not defined %var%-folders (set %var%-folders=%%L) else (
  if not defined %var%-files   (set %var%-files=%%L)   else (
  if not defined %var%-fsize   (set %var%-fsize=%%L)
)))
exit /b
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window

 

by: gisvpnPosted on 2009-03-30 at 09:03:06ID: 24020243

Just to clarify where would I define the paths of the two folders ?

 

by: QlemoPosted on 2009-03-30 at 09:06:56ID: 24020274

"FolderX" is the relative or absolute path of the first folder. You could extend the script for requesting to type in the folders, if you like (only upper part shown):



@echo off
setlocal
 
set /P folderX=First folder: 
set /P folderY=Other folder: 
 
call :count %FolderX% X
call :count %FolderY% Y
echo %FolderX%: %X-folders% folders with %X-files% files, %X-fsize% Bytes
echo %FolderY%: %Y-folders% folders with %Y-files% files, %Y-fsize% Bytes
 
exit /b

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: t0t0Posted on 2009-03-31 at 06:52:12ID: 24028894

Please try the following simple batch file. You can run the batch file with or without a foldername as a parameter.

:: =======================================
:: folderstat.bat
:: =======================================
@echo off

if not exist "%~1\" (
   echo.
   echo Folder %1 does not exist
   exit /b 1
)

set FolderCount=0
set FileCount=0
set FileSizeSum=0

for /f "tokens=*" %%a in ('dir /ad /b /s %1') do (
   set /a FolderCount+=1
)

for /f "tokens=*" %%a in ('dir /a-d /b /s %1') do (
   set /a FileCount+=1
   set /a FileSizeSum+=%%~za
)

echo.
echo Folder %1 contains %FolderCount% Folder(s) and %FileCount% file(s)
echo Total size of %FileCount% file(s) = %FileSizeSum%

 

by: QlemoPosted on 2009-03-31 at 09:30:19ID: 24030678

t0t0,
pay attention to the fact that integer arithmetic is only available up to 2^31-1 = 2147483647, which corresponds to ca. 2GB. Could be too less for e.g. archive files. I recommend to calculate in KB instead (would allow for 2TB), and maybe for accuracy sake, add the remainder separately.

 

by: t0t0Posted on 2009-03-31 at 17:33:42ID: 24035090

Qlemo
I'm paying attention...
Hmmmm... 32-bit precision eh?.... Okay, I can do that.... Thank you for your suggestion.

gisvpn
Please see the modified code which splits the output into GB, MB, KB and Byte sizes (just in case your total file size exceeds 2TB).


@echo off
setlocal enabledelayedexpansion

if not exist "%~1\" (
   echo.
   echo Folder %1 does not exist
   exit /b 1
)

set FolderCount=0
set FileCount=0
set FileSizeB=0
set FileSizeKB=0
set FileSizeMB=0
set FileSizeGB=0

for /f "tokens=*" %%a in ('dir /ad /b /s %1') do (
   set /a FolderCount+=1
)

for /f "tokens=*" %%a in ('dir /a-d /b /s %1') do (
   set /a FileCount+=1
   set /a FileSizeB+=%%~za
   set /a FileSizeKB+=!FileSizeB! / 1024
   set /a FileSizeB=!FileSizeB! %% 1024
   set /a FileSizeMB+=!FileSizeKB! / 1024
   set /a FileSizeKB=!FileSizeKB! %% 1024
   set /a FileSizeGB+=!FileSizeMB! / 1024
   set /a FileSizeMB=!FileSizeMB! %% 1024
)

echo.
echo Folder %1 contains %FolderCount% Folder(s) and %FileCount% file(s)
echo.
echo Total size of file(s) = %FileSizeGB%GB %FileSizeMB%MB %FileSizeKB%KB %FileSizeB%Bytes


 

by: gisvpnPosted on 2009-04-01 at 01:11:44ID: 24036838

Qlemo, t0t0, thank you both for your comments and posts.

t0t0 : thanks for the modified code, could I ask if I wanted to specify a different folder to look at where can I specify the path of the folder to look at?

Thanks,

GISVPN

 

by: t0t0Posted on 2009-04-01 at 01:59:08ID: 24037089

As it stands, you could specify what folder to look at by passing the full path, or relative path as a command line argument as in:

   FOLDERSTAT c:\windows\temp

or (parent folder) as in:

   FOLDERSTAT ..\

etc...

NOTE: As with DOS, if your path contains spaces then you need to enclose it in double-quotes as in:

   FOLDERSTAT "C:\Documents and Settings\Paul\My Documents"

or you could hardcode the path/folder into the code itself. I have modified the code so that you only need to edit a single line - the third line of code. Do not use double-quotes if hardcoding the path in the batch file.

Replace 'c:\windows\temp' on the 3rd line to whatever folder you want to look at:


@echo off
setlocal enabledelayedexpansion

set TargetFolder=c:\windows\temp

if not "%~1"=="" (
   set TargetFolder=%~1
)

if not exist "%TargetFolder%\" (
   echo.
   echo Folder %TargetFolder% does not exist
   exit /b 1
)

set FolderCount=0
set FileCount=0
set FileSizeB=0
set FileSizeKB=0
set FileSizeMB=0
set FileSizeGB=0

for /f "tokens=*" %%a in ('dir /ad /b /s %TargetFolder%\') do (
   set /a FolderCount+=1
)

for /f "tokens=*" %%a in ('dir /a-d /b /s %TargetFolder%\') do (
   set /a FileCount+=1
   set /a FileSizeB+=%%~za
   set /a FileSizeKB+=!FileSizeB! / 1024
   set /a FileSizeB=!FileSizeB! %% 1024
   set /a FileSizeMB+=!FileSizeKB! / 1024
   set /a FileSizeKB=!FileSizeKB! %% 1024
   set /a FileSizeGB+=!FileSizeMB! / 1024
   set /a FileSizeMB=!FileSizeMB! %% 1024
)

echo.
echo Folder %TargetFolder% contains %FolderCount% Folder(s) and %FileCount% file(s)
echo.
echo Total size of file(s) = %FileSizeGB%GB %FileSizeMB%MB %FileSizeKB%KB %FileSizeB%Bytes
echo.

 

by: gisvpnPosted on 2009-04-01 at 03:01:14ID: 24037439

Hi t0t0,

Thanks for the above post. do you know why I would get the message missing operand when I run the code, it appears about 20-30 times each time i run it ?

thanks,


GISVPN

 

by: t0t0Posted on 2009-04-01 at 03:40:54ID: 24037656

my guess is you have not included the line:

   setlocal enabledelayedexpansion

in the code where it is accessed before processing the second FOR loop

 

by: gisvpnPosted on 2009-04-01 at 04:37:50ID: 24038004

to t0t0,

that line is definately there, the number of times that the error appears on the screen depends upon the folder and sometimes never appears !..

Any other thoughts ;) ?

 

by: t0t0Posted on 2009-04-01 at 05:04:43ID: 24038187

have you changed the code in any way?

are you passing any command line arguments, if so, what?

are you running the batch file under XP, if not, what operating system?

are you running the batch file in a COMMAND session or a CMD session?

if you know how to perform a screen copy (by increasing CMD's screen buffers to say 400, remove the first line @echo off, clear the screen, run the batch file, mark and highlight the output, hit enter. and paste into a text file) you could post the output for inspection so that I can see where the problem occurs.




 

by: gisvpnPosted on 2009-04-01 at 05:23:47ID: 24038324

ok let me see what I can do.. its way too big at the moment, however i'll try to reduce the number of files and still make it show the error message :)

 

by: gisvpnPosted on 2009-04-01 at 05:36:54ID: 24038419

Hello,

please see the screenshots below. The first screenshot shows the script as I see it. The second is with echo on..

Another strange thing that I noted it that it also reports the incorrect number of folders and files :

The script reports :

42 folders and 147 Files .... however Windows says its 42 Folders and 139 files....

Also the sizes identifed are different, I am therefore assuming that there are a number of files it cannot read and count ?

:)

 

by: t0t0Posted on 2009-04-01 at 05:56:52ID: 24038582

rplace the 2nd FOR block with the following tempoary code (including the enclosing brackets). run the batch file and inspect the file OUTPUT.TXT by typing:

   MORE < output.txt

None of the square brackets should be empty. Post a copy of the text file. My guess is it's missing the filesize in the 2nd SET statement in the 2nd FOR loop.


(for /f "tokens=*" %%a in ('dir /a-d /b /s %TargetFolder%\') do (
   set /a FileCount+=1
   echo f=[%%a] fz=[%%~za]
   set /a FileSizeB+=%%~za
   set /a FileSizeKB+=!FileSizeB! / 1024
   set /a FileSizeB=!FileSizeB! %% 1024
   set /a FileSizeMB+=!FileSizeKB! / 1024
   set /a FileSizeKB=!FileSizeKB! %% 1024
   set /a FileSizeGB+=!FileSizeMB! / 1024
   set /a FileSizeMB=!FileSizeMB! %% 1024
))>output.txt

 

by: gisvpnPosted on 2009-04-01 at 06:32:32ID: 24038894

Yes there are some with some empty square brackets ? Why would that be ? :)

 

by: t0t0Posted on 2009-04-01 at 09:15:18ID: 24040778

Without looking at the output text file I can't tell....

It runs fine with everything I throw at it. Basically, if the folder does not exist the batch file will exit so we can assume the folder does exist. there are obviously files that are being processed and as far as the code goes, for each file it encounters it should be able to return it's size and filename... so for some reason, something is not right somewhere and I do not suspect the code itself.

you didn't confirm what your operating system is.

On the third line, can you change it to c:\windows\temp and see what happens.

Also, it would be helpful for me to actually see the output file because I need to identify which files, and why, some square brackets are blank. I will be in a better position to help you once I see the results for myself.

Your screen images do not give any clues. You have blanked out certain information which might have been helpful.

 

by: t0t0Posted on 2009-04-14 at 01:34:52ID: 24135941

hiya gisvpn

What's the current status regarding this question?

 

by: gisvpnPosted on 2009-04-30 at 04:46:13ID: 24268820

Sorry for the late reply. I think the problem has been that I was using ROBO copy to copy a folder over and for some reason it was not working with this script. However I have changed the ROBO copy slightly and it appears to be working fine now.

Thanks for everyones input.

GISVPN

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...