简体   繁体   中英

Windows Command Line - Determine Folder Contents Size and save as numerical value

I'm a novice at command line programming, and have a script I want to create. One thing that I need to do is determine the size of a folder's contents and save the numerical value with a variable for comparison later.

I am running Windows 7 command line and some files may be larger than 2GB.

Thanks for any assistance offered.

It's amazing how difficult it is to solve this simple problem with pure, native batch.

EDIT - This answer has been edited to include the cumulative file size of all sub-folders.

The DIR /S command gives the total size of all files in the folder and its sub-folders, but the output changes depending on your computers locale settings (language). The FIND command can isolate the lines containing the folder size, and FOR /F is used to parse the result. There is one "File(s)" line per folder, plus a final entry containing the grand total. The last SET command will supersede prior values. Finally, variable expansion search and replace is used to eliminate the thousands separators.

This will get the total number of bytes within a given folder on a US machine. The path of the folder should be provided in the first argument. Use "." for the current directory.

@echo off
setlocal
for /f "tokens=3" %%A in ('dir "%~1\"^|find "File(s)"') do set size=%%A
set "size=%size:,=%"
echo Folder "%~f1" contains %size% bytes

The above would have to change for different locales. For example, "File(s)" would change for different languages, and some countries use "." instead of "," as thousands separators.

But that only gets you half way there if you want to later compare the size to some other value because the IF statement can only compare numbers as large as 2147483647 (~2 Gigabytes). Any number greater than 2147483647 is treated as 2147483647. The way around that problem is to left pad the number with zeros to a constant width that is "guaranteed" to be larger than any number you will run across. Then force the IF to do a string comparison by including non-numeric characters in the comparison. I like to use quotes with a numeric width of 15 digits (~999 terabytes).

@echo off
setlocal
for /f "tokens=3" %%A in ('dir "%~1\"^|find "File(s)"') do set size=%%A
set "size=%size:,=%"
echo Folder "%~f1" contains %size% bytes

:: Left pad the width to 15 digits
set "paddedSize=000000000000000%size%"
set "paddedSize=%paddedSize:~-15%"

:: Compare the size to 1 Terabyte
if "%paddedSize%" gtr "001099511627776" echo The folder is larger than 1 Terabyte

Note: The DIR command ignores alternate data streams when computing total file size. Alternate data streams are hidden content that can be associated with files on NTFS volumes. See http://www.bleepingcomputer.com/tutorials/windows-alternate-data-streams/ for more information.

search for the string literal "Bytes" at the beginning of a line from robocopy /L src dst . , the /L will prevent actual files from being copied and set it to a local variable

another way is with compact /s .compact tool is not translated and should work on every windows machine in the same way:

@echo off
pushd "%~1"

for /f %%s in ('compact /s ^| find " total bytes"') do (
    set _size=%%s
)

echo folder size is : %_size:,=% bytes
popd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM