Windows command for file size only

后端 未结 12 1972
死守一世寂寞
死守一世寂寞 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: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
    

提交回复
热议问题