代码之家  ›  专栏  ›  技术社区  ›  Liam

Windows命令仅用于文件大小

  •  51
  • Liam  · 技术社区  · 16 年前

    是否有一个Windows命令可以像这样输出指定文件的大小(以字节为单位)?

    > filesize test.jpg
    65212
    

    我知道 dir 命令输出此信息,但也输出其他信息。

    我可以很容易地编写这样的程序,但如果可能的话,我更愿意使用本机Windows命令,或者只使用WindowsXP新安装中可用的命令。

    14 回复  |  直到 6 年前
        1
  •  46
  •   Kothar    16 年前

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

    filesize.bat:

    @echo off
    echo %~z1
    

    这将给出与您在问题中建议的结果类似的结果。

    类型

    help call
    

    在命令提示符下,选择所有疯狂的变量操作选项。另请参阅 this article 了解更多信息。

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

        2
  •  39
  •   Patrick Cuff    16 年前

    如果你不想在批处理脚本中这样做,你可以从命令行这样做:

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

    丑陋,但它奏效了。您还可以传入文件掩码以获取多个文件的列表:

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

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

        3
  •  11
  •   David Doumèche    10 年前

    使用函数来摆脱某些限制 ~z 操作员。它对a特别有用 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
    
        4
  •  10
  •   Iain Samuel McLean Elder    11 年前

    尝试 forfiles :

    forfiles /p C:\Temp /m file1.txt /c "cmd /c echo @fsize"
    

    这个 forfiles 命令运行命令 c 对于每个文件 m 在目录中 p .

    变量 @fsize 替换为每个文件的大小。

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

    cmd /c echo 27
    

    哪种印刷品 27 到屏幕上。

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

        5
  •  3
  •   Scott Weinstein    16 年前

    由于您使用的是WindowsXP,因此可以选择Windows PowerShell。

    (Get-Item filespec ).Length 
    

    或作为一种功能

    function Get-FileLength { (Get-Item $args).Length }
    Get-FileLength filespec
    
        6
  •  2
  •   Kanagavelu Sugumar    10 年前

    创建一个名为的文件 filesize.cmd (并放入文件夹 C: \Windows\System32 ):

    @echo %~z1
    
        7
  •  1
  •   Mick    12 年前
    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 (.).
    
        8
  •  1
  •   nixda Shay Levy    11 年前

    感兴趣的 from here :

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

    diruse /s /m /q:100 /d d:
    

    /s选项用于搜索子目录,/m选项用于显示磁盘使用情况(以兆字节为单位),/q:100选项用于标记大于100MB的文件夹,/d选项仅显示超过/q指定阈值的文件夹。

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

    diskuse D: /x:104857600 /v /s
    

    /x:104857600选项会显示超过104857600字节的文件,并且只有在包含/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
    
        9
  •  1
  •   Nikita R.    9 年前

    在PowerShell中,您可以执行以下操作:

    $imageObj = New-Object System.IO.FileInfo("C:\test.jpg")    
    $imageObj.Length
    
        10
  •  1
  •   user5814704    9 年前

    在批处理文件中,以下方法适用于本地文件,但不适用于网络硬盘上的文件

    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
    
        11
  •  1
  •   Bo Persson Touseef    9 年前

    这并不是您所要求的,它只能从命令行使用(在批处理文件中可能没有用),但检查文件大小的一种快速方法就是使用 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
    
        12
  •  0
  •   eccieThe Techie    16 年前

    在PowerShell中,您 本应 执行以下操作:

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