0

我正在寻找一种方法来打印(或写入文件)我的 Windows PC 的文件夹树,包括每个文件夹的大小 - 但不是单个文件的大小。输出应如下所示: - 我的文档/图片/自拍/ - 100MB - 我的文档/电影/ - 1000MB - 我的音乐/莫扎特/ - 300MB ..等等

注意:1)如果没有办法可以列出单个文件,但是我可以稍后以编程方式(即使用解析或正则表达式)将它们从列表中删除..

2) 目的是创建一个像http://bl.ocks.org/mbostock/raw/1283663/readme.json这样的分层文件,所以 .json 是最好的,但不是必需的:我将解析文本并创建一个 .json 文件作为第二步。

3)这就是我所拥有的,但我不知道如何将其转换为我的需要。

@echo off
setlocal disabledelayedexpansion

set "folder=%~1"
if not defined folder set "folder=%cd%"

for /d %%a in ("%folder%\*") do (
    set "size=0"
    for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"
    setlocal enabledelayedexpansion
    echo(%%~nxa # !size!
    endlocal
)

endlocal

4) 我只能在我的机器上使用 python 和批处理脚本:(

感谢所有交流

4

1 回答 1

2

这是 Python(2.7 兼容语法)脚本:

import sys
from os import stat, getcwd
from os.path import isdir, isfile, join
from glob import glob
from pprint import pprint

NAME_KEY = "name"
SIZE_KEY = "size"
CHILDREN_KEY = "children"

def _iter_path_w_files(path):
    if isfile(path):
        return {NAME_KEY: path, SIZE_KEY: stat(path).st_size}
    elif isdir(path):
        ret = {NAME_KEY: path, CHILDREN_KEY: []}
        for child in glob(join(path, "*")):
            ret[CHILDREN_KEY].append(_iter_path_w_files(child))
        return ret
    else:  # For readability only
        return None

def _iter_path_wo_files(path):
    ret = {NAME_KEY: path, SIZE_KEY: 0}
    for child in glob(join(path, "*")):
        if isfile(child):
            ret[SIZE_KEY] += stat(child).st_size
        else:
            child_ret = _iter_path_wo_files(child)
            ret.setdefault(CHILDREN_KEY, []).append(child_ret)
            ret[SIZE_KEY] += child_ret[SIZE_KEY]
    return ret

def iter_path(path, show_files=True):
    if show_files:
        return _iter_path_w_files(path)
    else:
        if isfile(path):
            return stat(path).st_size
        elif isdir(path):
            return _iter_path_wo_files(path)
        else:  # For readability only
            return None


if __name__ == "__main__":
    if len(sys.argv) > 1:
        path = sys.argv[1]
    else:
        path = getcwd()

    files = False  # Toggle this var if you want the files reported or not

    d = iter_path(path, files)
    pprint(d)

对于像这样的目录树(文件旁边的数字是它们的大小):

  • 目录0
    • 文件00 (6)
    • 目录00
      • 目录000
        • 目录0000
          • 文件00000 (9)
      • 文件000 (7)
    • 目录01
      • 目录010
      • 文件010 (7)

输出将是:

files = False

{'children': [
              {'children': [
                            {'children': [
                                          {'name': 'dir0\\dir00\\dir000\\dir0000',
                                           'size': 9L
                                          }
                                         ],
                             'name': 'dir0\\dir00\\dir000',
                             'size': 9L
                            }
                           ],
               'name': 'dir0\\dir00',
               'size': 16L
              },
              {'children': [
                            {'name': 'dir0\\dir01\\dir010',
                             'size': 0
                            }
                           ],
               'name': 'dir0\\dir01',
               'size': 7L
              }
             ],
 'name': 'dir0',
 'size': 29L
}

files = True

{'name': 'dir0',
 'children': [
              {'name': 'dir0\\dir00',
               'children': [
                            {'name': 'dir0\\dir00\\dir000',
                             'children': [
                                          {'name': 'dir0\\dir00\\dir000\\dir0000',
                                           'children': [
                                                        {'name': 'dir0\\dir00\\dir000\\dir0000\\file00000',
                                                         'size': 9L
                                                        }
                                                       ]
                                          }
                                         ]
                            },
                            {'name': 'dir0\\dir00\\file000',
                             'size': 7L
                            }
                           ]
              },
              {'name': 'dir0\\dir01',
               'children': [
                            {'name': 'dir0\\dir01\\dir010',
                             'children': []
                            },
                            {'name': 'dir0\\dir01\\file010',
                             'size': 7L
                            }
                            ]
              },
              {'name': 'dir0\\file00',
               'size': 6L
              }
             ]
}

这些是与 json 完全兼容的 python 字典(我对其进行了格式化以提高可读性)(您可以尝试:(字典json.dumps(d)在哪里d))。

于 2016-02-02T11:16:05.480 回答