Solved

Number of Files and Folder Size (Batch File)

Posted on 2009-03-30
27
2,410 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:gisvpn
  • 12
  • 8
  • 7
27 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 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

Open in new window

0
 

Author Comment

by:gisvpn
ID: 24019334
Do you have a little more information on how this works ?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 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.

 
0
 

Author Comment

by:gisvpn
ID: 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
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24019655
What do you mean by "compare"? Just compare the numbers, or regard file name/data differences?
0
 

Author Comment

by:gisvpn
ID: 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

:)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24019773
That will need some kind of processing. Stay tuned.

0
 

Author Comment

by:gisvpn
ID: 24019968
:)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 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

Open in new window

0
 

Author Comment

by:gisvpn
ID: 24020243
Just to clarify where would I define the paths of the two folders ?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 400 total points
ID: 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

Open in new window

0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 100 total points
ID: 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%

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Expert Comment

by:t0t0
ID: 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


0
 

Author Comment

by:gisvpn
ID: 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
0
 
LVL 16

Expert Comment

by:t0t0
ID: 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.
0
 

Author Comment

by:gisvpn
ID: 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
0
 
LVL 16

Expert Comment

by:t0t0
ID: 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
0
 

Author Comment

by:gisvpn
ID: 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 ;) ?
0
 
LVL 16

Expert Comment

by:t0t0
ID: 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.




0
 

Author Comment

by:gisvpn
ID: 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 :)
0
 

Author Comment

by:gisvpn
ID: 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 ?

:)
batchtest2.bmp
batchtest.bmp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 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

0
 

Author Comment

by:gisvpn
ID: 24038894
Yes there are some with some empty square brackets ? Why would that be ? :)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 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.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24135941
hiya gisvpn

What's the current status regarding this question?
0
 

Author Comment

by:gisvpn
ID: 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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now