Windows command for file size only

Viewed 202304

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 dir command outputs this information, but it outputs other information also.

I could easily write such a program, but I would prefer to use a native Windows command if possible, or only what is available in a fresh install of Windows XP.

14 Answers

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

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.

Try forfiles:

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

The forfiles command runs command c for each file m in directory p.

The variable @fsize is replaced with the size of each file.

If the file C:\Temp\file1.txt is 27 bytes, forfiles runs this command:

cmd /c echo 27

Which prints 27 to the screen.

As a side-effect, it clears your screen as if you had run the cls command.

Since you're using Windows XP, Windows PowerShell is an option.

(Get-Item filespec ).Length 

or as a function

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

In PowerShell you can do:

$imageObj = New-Object System.IO.FileInfo("C:\test.jpg")    
$imageObj.Length

In PowerShell you should do this:

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

I'm not sure about remote ones, but for local Windows trough {File Sharing / Network}, %~z does work

for %%x in ("\\ComputerName\temp\temp.txt") do set "size=%%~zx"

More generalized version of this . The previous version may be not requiring enableDelayedExpansion enableExtensions, but can't run in for loops .

  • Some clarification --
    • | can't be used to pass an output value to set ; for /f doesn't support some characters in it's subject value (the path to edit), if without in-text Escaping ; for /l doesn't allow to change the count/condition values (after start) ; !<<variableName>>:<<escaped text>>*! doesn't work .
    • at keepOnlyAllBefore1stSpace, %%%%x is passed instead of !nu_f!, because that is needed for the same reason/use as %%%%x is made to be created .
@setLocal enableDelayedExpansion enableExtensions
@echo off

set "file=C:\Users\Admin\Documents\test.jpg"

for %%x in ("!file!") do set "name=%%~nxx"
for %%x in ("!file!") do set "storage=%%~pdx"
    set "storage=!storage:~0,-1!"

dir "!storage!" > "!temp!\fileInfo.txt"

findstr /c:"!name!" "!temp!\fileInfo.txt" > "!temp!\fileInfo_1.txt"
    del "!temp!\fileInfo.txt"
set /p "size=" < "!temp!\fileInfo_1.txt"
    del "!temp!\fileInfo_1.txt"

call :for 1 2 "call :deleteCollumnFromStart size"

call :for 1 1 "call :keepOnlyAllBefore1stSpace %%%%x size"

:removeSpacesFromEnd
    if /i "!size:~-1!" equ " " set "size=!size:~0,-1!"
    if /i "!size:~-1!" equ " " goto removeSpacesFromEnd

echo(!size:,= ! bytes

pause
exit /b


:deleteCollumnFromStart
    set "%~1=!%~1:* =!"
    :removeAllSpacesFromStart
        if /i "!%~1:~0,1!" equ " " set "%~1=!%~1:~1!"
        if /i "!%~1:~0,1!" equ " " goto removeAllSpacesFromStart
goto :eof

:keepOnlyAllBefore1stSpace
    if /i "!%~2:~%~1,1!" equ " " (
        set "%~2=!%~2:~0,%~1!"
    ) else (
        set /a "nu1_f= !nu1_f! + 1"
    )
goto :eof

:for
    set "nu_f=%~1"
    set "nu1_f=%~2"
    :f_repeatTimes
        if not !nu1_f! lss !nu_f! (
            rem echo(f_repeatTimes !nu_f! !nu1_f! %*
            for %%x in (!nu_f!) do (
                %~3
            )
            set /a "nu_f= !nu_f! + 1"
            goto f_repeatTimes
        )
goto :eof

wmic datafile where name='c:\\windows\\system32\\cmd.exe' get filesize /format:value

Related