Windows命令行 - 确定文件夹内容大小并另存为数值 [英] Windows Command Line - Determine Folder Contents Size and save as numerical value

查看:300
本文介绍了Windows命令行 - 确定文件夹内容大小并另存为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手在命令行编程,并有一个脚本,我想创建。我需要做的一件事是确定文件夹的内容的大小,并保存数值与一个变量,以供以后比较。



我运行Windows 7命令行和一些文件可能大于2GB。



感谢您提供任何帮助。

解决方案

>



DIR / S命令提供了所有子文件夹的累计文件大小。的文件夹及其子文件夹中的所有文件,但输出根据计算机区域设置(语言)而有所不同。 FIND命令可以隔离包含文件夹大小的行,FOR / F用于解析结果。每个文件夹有一个文件行,加上包含总计的最终条目。最后一个SET命令将取代先前的值。最后,变量扩展搜索和替换用于消除数千个分隔符。



这将获取美国机器上给定文件夹中的总字节数。应在第一个参数中提供文件夹的路径。使用 。

  @echo off 
setlocal
for / ftokens = 3% %A in('dir%〜1\^ | findFile(s)')do set size = %% A
setsize =%size:,=%
echo文件夹%〜f1包含%size%字节

。例如,文件将针对不同的语言而改变,一些国家使用。。而不是,作为千分格。



但是这只会让你一半,如果你想以后比较一些其他值的大小,因为IF语句只能比较大到2147483647(〜2吉字节)的数字。任何大于2147483647的数字被视为2147483647.围绕这个问题的方法是将数字用零填充为恒定宽度,即保证大于将要运行的任何数字。然后通过在比较中包含非数字字符强制IF进行字符串比较。我喜欢使用数字宽度为15位数字(约999太字节)的引号。

  @echo off 
setlocal
for / ftokens = 3%% A in('dir%〜1\^ | findFiles)set size = %% A
set echo =%size:,=%
echo文件夹%〜f1包含%大小%字节

::左侧填充宽度为15位
setpaddedSize = 000000000000000%size%
setpaddedSize =%paddedSize:〜-15%

::比较大小到1太字节
如果%paddedSize%gtr001099511627776 echo该文件夹大于1太字节

注意:DIR命令在计算总文件大小时忽略备用数据流。备用数据流是可与NTFS卷上的文件相关联的隐藏内容。请参见 http://www.bleepingcomputer.com/tutorials/windows-alternate-data -streams / 了解更多信息。


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

这篇关于Windows命令行 - 确定文件夹内容大小并另存为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆