2

Question: how to list all files on volume with size they occupy on disk?

Applicable solutions:

  • cmd script
  • free tool with sqlite/txt/xls/xml/json output
  • C++ / winapi code

The problem:

There are many tools and apis to list files, but their results dont match chkdsk and actual free space info:

                                     Size              Count (x1000)
chkdsk c:                            67 GB             297
dir /S                               42 GB             267                
FS Inspect                           47 GB             251
Total Commander (Ctrl+L)             47 GB             251
explorer (selection size)            44 GB             268
explorer (volume info)               67 GB             -
WinDirStat                           45 GB             245
TreeSize                             couldn't download it - site unavailable
C++ FindFirstFile/FindNextFile       50 GB             288
C++ GetFileInformationByHandleEx     50 GB             288

Total volume size is 70 GB, about 3 GB is actually free.

I'm aware of:

  • File can occupy on disk, more than its actual size, i need the size it occupies (i.e. greater one)
  • Symlinks, Junctions etc - that would be good to see them (though i don't think this alone can really give 20 GB difference in my case)
  • Filesystem uses some space for indexes and system info (chkdisk shows negligible, don't give 20 GB)
  • I run all tools with admin privileges, hidden files are shown.
  • FindFirstFile/FindNextFile C++ solution - this dont give correct results, i don't know because of what, but this gives the same as Total commander NOT the same as chkdsk

Practical problem:

I have 70 GB SSD disk, all the tools report about 50 GB is occupied, but in fact it's almost full. Format all and reinstall - is not an option since this will happens again quite soon.

I need a report about filesizes. Report total must match actual used and free space. I'm looking for an existing solution - a tool, a script or a C++ library or C++ code.

(Actual output below)

chkdsk c:

Windows has scanned the file system and found no problems.
No further action is required.

  73715708 KB total disk space.
  70274580 KB in 297259 files.
    167232 KB in 40207 indexes.
         0 KB in bad sectors.
    463348 KB in use by the system.
     65536 KB occupied by the log file.
   2810548 KB available on disk.

      4096 bytes in each allocation unit.
  18428927 total allocation units on disk.
    702637 allocation units available on disk.

dir /S

Total Files Listed:
    269966 File(s) 45 071 190 706 bytes
    143202 Dir(s)   3 202 871 296 bytes free

FS Inspect http://sourceforge.net/projects/fs-inspect/

47.4 GB 250916 Files

Total Commander

49709355k, 48544M 250915 Files
Yuri Yaryshev
  • 892
  • 6
  • 21
  • What exactly is the question please – David Heffernan Aug 17 '15 at 11:30
  • You have two basic problems 1) [List all files in a directory](https://stackoverflow.com/questions/67273/how-do-you-iterate-through-every-file-directory-recursively-in-standard-c) and 2) [Get a file's size](https://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c) – Cory Kramer Aug 17 '15 at 11:31
  • Question - how to get the list of files and its sizes – Yuri Yaryshev Aug 17 '15 at 11:33
  • And if you aren't interested in coding something in C++ and just want to find what is consuming your disk space, use a software like [TreeSize](http://www.jam-software.com/freeware/) or [WinDirStat](https://windirstat.info/) – Cory Kramer Aug 17 '15 at 11:33
  • The solution in c you recomend will lead to results like total commander wich are incorrect, since not all files are actually found with FindFirst/FindNext, chkdsk dont work this way... – Yuri Yaryshev Aug 17 '15 at 11:34
  • 1
    You need to decide what you want. Do you want a list of all files, and their sizes? Or do you want a report about the space used on the disk? And what do you want us to do? Are you hoping that we will write the program for you. It looks rather that way. You really do need to ask a very specific question – David Heffernan Aug 17 '15 at 11:37
  • I need a report about filesizes. Report total must match actual used and free space. Im looking for an existing solution - a tool, a script or a C++ library or C++ code. – Yuri Yaryshev Aug 17 '15 at 11:41
  • In that case this is off topic here. We don't do library or tool recommendations. – David Heffernan Aug 17 '15 at 11:45
  • I dont need you to do a lib, maybe someone knows existing one. – Yuri Yaryshev Aug 17 '15 at 11:45
  • And maybe some one can tell why FindFirstFile FindNextFile gives incorrect results – Yuri Yaryshev Aug 17 '15 at 11:46
  • Please read the site help to learn what questions are on topic here. – David Heffernan Aug 17 '15 at 11:46
  • Added results for WinDirStat – Yuri Yaryshev Aug 17 '15 at 11:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87161/discussion-between-lord-odin-and-david-heffernan). – Yuri Yaryshev Aug 17 '15 at 11:48
  • FindFirstFile/FindNextFile work correctly. They just don't do what you want. They presumably can't find files that the executing user does not have access to. – David Heffernan Aug 17 '15 at 11:50
  • Great, than why recommend them? I clearly noted in title i need all files on volume, not just iterate some folder to find some accessible files, didn't i? I run all the tools with admin priviliges, still this don't allows to find some files with FindFirst/FindNext. – Yuri Yaryshev Aug 17 '15 at 11:54
  • FindFirstFile/FindNextFile can enumerate all files, if the user has sufficient rights. Bear in mind that your question title suggests that's what you want. Anyway, it would be best to delete the question since you know it is off topic. – David Heffernan Aug 17 '15 at 11:57
  • It isn't off topic. You are not constructive, lets stop this discuss, i'll wait for other answers. – Yuri Yaryshev Aug 17 '15 at 12:04
  • It is off topic. You haven't read the help have you. – David Heffernan Aug 17 '15 at 12:06
  • 1
    There are a lot of things that won't show up in a directory listing, even if running as administrator. For example, System Restore points (stored in the System Volume Information directory) is inaccessible to administrators, but it still contributes to disk space usage. Plus, of course, disk space occupied by the directories themselves, space to record file names, the free sector bitmap, the NTFS change journal... – Raymond Chen Aug 17 '15 at 20:47
  • Requests for existing libraries are off-topic. However, if you're willing to write your own code, [see this](http://stackoverflow.com/a/7459109/886887). – Harry Johnston Aug 18 '15 at 00:56

2 Answers2

1

On a Posix system, the answer would be to use the stat function. Unfortunately, it does not give the number of allocated blocs in Windows so it does not meet your requirements.

The correct function from Windows API is GetFileInformationByHandleEx. You can use FindFirstFile, FindNextFile to browse the full disk, and ask for FileStandardInfo to get a FILE_STANDARD_INFO that contains for a file (among other fields): LARGE_INTEGER AllocationSize for the allocated size and LARGE_INTEGER EndOfFile for the used size.

Alternatively, you can use directly GetFileInformationByHandleEx on directories, asking for FileIdBothDirectoryInfo to get a FILE_ID_BOTH_DIR_INFO structure. This allows you to get information on many files in a single call. My advice would be to use that one, even if it is of less common usage.

Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
  • I've just implemented your recomendation and stepped into a problem: CreateFile for \\?\C:\Documents and Settings\ folder results in GetLastError = "Insufficient privileges". Even though I run it as administrator. Have you encountered this problem? – Yuri Yaryshev Aug 17 '15 at 15:44
  • Looks like a totally different problem. What parameters did you pass to CreateFile? But is could be more suited for another question... – Serge Ballesta Aug 17 '15 at 16:00
  • I fixed the problem: "C:\Documents and Settings" - is junction. But the result is 50 GB (((((.... again – Yuri Yaryshev Aug 17 '15 at 20:02
  • I fixed second problem: Wow64DisableWow64FsRedirection should be called before you start listing system disk. – Yuri Yaryshev Aug 29 '15 at 09:23
1

To get list of all files (including hidden and system files), sorted within directories with descending size, you can go to your cmd.exe and type:

dir /s/a:-d/o:-s C:\* > "list_of_files.txt"

Where:

  • /s lists files within the specified directory and all subdirectories,
  • /a:-d lists only files (no directories),
  • /o:-s put files within directory in descending size order,
  • C:\* means all directories on disk C,
  • > "list_of_files.txt" means save output to list_of_files.txt file

Listing files grouped by directory may be a little inconvenient, but it's the easiest way to list all files. For more information, take a look at technet.microsoft.com

Checked on Win7 Pro.

Gawron
  • 55
  • 11