[英]List combined size of all files with type in batch
我是批处理脚本的新手,我想知道是否有可能在文本文件中列出所有文件的组合大小。
例如:如果有两个.mp3
文件,每个文件大小为1GB
则输出应为
MP3 2147483648 bytes
我知道如何列出目录中的所有文件或仅列出一种类型的文件。 我遇到的问题是将相似文件类型的大小加在一起。
到目前为止,这是我的代码:
@echo off
Rem This is for listing down all the files in the directory Program files
set mypath=%cd%
dir "%mypath%" *.mp3 > %mypath%\lists.txt
echo program completed
调整批处理代码以管理结果中的任意数量的数字非常简单。 该解决方案最多可以管理16位数字的累积结果:
@echo off
setlocal EnableDelayedExpansion
set "digits=8"
rem Assemble the factor with the proper number of digits
set "factor=1"
for /L %%i in (1,1,%digits%) do set "factor=!factor!0"
rem Accumulate size of all extensions in 2 groups of %digits% digits each
for %%a in (*.*) do (
set "size=%factor%%%~Za"
set /A "low[%%~Xa]+=1!size:~-%digits%!-factor, carry=low[%%~Xa]/factor, low[%%~Xa]%%=factor"
set "size=%%~Za"
set "size=!size:~0,-%digits%!"
set /A "high[%%~Xa]+=carry+size"
)
rem Show results
for /F "tokens=2,3 delims=[.]=" %%a in ('set high[') do (
if %%b neq 0 (
set "low=%factor%!low[.%%a]!"
echo %%a %%b!low:~-%digits%! bytes
) else (
echo %%a !low[.%%a]! bytes
)
)
输出示例:
C:\Users\Antonio\Documents\tests> dir *.mp3
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 0895-160E
Directorio de C:\Users\Antonio\Documents\tests
07/07/2010 09:58 a. m. 14,336,418 BigFile.mp3
01/05/2018 03:01 p. m. 143,364,180 BigFile2.mp3
01/05/2018 03:13 p. m. 1,433,641,800 BigFile3.mp3
07/07/2010 09:58 a. m. 14,336,418 BigFileB.mp3
4 archivos 1,605,678,816 bytes
0 dirs 390,478,827,520 bytes libres
C:\Users\Antonio\Documents\tests> test.bat
bat 200759 bytes
csv 20412 bytes
mp3 1605678816 bytes
pdf 62799 bytes
txt 1036041 bytes
我建议对于刚开始使用批处理脚本的人员来说,这项任务要简单得多,它将提供一种利用PowerShell
的解决方案。
@Echo Off
Set "MyDir=%~1"
If Defined MyDir CD /D "%MyDir%" 2>Nul || Exit /B
Dir /B/A-D >Nul 2>&1 || Exit /B
(For /F "Skip=3 Tokens=1-2 Delims=. " %%A In ('PowerShell -NoP -NoL^
"GCI -Ex 'lists.txt' -Fo|?{!$_.PSIsContainer}|Group Extension|"^
"Select Name,@{n='TS';e={(($_.Group|Measure Length -Sum).Sum)}}"
') Do Echo %%A %%B bytes)>"%__CD__%lists.txt"
有了这个你要么:
%~1
替换为目标目录。 要么:
如果两者都不做,则应为当前目录生成输出。
(请注意,由于输出文件会覆盖目标目录中的任何现有文件,因此它将在其自己的结果中包括其自身的旧大小。因此,作为PowerShell
命令的一部分,我添加了一个排除项(未嵌套))
只要累计大小不超过整数限制(2,147,483,647),
这小批可能会做:
:: Q:\Test\2018\05\01\SO_50117738.cmd
@Echo off & SetLocal
Pushd "C:\Test"
For /f "delims==" %%A in ('2^>Nul Set Ext[') Do Set "%%A="
For /f "delims=" %%A in ('Dir /B/A-D') Do Set /A "Ext[%%~xA]+=%%~zA/1024"
Set Ext[
现在,示例输出为整个KB(被截断的字节)
> Q:\Test\2018\05\01\SO_50117738.cmd
Ext[.cmd]=0
Ext[.html]=2
Ext[.ps1]=11
Ext[.sh]=3
Ext[.txt]=3
Ext[.vbs]=0
Ext[]=0
> powershell -nop -nol "$ext=@{};ls -File|%{$ext.\"$($_.Extension)\"+=$_.Length};$ext"
Name Value
---- -----
.txt 4567
.ps1 12827
.vbs 435
.cmd 2000
.sh 4605
.html 3536
219
另一个具有对输出的更多控制权
> powershell -nop -nol "$ext=@{};ls -File|%{$ext.\"$($_.Extension)\"+=$_.Length};$ext.GetEnumerator()|sort Name|%{'{0,-6} {1,12}' -f $_.Name,$_.Value}"
219
.cmd 2000
.html 3536
.ps1 12827
.sh 4605
.txt 4567
.vbs 435
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.