有没有办法批量输出磁盘上的文件大小?

Is there a way to output file size on disk in batch?

有没有办法像在属性中那样获取文件在磁盘上的大小 window:

我试过:

(在批处理文件中)

echo %~z1

,

for %i in (TestFile.txt) do echo %~zi

,

dir

但它们只有 return 文件的大小。

有什么方法可以得到像看到的那样的 "size on disk" 在属性中 window?

有趣的问题。我不知道磁盘值的大小是任何可编写脚本的对象的 属性。您可以通过获取文件大小对每个簇的字节数取模,从文件大小中减去该模数,然后加上簇大小来计算它。 (编辑: 或使用 Aacini 的更高效的计算,我仍在努力理解。)

@echo off
setlocal

for %%I in (Testfile.txt) do (
    set "fs=%%~zI"
    for /f %%J in (
        'wmic volume where "driveletter='%%~dI'" get blocksize /value'
    ) do 2>nul set /a %%J
)

echo Size: %fs%

set /a ondisk = ((fs-1)/blocksize+1) * blocksize

echo Size on disk: %ondisk%

许多网站声称 fsutil fsinfo ntfsinfo DRIVE: 是获取每个簇字节数的最佳方式。似乎这种方法充满了危险,不同的标签取决于语言环境,不同版本的 Windows 的行数也不同。此外,正如 Marged 所说,fsutil 需要提升。这种 WMI 方法似乎更普遍,而且不需要管理员权限。

Thanks JosefZ, Marged, and Aacini for all your input!

这不是一个答案,只是@rojo 要求的值:

NTFS-Volumeseriennummer             0xacf01e6ef01e3ed0
NTFS-Version :                           3.1
LFS-Version    :                  2.0
Anzahl der Sektoren :               0x000000000ed737ff
Gesamtzahl Cluster :                0x0000000001dae6ff
Freie Cluster :                     0x00000000008c8d41
Insgesamt reserviert :              0x0000000000000f70
Bytes pro Sektor  :                 512
Bytes pro physischem Sektor :       512
Bytes pro Cluster :                 4096
Bytes pro Dateidatensatzsegment   : 1024
Cluster pro Dateidatensatzsegment : 0
Gültige MFT-Datenlänge :            0x000000001c1c0000
MFT-Start-LCN  :                    0x00000000000c0000
MFT2-Start-LCN :                    0x0000000000000002
MFT-Zonenstart :                    0x00000000018a8ee0
MFT-Zonenende   :                   0x00000000018b12e0
Ressourcen-Manager-Bezeichner:        A81246B1-33B0-11E4-A94B-AEB4ABF863CB

这是来自德语 Windows 8.1。 我认为如果有必要使批处理语言环境独立,则不能采用 grepping 方法。一种解决方案是使用脚本主机编写适当的文件系统对象的脚本。

WMIC 命令有这个结果...

SOMENAME,4096,C:\

...加上我不需要 运行 具有管理权限的命令的优点。

寻找同一问题的答案,我刚找到这个:
http://www.ltr-data.se/opencode.html/
sizeof 工具就是这样做的。

sizeof, freeware by Olof Lagerkvist. http://www.ltr-data.se e-mail: olof@ltr-data.se More info, including distribution permissions and source code is available on the website.

This tool displays the total allocation size on disk for files and directories.

Command line syntax: sizeof [-xo] [-presentation] file1 [file2 ...]

-x Do not follow junctions or other reparse points. This makes sure that sizeof stays within the filesystem where it starts searching.

-o Include sizes of offline files.

-c Count and display number of files in each found directory, instead of count and display sizes of them.

The presentation determines in which format to display the sizes of files and directories. The argument can be any of the following:

-b Number of 512-byte blocks. -h Human readable format. -k Kilobytes. -m Megabytes. -g Gigabytes. -t Terabytes.

它比本页上提出的其他一些解决方案要好,因为它提供了正确的“磁盘大小”(顺便说一句,应该重命名为“分区大小”或“存储设备大小”,因为现在很多存储设备不是“磁盘”)用于压缩或稀疏文件。我正是想比较从文件共享软件部分下载的一堆大文件的 NTFS 压缩效果和“稀疏性”;所以我将首先使用 sizeof 获取当前压缩文件的实际分配大小,然后我将解压缩它们,然后将它们转换为“稀疏”,然后 运行再次sizeof

我只找到了一个可以将非稀疏文件转换为稀疏文件并实际取消分配其空扇区的实用程序:一个名为 SparseTest 的命令行工具,该工具已发布超过 10 年以前,“仅用于演示目的”,似乎很久以前就消失了,但在那里仍然可用:
https://web.archive.org/web/20151103231305if_/http://pages.infinit.net/moonligh/eMule/Releases/SparseTest.zip

SparseTest 还会在应用“稀疏”属性之前显示“磁盘上的大小”(但输出更复杂,因此如果需要该信息用于其他目的,则在批处理脚本中使用起来并不容易).它计算前后的校验和,以确保文件的完整性在此过程中得以保留(我仍然建议先进行备份,然后使用其他工具(如 WinMerge 或 MD5Checker)验证所有文件是否完全相同)。与以蓝色显示的具有“C”属性的文件相反,“稀疏”文件与常规文件没有区别,除了“属性”列中的“P”(它甚至没有出现在“属性”中)。

原生Windows工具fsutil可以设置稀疏属性,但不会实际压缩文件,它的“磁盘大小”仍然存在即使它包含很多空扇区也没有改变;只有当文件大小后来随着空扇区的增加而增加时,它们才会以“稀疏”方式添加,即作为元数据存储而不实际分配。