views:

7384

answers:

7

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.

A: 

in powershell you can do this:

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

$imageObj.Length

this should work

klyde
It will work, but it ignores all the built-in capabilities of PowerShell for common tasks
Scott Weinstein
You'd rather use Get-ChildItem test.jpg | Select-Object Length in Powershell.
Joey
+9  A: 

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

Mike Houston
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
+5  A: 

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
Nice, I was wondering how to get that to work from the command line
Mike Houston
A: 

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

(Get-Item filespec ).Length

or as a function

function Get-FileLength { (Get-Item $args).Length }
Get-FileLength filespec
Scott Weinstein
A: 

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
+1  A: 

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

A: 

in Powershell you SHOULD do this:

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