1

I am looking for a program that can list all files in a folder/drive as well as all sub-folders, recursively, hierarchically, and with folder/file size in Windows. The output should be saved in a text files.

Here is an example of output I would like to have:

C:\folder>list.bat
.
├── [1.9M]  aclArc-20080325.tgz
├── [ 64K]  aclArc-20080325.tgz.1
├── [ 15G]  backup
│   └── [ 15G]  pubmed.zip
├── [ 293]  connection_ssh2.sh
├── [ 242]  connection_ssh.sh
├── [244G]  corpora
│   ├── [ 15G]  cv_corpus_v1
│   │   ├── [ 12G]  cv_corpus_v1.tar.gz
[...]

I tried:

1) tree accessible natively in cmd.exe, but it doesn't have any option to display file sizes:

C:\folder>tree /?
Graphically displays the folder structure of a drive or path.

TREE [drive:][path] [/F] [/A]

   /F   Display the names of the files in each folder.
   /A   Use ASCII instead of extended characters.

2) On Ubuntu, one can use tree --du, for tree 1.6 and above:

--du For each directory report its size as the accumulation of sizes of all its files and sub-directories (and their files, and so on). The total amount of used space is also given in the final report (like the 'du -c' command.) This option requires tree to read the entire directory tree before emitting it, see BUGS AND NOTES below. Implies -s.

so I tried tree.exe provided in Cygwin and gnuwin32, but both of them come with tree version 1.5. UnxUtils.zip and C:\Program Files (x86)\Git\bin don't contain tree.exe.

  • Maybe Cygwin is an alternative, it has a tree --du possibility. – albert Feb 19 '18 at 14:27
  • What version of Windows are you using? It might be possible without too much trouble to write this yourself in PowerShell. – Jeff Zeitlin Feb 19 '18 at 18:58
  • @JeffZeitlin I use Windows 7 and Windows 10. – Franck Dernoncourt Feb 19 '18 at 19:00
  • @albert I tried tree provided in Cygwin and gnuwin32, but both of them come with tree version 1.5, which doesn't have --du. UnxUtils.zip and C:\Program Files (x86)\Git\bin don't contain tree.exe. – Franck Dernoncourt Feb 19 '18 at 19:01
  • 1
    @FranckDernoncourt, the version of Cygwin (32-bit) has version: $ tree --version tree v1.7.0 (c) 1996 - 2014 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro – albert Feb 19 '18 at 19:30
3

In python the first example in os.walk would be simple to modify to give you what you need.

The example:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print(root, "consumes", end=" ")
    print(sum(getsize(join(root, name)) for name in files), end=" ")
    print("bytes in", len(files), "non-directory files")
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

With humandfriendly installed with pip install humanfriendly to make the friendly format of the sizes.

Modified:

from humanfriendly import format_size
import os
from os.path import join, getsize
from os.path import join, getsize, split

for root, dirs, files in os.walk('.'):
    depth = len(split(root))
    print('--' * depth, split(root)[-1], '[%5s]' % format_size(sum(getsize(join(root, name)) for name in files), binary=True))
    for fname in files:
        print('| '*depth, fname, '[%5s]' % format_size(getsize(join(root, fname)), binary=True))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.