Windows command for file size only

后端 未结 12 1824
死守一世寂寞
死守一世寂寞 2020-11-27 16:12

Is there a Windows command that will output the size in bytes of a specified file like this?

> filesize test.jpg
65212

I know that the d

相关标签:
12条回答
  • 2020-11-27 16:26

    If you don't want to do this in a batch script, you can do this from the command line like this:

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

    Ugly, but it works. You can also pass in a file mask to get a listing for more than one file:

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

    Will display the size, file name of each .DOC file.

    0 讨论(0)
  • 2020-11-27 16:27

    This is not exactly what you were asking about and it can only be used from the command line (and may be useless in a batch file), but one quick way to check file size is just to use dir:

    > dir Microsoft.WindowsAzure.Storage.xml
    

    Results in:

    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
    
    0 讨论(0)
  • 2020-11-27 16:28

    If you are inside a batch script, you can use argument variable tricks to get the filesize:

    filesize.bat:

    @echo off
    echo %~z1
    

    This gives results like the ones you suggest in your question.

    Type

    help call
    

    at the command prompt for all of the crazy variable manipulation options. Also see this article for more information.

    Edit: This only works in Windows 2000 and later

    0 讨论(0)
  • 2020-11-27 16:29
    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 (.).
    
    0 讨论(0)
  • 2020-11-27 16:29

    In a batch file, the below works for local files, but fails for files on network hard drives

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

    However, it's inferior code, because it doesn't work for files saved on a network drive (for example, \\Nas\test.jpg and \\192.168.2.40\test.jpg). The below code works for files in any location, and I wrote it myself.

    I'm sure there are more efficient ways of doing this using VBScript, or PowerShell or whatever, but I didn't want to do any of that; good ol' batch for me!

    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
    
    0 讨论(0)
  • 2020-11-27 16:35

    Use a function to get rid off some limitation in the ~z operator. It is especially useful with a for loop:

    @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
    
    0 讨论(0)
提交回复
热议问题