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

仅用于文件大小的Windows命令?

  •  38
  • Liam  · 技术社区  · 15 年前

    是否有Windows命令将以字节为单位输出指定文件的大小?

    >filesize test.jpg
    65212
    

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

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

    12 回复  |  直到 8 年前
        1
  •  46
  •   Kothar    15 年前

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

    文件:

    @echo off
    echo %~z1
    

    这会产生与您在问题中建议的结果相同的结果。

    类型

    help call
    

    在所有疯狂的变量操作选项的命令提示下。也看到 this article 更多信息。

    编辑: 仅在Windows 2000及更高版本中有效

        2
  •  39
  •   Patrick Cuff    15 年前

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

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

    很难看,但很管用。您还可以传入一个文件掩码以获取多个文件的列表:

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

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

        3
  •  11
  •   David Doumèche    9 年前

    使用函数可以消除~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 1st argument in %size% variable, and return
    :filesize
      set size=%~z1
      exit /b 0
    
        4
  •  10
  •   Iain Samuel McLean Elder    10 年前

    尝试 forfiles :

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

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

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

    如果文件 C:\Temp\file1.txt 是27字节, 伪造档案 运行此命令:

    cmd /c echo 27
    

    哪些版画 27 到屏幕上。

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

        5
  •  3
  •   Scott Weinstein    15 年前

    因为您使用的是XP,所以Windows PowerShell是一个选项。

    (Get-Item filespec ).Length 
    

    或者作为一个函数

    function Get-FileLength { (Get-Item $args).Length }
    Get-FileLength filespec
    
        6
  •  2
  •   Kanagavelu Sugumar    9 年前
    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 (.).
    
        7
  •  1
  •   Mick    11 年前

    from here :

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

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

    /S选项导致搜索子目录,/M选项以兆字节为单位显示磁盘使用情况,/Q:100选项导致标记大于100 MB的文件夹,/D选项仅显示超过/Q指定阈值的文件夹。

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

    diskuse D: /x:104857600 /v /s
    

    /X:104857600选项导致显示超过104857600字节的文件,并且仅当包含/V选项(verbose)时才有效。/S选项表示搜索指定路径(在本例中为d:drive)的子目录。

    使用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
    
        8
  •  1
  •   nixda Shay Levy    11 年前

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

    $imageObj = New-Object System.IO.FileInfo("C:\test.jpg")    
    $imageObj.Length
    
        9
  •  1
  •   Nikita R.    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
    
        10
  •  1
  •   user5814704    8 年前

    创建filesize.cmd(并放入c:\windows\system32):

    @echo %~z1
    
        11
  •  1
  •   Bo Persson tox    8 年前

    在批处理文件中,以下内容适用于本地文件,但对网络硬盘上的文件无效

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

    但是,它是劣质代码,因为它不适用于保存在网络驱动器上的文件(例如\nas\t e s t.jpg、\192.168.2.40\t e s t.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 /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
    
        12
  •  0
  •   eccieThe Techie    15 年前

    在PowerShell中,您 应该 这样做:

    (get childitem c:\temp\file1.txt).length