All I need is the sum total of all the folders and subfolders and files in a directory. More specifically, if I run the "dir /s" command, I get these two lines at the last. How can I get just the total number of bytes in a folder?
125 File(s) 682,763,152 bytes
7 Dir(s) 17,952,768,000 bytes free
Is the only way to pipe the output of the "dir /s" command to a file, read the file to extract just the previous-to-last line?
Thank you
Just the total size of a folder & subfolders
Moderator: DosItHelp
Re: Just the total size of a folder & subfolders
That's a piece of code I've once taken out from a thread here at DosTips.
Make sure there is no trailing backslash in the path. Don't worry about the use of robocopy. The /L option makes it a simulation without any physical action.
Steffen
Code: Select all
@echo off &setlocal
set "folder=%userprofile%\Desktop"
for /f "tokens=1,2 delims=: " %%a in ('robocopy "%folder%" "%folder%" /L /S /NJH /BYTES /FP /NC /NDL /NFL /TS /XJ /R:0 /W:0') do if /i "%%a"=="Bytes" set "size=%%b"
echo %size% bytes
pause
Steffen
Re: Just the total size of a folder & subfolders
The following alternative, will work with your output:
I'm not sure if all system languages output the filesize as the third token, so if this is going to be used for any other system languages, I'd be interested to know.
Code: Select all
@SetLocal EnableExtensions
@For /F Tokens^=3 %%G In ('Dir /S/A-D/-C 2^>NUL')Do @Set /A $=#,#=%%G 2>NUL
@Echo %$% bytes
@Pause
Re: Just the total size of a folder & subfolders
JREN.BAT is a hybrid batch/JScript regular expression file/folder renaming utility. But it also has options that make it useful as a directory listing utility, with the ability to display the total size of all files within a folder (including all subfolders recursively).
The following will give the size of each folder in the current directory, left padded to length 12, followed by 2 spaces and then the folder name.
To get all folders within "c:\somePath\":
To get the size of "c:\somePath\someFolder"
Obviously you must use CALL JREN if you put the command within your own batch script.
The only problem with JREN is all output will be aborted with an error if JREN runs into a file or folder to which it does not have access. The problem can be somewhat avoided if run as administrator. But even then I still run into problems with the "Users" and "Windows" folders on my boot drive on Windows 10. Here I use a simple FOR loop to get individual folders to list one at a time so I can get most of the folders, and the ones that fail I can detect the error and print out the problem folder name
The following will give the size of each folder in the current directory, left padded to length 12, followed by 2 spaces and then the folder name.
Code: Select all
jren "^" "size(' ')+' '" /d /list /j
Code: Select all
jren "^" "size(' ')+' '" /d /list /j /p "c:\somePath\"
Code: Select all
jren "^" "size(' ')+' '" /d /list /j /p "c:\somePath\" /fm "someFolder"
The only problem with JREN is all output will be aborted with an error if JREN runs into a file or folder to which it does not have access. The problem can be somewhat avoided if run as administrator. But even then I still run into problems with the "Users" and "Windows" folders on my boot drive on Windows 10. Here I use a simple FOR loop to get individual folders to list one at a time so I can get most of the folders, and the ones that fail I can detect the error and print out the problem folder name
Code: Select all
C:\>for /d %F in (c:\*) do @call jren "^" "size(' ')+' '" /d /list /j /p "%~dpF" /fm "%~nxF" 2>nul || echo Permission denied: %~nxF
274920773 Apps
17439 DELL
521344 Downloads
24542096 Drivers
853244 Intel
0 PerfLogs
9219668569 Program Files
1536383448 Program Files (x86)
Permission denied: Users
25329606 utils
Permission denied: Windows