51

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liam
  • 19,819
  • 24
  • 83
  • 123

14 Answers14

51

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

Kothar
  • 6,579
  • 3
  • 33
  • 42
  • 9
    Note, this will only work for one file. If you want to be able to pass in a mask to get the sizes of multiple files, change the second line to something like `for %%I in (%1) do @echo %%~znI`. – Patrick Cuff Jan 27 '09 at 16:41
  • @Mike, But what's the command to count the number of bytes of the channeled stdout? (as opposed to saved file) – Pacerier Aug 04 '16 at 20:46
  • @Pacerier, not sure if that's possible using this method. The %~z variables refer specifically to a file passed as an argument, so will be looking up their values from the filesystem, rather than a dynamic stream of bytes. – Kothar Aug 05 '16 at 17:39
  • Don't derp like me and combine the answer's batch with Patrick Cuff's comment. I was going crazy trying to figure out why `for %I in (file.ext) do @echo %~zn1` wasn't working in a console (`1 != I` xD) – kayleeFrye_onDeck Oct 15 '19 at 21:16
49

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.

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
17

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Doumèche
  • 508
  • 3
  • 10
13

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.

Iain Samuel McLean Elder
  • 19,791
  • 12
  • 64
  • 80
6

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
  • 1
    ps: I don't think Powershell is included in a fresh install of XP. Unless you are imaging from a SP2 or later with Powershell already in there. – Cheeso Feb 27 '09 at 06:16
4

Create a file named filesize.cmd (and put into folder C:\Windows\System32):

@echo %~z1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2
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 (.).
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
  • Note that this command wont provide directory size, even there is no ready made command available in windows. You have to write recursive script for that. – Kanagavelu Sugumar Jan 30 '15 at 05:54
1

Taken from here:

The following command finds folders that are greater than 100 MB in size on the D: drive:

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

The /s option causes subdirectories to be searched, the /m option displays disk usage in megabytes, the /q:100 option causes folders that are greater than 100 MB to be marked, and the /d option displays only folders that exceed the threshold specified by /q.

Use the diskuse command to find files over a certain size. The following command displays files over 100 MB in size on the D: drive:

diskuse D: /x:104857600 /v /s

The /x:104857600 option causes files over 104,857,600 bytes to be displayed and is valid only if you include the /v option (verbose). The /s option means subdirectories from the specified path (in this case, the D: drive) are searched.

Using 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
Mick
  • 13,248
  • 9
  • 69
  • 119
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
XFlak
  • 19
  • 1
1

In PowerShell you can do:

$imageObj = New-Object System.IO.FileInfo("C:\test.jpg")    
$imageObj.Length
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
klyde
  • 215
  • 3
  • 14
0

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
Nikita R.
  • 7,245
  • 3
  • 51
  • 62
0

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
irvnriir
  • 673
  • 5
  • 14
0

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 17 '22 at 03:38
0

In PowerShell you should do this:

(Get-ChildItem C:\TEMP\file1.txt).Length
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131