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

夙愿已清 提交于 2019-12-11 08:36:27

问题


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.


回答1:


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 U.S. 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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/15186704/windows-command-line-determine-folder-contents-size-and-save-as-numerical-valu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!