≡ Menu

Get File size and directory size from command line

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

Download XP Support Tools

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.

10 comments… add one
  • Bill Sivula

    Jan-18-2012
    I validated/downloaded/started to install.
    The setup software said, “This program has known compatibility issues.”
    (I will probably continue anyway, in hopes that SOME of the software may work.)
    I’m guessing that the problem is that I am running Windows 7 64-Bit.
    Am I correct, that YOU are using Windows 7 32Bit ?

  • Bill Sivula

    Jan-18-2012—followup
    The attempt to instal; anyway, (in spite of compatibility problem) aborted,
    It said, approximately, “can only be installed on WinXP”.

    • admin

      Try extracting the files instead of installing them. Command for this is something like this.
      installer.exe /x

      My machine is also 64 bit and is running on 64-bit OS.

  • whoever

    In Windows7, right-click on the installer, select Troubleshoot compatibility, select Troubleshoot program, select first option “This program worked in earlier versions of Windows but won’t install or run now”, select Windows XP (Service Pack 2), select Start the program, ignore the compatibility issue warning by clicking Run program

  • shrikant

    Dear Team,

    please suggest how to get size in GB.MB useing DOS

    • chris

      get a calculator divide by 1024

  • Nabil

    You can get the list of the directories and their size using the following command:

    Dir/s |Find /V “/”

    Enjoy

    Regards,

  • Josephat Mwanzia

    The following command should do and output the file as csv

    Dir/s |Find /V “/”> Folder_info.csv

    To get the file use

    notepad.exe Folder_info.txt

    then save as and save where you want.

  • Rick

    The ‘for’ command can return the size of a file using %~zI.
    Type ‘for /?’ at a command prompt for the details.

  • Martin Oyston

    for Windows 10
    forfiles /M *.log /C “cmd /c echo @fsize @file”
    gives filesize and filename for log files in the current directory

    forfiles is a great addition to the dos shell tools!

Leave a Comment