windows 如何通过批处理文件列出所有大小的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21711180/
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
How to list all folder with size via batch file
提问by goldenbutter
I want a simple solution for list of folders and size of them in either txt or csv format.
我想要一个简单的解决方案,用于 txt 或 csv 格式的文件夹列表和它们的大小。
I use this code for folder list
我将此代码用于文件夹列表
dir C:\Temp\*.* /b /a:d > C:\folderList.txt
current output
电流输出
<<folderList.txt>>
folder1
folder2
folder3
desired output
期望的输出
<<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
只是它会生成每个文件夹的大小.. 我该如何继续?任何帮助
回答by MC ND
For each folder in the list, use dir
command to retrieve the size of the files under the folder
对于列表中的每个文件夹,使用dir
命令检索文件夹下文件的大小
@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 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.
对于其中的每个文件夹 ( for /d
),dir
在内部for
命令中执行递归命令,并从其输出findstr
中解析末尾(由 提取)的摘要行(tokens
infor
命令)以及该子文件夹下所有文件的总大小被检索。然后文件夹的名称(和扩展名,如果有的话)和它下面元素的大小被回显到控制台。
If a file needs to be created, redirect the output of the batch to a file
如果需要创建文件,将批处理的输出重定向到文件
getSizes.cmd "c:\temp" > C:\folderList.txt
回答by Matt Williamson
Using MC ND's excellent code, I've added conversion to Kb, Mb, Gb, etc. Just in case you'd rather have it in those formats.
使用 MC ND 的优秀代码,我添加了到 Kb、Mb、Gb 等的转换功能。以防万一您更愿意使用这些格式。
@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
call :GetUnit !size! unit
call :ConvertBytes !size! !unit! newsize
echo(%%~nxa - !newsize! !unit!
endlocal
)
endlocal
exit /b
:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~2" EQU "TB" set val=/1024/1024/1024/1024
> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)
for /f "delims=" %%a in (
'cscript //nologo %temp%\tmp.vbs'
) do endlocal & set %~3=%%a
del %temp%\tmp.vbs
exit /b
:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %1 LEQ 1024 set "unit=Bytes"
if %1 GTR 1024 set "unit=KB"
if %1 GTR 1048576 set "unit=MB"
if %1 GTR 1073741824 set "unit=GB"
if %byt:~-14% GTR %TB:~-14% set "unit=TB"
endlocal & set %~2=%unit%
exit /b
回答by Leland
I took @Matt Williamsons code and made it export each line to a .csv file in the run directory, folderSizes.csv. It provides the full Byte size as a column so that you can easily sort in excel (or whatever).
我采用了@Matt Williamsons 代码,并将每一行导出到运行目录中的 .csv 文件,folderSizes.csv。它提供完整的字节大小作为一列,以便您可以轻松地在 excel(或其他)中进行排序。
@echo off
echo Getting folder sizes for you...storing to folderSizes.csv
setlocal disabledelayedexpansion
if EXIST folderSizes.csv del folderSizes.csv
echo Folder,Bytes Size,Short Size > folderSizes.csv
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
call :GetUnit !size! unit
call :ConvertBytes !size! !unit! newsize
echo(%%~nxa,!size!,!newsize!!unit! >> folderSizes.csv
endlocal
)
endlocal
exit /b
:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~2" EQU "TB" set val=/1024/1024/1024/1024
> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)
for /f "delims=" %%a in (
'cscript //nologo %temp%\tmp.vbs'
) do endlocal & set %~3=%%a
del %temp%\tmp.vbs
exit /b
:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %1 LEQ 1024 set "unit=Bytes"
if %1 GTR 1024 set "unit=KB"
if %1 GTR 1048576 set "unit=MB"
if %1 GTR 1073741824 set "unit=GB"
if %byt:~-14% GTR %TB:~-14% set "unit=TB"
endlocal & set %~2=%unit%
exit /b
回答by dbenham
My JREN.BAT utilitycan be used to get a list of folders with sizes. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
我的JREN.BAT 实用程序可用于获取具有大小的文件夹列表。它是一个混合 JScript/批处理脚本,可以在 XP 以后的任何 Windows 机器上本地运行。
JREN does not conveniently convert to MB (or any other unit) - it simply lists the size in bytes. But it sure is convenient (and comparatively fast) to get the listing:
JREN 不能方便地转换为 MB(或任何其他单位)——它只是以字节为单位列出大小。但是获取列表确实很方便(并且相对较快):
jren "$" "' # '+size()" /d /j /list /p "d:\temp" >"C:\folderList.txt"
You might consider putting the folder size first, space padded to a fixed width that exceeds the biggest folder, followed by the folder name. I find this format much easier to read, and it is still easy to parse:
您可能会考虑将文件夹大小放在首位,将空间填充为超过最大文件夹的固定宽度,然后是文件夹名称。我发现这种格式更容易阅读,而且仍然很容易解析:
jren "^" "size(' ')+' '" /d /j /list /p "d:\temp" >"C:\folderList.txt"
The output would look something like this
输出看起来像这样
1852 SomeFolderName
1616869 Another folder name
137764 yetAnother
Since JREN is a batch file, you must use CALL JREN if you put the command within another batch script.
由于 JREN 是一个批处理文件,如果您将命令放在另一个批处理脚本中,则必须使用 CALL JREN。
回答by Ketan Verma
if you are using windows 7 or new use powershell and type command
如果您使用的是 Windows 7 或新用户,请使用 powershell 并输入命令
ls- r >> log.txt
ls-r >> 日志.txt
it will list all the files in current directory along with file size in bytes to log file.
它将列出当前目录中的所有文件以及以字节为单位的文件大小以记录文件。