How to find the size of a file
In Windows, we can use dir command to get the file size.
C:\>dir vlcplayer.exe Directory of C:\ 02/22/2011 10:30 PM 20,364,702 vlcplayer.exe 1 File(s) 20,364,702 bytes 0 Dir(s) 86,917,496,832 bytes free
But there is no option/switch to print only the file size.
Get size for all the files in a directory
Dir command accepts wild cards. We can use ‘*” to get the file sizes for all the files in a directory.
C:\>dir C:\Windows\Fonts Volume in drive C is Windows 7 Volume Serial Number is 14A1-91B9 Directory of C:\Windows\Fonts 06/11/2009 02:13 AM 10,976 8514fix.fon 06/11/2009 02:13 AM 10,976 8514fixe.fon 06/11/2009 02:13 AM 11,520 8514fixg.fon 06/11/2009 02:13 AM 10,976 8514fixr.fon 06/11/2009 02:13 AM 11,488 8514fixt.fon 06/11/2009 02:13 AM 12,288 8514oem.fon 06/11/2009 02:13 AM 13,248 8514oeme.fon 06/11/2009 02:13 AM 12,800 8514oemg.fon
We can also get size for files of certain type. For example, to get file size for mp3 files, we can run the command ‘dir *.mp3‘.
The above command prints file modified time also. To print only the file name and size we can run the below command from a batch file.
@echo off for /F "tokens=4,5" %%a in ('dir c:\windows\fonts') do echo %%a %%b
Save the above commands to a text file, say filesize.bat, and run it from command prompt.
Get directory size
There’s no Windows built in command to find directory size. But there is a tool called diruse.exe which can be used to get folder size. This tool is part of XP support tools. This command can be used to get directory size. This command’s syntax is given below.
diruse.exe directory_name C:\>diruse c:\windows Size (b) Files Directory 12555896050 64206 SUB-TOTAL: C:\WINDOWS 12555896050 64206 TOTAL: C:\WINDOWS
As you can see in the above example, diruse prints the directory size in bytes and it also prints the number of files in the directory(it counts the number of files in the sub folders also)
To get the directory size in mega bytes we can add /M switch.
C:\>diruse /M C:\Windows Size (mb) Files Directory 11974.24 64206 SUB-TOTAL: C:\WINDOWS 11974.24 64206 TOTAL: C:\WINDOWS
Though the tool is intended for XP and Server 2003, I have observed that it works on Windows 7 also. The above examples were indeed from a Windows 7 computer.