Link to home
Start Free TrialLog in
Avatar of gisvpn
gisvpnFlag for United States of America

asked on

Number of Files and Folder Size (Batch File)

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of gisvpn

ASKER

Do you have a little more information on how this works ?
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.

 
Avatar of gisvpn

ASKER

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
What do you mean by "compare"? Just compare the numbers, or regard file name/data differences?
Avatar of gisvpn

ASKER

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

:)
That will need some kind of processing. Stay tuned.

Avatar of gisvpn

ASKER

:)
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

Avatar of gisvpn

ASKER

Just to clarify where would I define the paths of the two folders ?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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


Avatar of gisvpn

ASKER

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
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.
Avatar of gisvpn

ASKER

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
my guess is you have not included the line:

   setlocal enabledelayedexpansion

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

ASKER

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 ;) ?
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.




Avatar of gisvpn

ASKER

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 :)
Avatar of gisvpn

ASKER

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
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

Avatar of gisvpn

ASKER

Yes there are some with some empty square brackets ? Why would that be ? :)
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.
hiya gisvpn

What's the current status regarding this question?
Avatar of gisvpn

ASKER

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