Windows命令仅用于文件大小?

有没有一个Windows命令,会输出这样的指定文件的字节大小?

>filesize test.jpg 65212 

我知道dir命令输出这个信息,但是它也输出其他信息。

我可以很容易地编写这样的程序,但是如果可能的话,我宁愿使用本地Windows命令,或者只使用Windows XP的全新安装。

如果您在批处理脚本中,则可以使用参数variables技巧来获取文件大小:

filesize.bat:

 @echo off echo %~z1 

这给出了你在你的问题中build议的结果。

types

 help call 

在所有疯狂的variables操作选项的命令提示符。 另请参阅此文章以获取更多信息。

编辑:这只适用于Windows 2000及更高版本

如果您不想在批处理脚本中执行此操作,则可以通过命令行执行以下操作:

 for %I in (test.jpg) do @echo %~zI 

丑,但它的作品。 您还可以传入文件掩码来获取多个文件的列表:

 for %I in (*.doc) do @echo %~znI 

将显示每个.DOC文件的大小,文件名。

在〜z运算符中使用一个函数来摆脱一些限制,特别是for循环中的usefull:

 @echo off set size=0 call :filesize "C:\backup\20120714-0035\error.log" echo file size is %size% goto :eof :: set filesize of 1st 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

variables@fsize被replace为每个文件的大小。

如果文件C:\Temp\file1.txt是27个字节, forfiles运行此命令:

 cmd /c echo 27 

打印27到屏幕上。

作为一个副作用,它会清除你的屏幕,就像你运行了cls命令一样。

由于您使用的是XP,Windows PowerShell是一个选项。

 (Get-Item filespec ).Length 

或者作为一个function

 function Get-FileLength { (Get-Item $args).Length } Get-FileLength filespec 

这不正是你所问的,它只能从命令行使用(并可能在batch file中没用),但一个快速的方法来检查文件大小只是使用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 
 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 (.). 

创buildfilesize.cmd(并放到C:\ Windows \ System32):

 @echo %~z1 

在Powershell中你应该这样做:

(Get-ChildItem C:\ TEMP \ file1.txt).Length

采取从这里 :

以下命令在D:驱动器上查找大于100 MB大小的文件夹:

 diruse /s /m /q:100 /dd: 

/ s选项会导致search子目录,/ m选项以兆字节显示磁盘使用情况,/ q:100选项会导致标记大于100 MB的文件夹,而/ d选项仅显示超过阈值的文件夹由/ q指定。

使用diskuse命令查找超过一定大小的文件。 以下命令在D:驱动器上显示大小超过100 MB的文件:

 diskuse D: /x:104857600 /v /s 

/ x:104857600选项会导致超过104,857,600字节的文件显示,并且仅在包含/ v选项(详细)时才有效。 / s选项表示从指定的path(在本例中为D:驱动器)中search子目录。

使用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 

在batch file中,下面的文件适用于本地文件,但对于networking硬盘驱动器上的文件不适用

 for %%I in ("test.jpg") do @set filesize=%~z1 

然而,它是低级代码,因为它不适用于保存在networking驱动器上的文件(例如\ Nas \ test.jpg,\ 192.168.2.40 \ test.jpg)以下代码适用于任何位置的文件,请自行编写。 我相信有更有效的方法来使用vbs或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 /ad "%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