windows 打印带有大小、窗口的文件夹树
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35135068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print the folder tree with sizes, windows
提问by Effe Pelosa
I'm looking for a way to print (or write to file) the folder tree of my windows PC, including the size of each folder - but not of single files. The output should look like: - My Documents/pictures/selfies/ - 100MB - My Documents/movies/ - 1000MB - My Music/Mozart/ - 300MB ..and so forth
我正在寻找一种方法来打印(或写入文件)我的 Windows PC 的文件夹树,包括每个文件夹的大小 - 但不是单个文件的大小。输出应如下所示: - 我的文档/图片/自拍/ - 100MB - 我的文档/电影/ - 1000MB - 我的音乐/莫扎特/ - 300MB ..等等
notes: 1) single files can be listed if there's no way around it, but in a way that I can programmatically (i.e. with parsing or regex) remove them from the list later..
注意:1)如果没有办法,可以列出单个文件,但我可以以编程方式(即使用解析或正则表达式)稍后将它们从列表中删除..
2) the aim is to create a hierarchical file like http://bl.ocks.org/mbostock/raw/1283663/readme.jsonso a .json would be best, but not a requirement: I'll parse the text and create a .json file as a second step.
2)目的是创建一个像http://bl.ocks.org/mbostock/raw/1283663/readme.json这样的分层文件,所以 .json 最好,但不是必需的:我将解析文本并第二步,创建一个 .json 文件。
3) this is what I have, but I have no idea on how to convert it to my needs.
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) I can only work in python and batch scripting on my machine :(
4) 我只能在我的机器上使用 python 和批处理脚本:(
Thanks all AC
感谢所有交流
回答by CristiFati
Here's the Python (2.7 compatible syntax) script:
这是 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)
For a directory tree like (numbers next to files, are their sizes):
对于像(文件旁边的数字,是它们的大小)这样的目录树:
- dir0
- file00 (6)
- dir00
- dir000
- dir0000
- file00000 (9)
- dir0000
- file000 (7)
- dir000
- dir01
- dir010
- file010 (7)
- 目录0
- 文件 00 (6)
- 目录00
- 目录000
- 目录0000
- 文件 00000 (9)
- 目录0000
- 文件 000 (7)
- 目录000
- 目录01
- 目录010
- 文件010 (7)
the outputs would be:
产出将是:
files = False
:
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
:
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
}
]
}
Those are python dictionaries (I formatted them for readability) which are perfectly compatible with json (you can try: json.dumps(d)
(where d
is a dictionary)).
这些是与 json 完美兼容的 python 词典(我将它们格式化以提高可读性)(您可以尝试:(字典json.dumps(d)
在哪里d
))。