批处理提取某文件夹中的文件容量并赋值给变量,进行除法运算

例如: A 文件夹 中有 A1.BAT A2.BAT A3.BAT B1.CAD 4个文件.
提取后缀BAT的3个文件.统计文件大小
dir *.bat 可以查出文件总大小.
将查出的文件大小赋值给变量 SS
需要得到的结果如下:
echo /SS = "文件总容量"/3 (3个文件)
"文件总容量"除以"文件总数" >>test.txt

@echo off
setlocal enabledelayedexpansion
dir *.bat /-c >temp.tmp
find "/" temp.tmp>temp2.tmp
del temp.tmp
for /f "delims=: tokens=2" %%i in (temp2.tmp) do (
set aa=%%i
set aa=!aa: =_!
echo !aa!>>temp.tmp
)
del temp2.tmp
set ncount=0
set nsum=0
for /f "delims=_ tokens=2" %%j in (temp.tmp) do (
set /a nsum=!nsum!+%%j
set /a ncount=1+!ncount!
)
del temp.tmp
set /a nsum=!nsum!/!ncount!
echo 平均数是!nsum!


保存为cmd文件运行即可输出结果

不要保存为bat,否则计算中会把自己的大小也算上

追问

一个文件的容量是20GB.所以可能有点问题.能换算成MB吗?

无效数字。数字精确度限为 32 位。
平均数是524288000
请按任意键继续. . .

追答

你是32bit的windows?变量长度有限制了

@echo off
setlocal enabledelayedexpansion
dir *.bat /-c >temp.tmp
find "/" temp.tmp>temp2.tmp
del temp.tmp
for /f "delims=: tokens=2" %%i in (temp2.tmp) do (
set aa=%%i
set aa=!aa: =_!
echo !aa!>>temp.tmp
)
del temp2.tmp
set ncount=0
set nsum=0
for /f "delims=_ tokens=2" %%j in (temp.tmp) do (
set /a nsum=!nsum!+%%j/1048576
set /a ncount=1+!ncount!
)
del temp.tmp
set /a nsum=!nsum!/!ncount!
echo 平均数是!nsum!


请及时采纳

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-25
比文本abc.txt内容abcdefgh其赋值给某变量?
相似回答