如何通过批处理文件列出所有文件夹的大小 [英] How to list all folder with size via batch file
问题描述
我想要一个简单的解决方案,用于 txt 或 csv 格式的文件夹列表及其大小.
I want a simple solution for list of folders and size of them in either txt or csv format.
我将此代码用于文件夹列表
I use this code for folder list
dir C:Temp*.* /b /a:d > C:folderList.txt
电流输出
<<folderList.txt>>
folder1
folder2
folder3
期望输出
<<folderList.txt>>
folder1 # 100 MB
folder2 # 30 MB
folder3 # 110 MB
它只会生成每个文件夹的大小..我该如何继续?任何帮助
Simply it would generate the size of each folder.. How can I proceed?? any help
推荐答案
对于列表中的每个文件夹,使用dir
命令检索文件夹下文件的大小
For each folder in the list, use dir
command to retrieve the size of the files under the folder
@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
它遍历指定的文件夹(作为参数传递给批处理文件或当前目录,如果没有参数).
It iterates over the indicated folder (passed as parameter to the batch file, or current directory if there is no paramter).
对于其中的每个文件夹(for/d
),在内部 for
命令内执行递归 dir
命令,并从其输出,最后的摘要行(由findstr
提取)被解析(for
命令中的tokens
)和所有文件的总大小在此子文件夹下检索.然后文件夹的名称(和扩展名,如果有的话)和它下元素的大小被回显到控制台.
For each folder inside it (for /d
) a recursive dir
command is executed inside the inner for
command, and from its output, the summary line at the end (extracted by findstr
) is parsed (the tokens
in for
command) and the total size of all the files under this subfolder is retrieved. Then the name (and extension if it has) of the folder and the size of the elements under it is echoed to console.
如果需要创建文件,将批处理的输出重定向到一个文件
If a file needs to be created, redirect the output of the batch to a file
getSizes.cmd "c: emp" > C:folderList.txt
这篇关于如何通过批处理文件列出所有文件夹的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!