【发布时间】:2009-01-27 15:30:54
【问题描述】:
是否有一个 Windows 命令可以像这样输出指定文件的大小(以字节为单位)?
> filesize test.jpg
65212
我知道dir 命令会输出此信息,但它也会输出其他信息。
我可以轻松编写这样的程序,但如果可能的话,我更愿意使用原生 Windows 命令,或者只使用全新安装的 Windows XP 中可用的命令。
【问题讨论】:
标签: windows command-line
是否有一个 Windows 命令可以像这样输出指定文件的大小(以字节为单位)?
> filesize test.jpg
65212
我知道dir 命令会输出此信息,但它也会输出其他信息。
我可以轻松编写这样的程序,但如果可能的话,我更愿意使用原生 Windows 命令,或者只使用全新安装的 Windows XP 中可用的命令。
【问题讨论】:
标签: windows command-line
如果您在批处理脚本中,可以使用参数变量技巧来获取文件大小:
文件大小.bat:
@echo off
echo %~z1
这会给出与您在问题中建议的结果类似的结果。
类型
help call
在命令提示符下查看所有疯狂的变量操作选项。另请参阅this article 了解更多信息。
编辑: 这仅适用于 Windows 2000 及更高版本
【讨论】:
for %%I in (%1) do @echo %%~znI 之类的内容。
for %I in (file.ext) do @echo %~zn1 不能在控制台中工作(1 != I xD)
如果您不想在批处理脚本中执行此操作,可以从命令行执行此操作,如下所示:
for %I in (test.jpg) do @echo %~zI
丑陋,但它有效。您还可以传入文件掩码以获取多个文件的列表:
for %I in (*.doc) do @echo %~znI
将显示每个 .DOC 文件的大小、文件名。
【讨论】:
使用函数来摆脱~z 运算符中的一些限制。对于 for 循环特别有用:
@echo off
set size=0
call :filesize "C:\backup\20120714-0035\error.log"
echo file size is %size%
goto :eof
:: Set filesize of first argument in %size% variable, and return
:filesize
set size=%~z1
exit /b 0
【讨论】:
试试forfiles:
forfiles /p C:\Temp /m file1.txt /c "cmd /c echo @fsize"
forfiles 命令为目录p 中的每个文件m 运行命令c。
变量@fsize被替换为每个文件的大小。
如果文件C:\Temp\file1.txt是27字节,forfiles运行这个命令:
cmd /c echo 27
将27 打印到屏幕上。
作为副作用,它会清除您的屏幕,就像您运行了 cls 命令一样。
【讨论】:
由于您使用的是 Windows XP,因此可以选择 Windows PowerShell。
(Get-Item filespec ).Length
或作为函数
function Get-FileLength { (Get-Item $args).Length }
Get-FileLength filespec
【讨论】:
C:\>FORFILES /C "cmd /c echo @fname @fsize"
C:\>FORFILES /?
FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {MM/dd/yyyy | dd}]
Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).
【讨论】:
创建一个名为filesize.cmd的文件(并放入文件夹C:\Windows\System32):
@echo %~z1
【讨论】:
拍摄from here:
以下命令在 D: 驱动器上查找大小超过 100 MB 的文件夹:
diruse /s /m /q:100 /d d:
/s 选项导致搜索子目录,/m 选项以 MB 为单位显示磁盘使用情况,/q:100 选项导致标记大于 100 MB 的文件夹,而 /d 选项仅显示超过 /q 指定的阈值。
使用 diskuse 命令查找超过一定大小的文件。以下命令显示 D: 驱动器上大小超过 100 MB 的文件:
diskuse D: /x:104857600 /v /s
/x:104857600 选项会导致显示超过 104,857,600 字节的文件,并且仅在包含 /v 选项(详细)时才有效。 /s 选项表示搜索来自指定路径(在本例中为 D: 驱动器)的子目录。
使用 VBScript
' This code finds all files over a certain size.
' ------ SCRIPT CONFIGURATION ------
strComputer = "**<ServerName>**"
intSizeBytes = 1024 * 1024 * 500 ' = 500 MB
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colFiles = objWMI.ExecQuery _
("Select * from CIM_DataFile where FileSize > '" & intSizeBytes & "'")
for each objFile in colFiles
Wscript.Echo objFile.Name & " " & objFile.Filesize / 1024 / 1024 & "MB"
next
【讨论】:
这不是您要问的,它只能从命令行使用(并且在批处理文件中可能没用),但是检查文件大小的一种快速方法就是使用 dir:
> dir Microsoft.WindowsAzure.Storage.xml
结果:
Directory of C:\PathToTheFile
08/10/2015 10:57 AM 2,905,897 Microsoft.WindowsAzure.Storage.xml
1 File(s) 2,905,897 bytes
0 Dir(s) 759,192,064,000 bytes free
【讨论】:
在 PowerShell 中你可以这样做:
$imageObj = New-Object System.IO.FileInfo("C:\test.jpg")
$imageObj.Length
【讨论】:
在批处理文件中,以下内容适用于本地文件,但不适用于网络硬盘驱动器上的文件
for %%I in ("test.jpg") do @set filesize=%~z1
但是,它是低级代码,因为它不适用于保存在网络驱动器上的文件(例如,\\Nas\test.jpg 和 \\192.168.2.40\test.jpg)。以下代码适用于任何位置的文件,是我自己编写的。
我确信使用 VBScript 或 PowerShell 或其他任何方法可以更有效地执行此操作,但我不想这样做;对我来说很好!
set file=C:\Users\Admin\Documents\test.jpg
set /a filesize=
set fileExclPath=%file:*\=%
:onemoretime
set fileExclPath2=%fileExclPath:*\=%
set fileExclPath=%fileExclPath2:*\=%
if /i "%fileExclPath%" NEQ "%fileExclPath2%" goto:onemoretime
dir /s /a-d "%workingdir%">"%temp%\temp.txt"
findstr /C:"%fileExclPath%" "%temp%\temp.txt" >"%temp%\temp2.txt"
set /p filesize= <"%temp%\temp2.txt"
echo set filesize=%%filesize: %fileExclPath%%ext%=%% >"%temp%\temp.bat"
call "%temp%\temp.bat"
:RemoveTrailingSpace
if /i "%filesize:~-1%" EQU " " set filesize=%filesize:~0,-1%
if /i "%filesize:~-1%" EQU " " goto:RemoveTrailingSpace
:onemoretime2
set filesize2=%filesize:* =%
set filesize=%filesize2:* =%
if /i "%filesize%" NEQ "%filesize2%" goto:onemoretime2
set filesize=%filesize:,=%
echo %filesize% bytes
SET /a filesizeMB=%filesize%/1024/1024
echo %filesizeMB% MB
SET /a filesizeGB=%filesize%/1024/1024/1024
echo %filesizeGB% GB
【讨论】:
在 PowerShell 中,您应该这样做:
(Get-ChildItem C:\TEMP\file1.txt).Length
【讨论】:
我不确定远程的,但对于本地 Windows 槽 {文件共享/网络},%~z 确实有效
for %%x in ("\\ComputerName\temp\temp.txt") do set "size=%%~zx"
this 的更通用版本。以前的版本可能不需要enableDelayedExpansion enableExtensions,但不能在for循环中运行。
| 不能用于将输出值传递给 set ; for /f 不支持其主题值(编辑路径)中的某些字符,如果没有文本内转义; for /l 不允许更改计数/条件值(启动后); !<<variableName>>:<<escaped text>>*! 不起作用。%%%%x 被传递而不是 !nu_f!,因为这与创建 %%%%x 的原因/用途相同。@setLocal enableDelayedExpansion enableExtensions
@echo off
set "file=C:\Users\Admin\Documents\test.jpg"
for %%x in ("!file!") do set "name=%%~nxx"
for %%x in ("!file!") do set "storage=%%~pdx"
set "storage=!storage:~0,-1!"
dir "!storage!" > "!temp!\fileInfo.txt"
findstr /c:"!name!" "!temp!\fileInfo.txt" > "!temp!\fileInfo_1.txt"
del "!temp!\fileInfo.txt"
set /p "size=" < "!temp!\fileInfo_1.txt"
del "!temp!\fileInfo_1.txt"
call :for 1 2 "call :deleteCollumnFromStart size"
call :for 1 1 "call :keepOnlyAllBefore1stSpace %%%%x size"
:removeSpacesFromEnd
if /i "!size:~-1!" equ " " set "size=!size:~0,-1!"
if /i "!size:~-1!" equ " " goto removeSpacesFromEnd
echo(!size:,= ! bytes
pause
exit /b
:deleteCollumnFromStart
set "%~1=!%~1:* =!"
:removeAllSpacesFromStart
if /i "!%~1:~0,1!" equ " " set "%~1=!%~1:~1!"
if /i "!%~1:~0,1!" equ " " goto removeAllSpacesFromStart
goto :eof
:keepOnlyAllBefore1stSpace
if /i "!%~2:~%~1,1!" equ " " (
set "%~2=!%~2:~0,%~1!"
) else (
set /a "nu1_f= !nu1_f! + 1"
)
goto :eof
:for
set "nu_f=%~1"
set "nu1_f=%~2"
:f_repeatTimes
if not !nu1_f! lss !nu_f! (
rem echo(f_repeatTimes !nu_f! !nu1_f! %*
for %%x in (!nu_f!) do (
%~3
)
set /a "nu_f= !nu_f! + 1"
goto f_repeatTimes
)
goto :eof
【讨论】:
wmic datafile where name='c:\\windows\\system32\\cmd.exe' 获取文件大小/format:value
【讨论】: