Bat文件以MB为单位显示结果文件大小而不是字节

时间:2017-03-11 00:27:37

标签: batch-file

您好我正在试图弄清楚如何让我的结果以MB显示,这样它的可读性更强,而不是字节。此脚本用于监视服务器列表中的公用文件夹。

感谢您提供的任何帮助。我在谷歌搜索了很长一段时间,但我自己也无法解决这个问题。

ECHO     DATE: %DATE% > filecount.TXT
ECHO     TIME: %TIME% >> filecount.TXT
ECHO     USER: %USERNAME% >> filecount.TXT
ECHO COMPUTER: %COMPUTERNAME% >> filecount.TXT
ECHO. >> filecount.TXT
ECHO. >> filecount.TXT
ECHO. >> filecount.TXT
FOR /F "tokens=1" %%i in (servers.txt) DO (
 ECHO   %%i  ::: >> filecount.TXT 
 ECHO Counting %%i
 dir \\%%i\c$\folder1\folder2  | findstr "File(s)"  >> filecount.TXT 

 ECHO. >> filecount.TXT  
 ECHO. >> filecount.TXT  
)

:::::::::::::::::::::::: END SCRIPT :::::::::::::::::::::::::

4 个答案:

答案 0 :(得分:2)

您可以方便有效地使用ROBOCOPY来获取累积文件数和文件大小。它会自动将大值转换为千字节,兆字节或千兆字节(或可能是太字节数?)。

我把代码放在自己的子程序中获取文件数和文件大小。

我还通过将主代码块放在括号中并重定向一次来简化代码 - 它更容易编写,而且效率更高(更快)。

我还添加了代码,以便将tgmk和空白转换为TBGB,{{ 1}},MBKB。没有翻译,代码会更简单。

如果你的语言不是英语,我很确定代码必须改变。

B

答案 1 :(得分:1)

这是一个批处理函数,它将整数转换为人类可读的B / KB / MB / GB约定。它在最高位的右侧一位使用位掩码来检查最终值是否应该向上舍入。将其放在最终exit /bgoto :EOF下面的脚本底部:

:humansize <return_var> <int>
setlocal enabledelayedexpansion
set "unit=B"
set /a "mask = 512, roundup = 0, size = %~2"
for %%I in (KB MB GB) do (
    if !size! geq 1024 (
        set "unit=%%I"
        set /a "roundup = size & mask, size >>= 10"
    )
)
if %roundup% gtr 0 set /a "size += 1"
endlocal & set "%~1=%size% %unit%" & exit /b

使用示例:

call :humansize human 1536
rem // %human% now contains "2 KB"

call :humansize human 1535
rem // %human% now contains "1 KB"

请注意,批量数学仅限于32位有符号整数(2GB)。

答案 2 :(得分:0)

SET命令不能进行浮点数学运算,因此这在技术上是一种估计。此外,您只能使用32位整数。所以你可以做的最大字节大小是2147483647

@echo off
ECHO     DATE: %DATE% > filecount.TXT
ECHO     TIME: %TIME% >> filecount.TXT
ECHO     USER: %USERNAME% >> filecount.TXT
ECHO COMPUTER: %COMPUTERNAME% >> filecount.TXT
ECHO. >> filecount.TXT
ECHO. >> filecount.TXT
ECHO. >> filecount.TXT
FOR /F "tokens=1" %%i in (servers.txt) DO (
    ECHO   %%i  ::: >> filecount.TXT 
    ECHO Counting %%i
    FOR /F "tokens=1-4 delims= " %%G IN ('dir /-C \\%%i\c$\folder1\folder2 ^| findstr "File(s)"') DO (
        set size=%%I
        set bytes=%%J
        setlocal enabledelayedexpansion
        IF !size! GEQ 1048576 (
            set /a size/=1048576
            set bytes=MB
        )
        echo %%G %%H     !SIZE! !bytes! >> filecount.TXT  
        endlocal
    )
    ECHO. >> filecount.TXT  
    ECHO. >> filecount.TXT  
)
pause
:::::::::::::::::::::::: END SCRIPT :::::::::::::::::::::::::

答案 3 :(得分:0)

批处理脚本按文件夹中文件的大小和计数获取总和

附件是一个批处理脚本方法,它将为您提供KB,MB和GB以及脚本所需的所有其他详细信息。它将全部转到日志文件并相应地附加,同时仍然遍历服务器的文件列表,获取目录中文件的总和和计数。

批处理脚本

LogFileServerList变量放在首位。设置目录以从 FOR 循环中获取文件详细信息,作为通过call命令传递的参数,将其作为参数传递给动态 PowerShell 脚本构建进行计算的例程等。

@ECHO ON

SET LogFile=filecount.TXT
SET ServerList=Servers.txt

ECHO.........................................>> "%LogFile%"
ECHO DATE: %DATE%             >> "%LogFile%"
ECHO TIME: %TIME%             >> "%LogFile%"
ECHO USER: %USERNAME%         >> "%LogFile%"
ECHO COMPUTER: %COMPUTERNAME% >> "%LogFile%"
ECHO.                         >> "%LogFile%"

FOR /F "TOKENS=1" %%i IN (%ServerList%) DO (
    ECHO ~~~Counting %%~i~~~             >> "%LogFile%"
    CALL :PowerShellBuild "\\%%~i\c$\folder1\folder2"
)
ECHO.........................................>> "%LogFile%" 
EXIT
GOTO EOF
:::::::::::::::::::::::: END OF SCRIPT STILL :::::::::::::::::::::::::

:PowerShellBuild
SET TmpPSScript=%Temp%\~tmpFileCalc.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

ECHO $Folder  = "%~1"                >>"%TmpPSScript%"
ECHO $Files   = Get-ChildItem $folder>>"%TmpPSScript%"
ECHO $KBTotal = ($files ^| Measure-Object -Sum Length).Sum / 1kb >>"%TmpPSScript%"
ECHO $MBTotal = ($files ^| Measure-Object -Sum Length).Sum / 1mb >>"%TmpPSScript%"
ECHO $GBTotal = ($files ^| Measure-Object -Sum Length).Sum / 1gb >>"%TmpPSScript%"
ECHO $Count   = ($files ^| Measure-Object).count                 >>"%TmpPSScript%"

ECHO $KBTotal = [math]::Round($KBTotal, 2) >>"%TmpPSScript%"
ECHO $MBTotal = [math]::Round($MBTotal, 2) >>"%TmpPSScript%"
ECHO $GBTotal = [math]::Round($GBTotal, 2) >>"%TmpPSScript%"

ECHO ECHO "Count: $Count" ^| Out-File -encoding ascii -append "%LogFile%" >>"%TmpPSScript%"
ECHO ECHO "KB: $KBTotal" ^| Out-File -encoding ascii -append "%LogFile%"  >>"%TmpPSScript%"
ECHO ECHO "MB: $MBTotal" ^| Out-File -encoding ascii -append "%LogFile%"  >>"%TmpPSScript%"
ECHO ECHO "GB: $GBTotal" ^| Out-File -encoding ascii -append "%LogFile%"  >>"%TmpPSScript%"

:PowerShellExec
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"
GOTO EOF

我的测试示例脚本

enter image description here

<强>结果

........................................
DATE: Sat 03/11/2017             
TIME: 16:52:32.48             
USER: User         
COMPUTER: FBIPC

~~~Counting Clutter~~~             
Count: 15
KB: 2490.48
MB: 2.43
GB: 0
~~~Counting Photowork~~~             
Count: 7
KB: 372.88
MB: 0.36
GB: 0
........................................ 

原生PowerShell

使用原生 PowerShell 测试系统上的语法,以确认它在PowerShell ISE应用程序中的此级别工作,但它适用于我的 Windows 10 with PowerShell 5.0 Windows 7与PowerShell 4.0

$LogFile  = "C:\Users\User\Desktop\Scripts\logfile.log"
$Folder  = "C:\Users\User\Desktop\Scripts"

$Files   = Get-ChildItem $folder
$KBTotal = ($files | Measure-Object -Sum Length).Sum / 1kb
$MBTotal = ($files | Measure-Object -Sum Length).Sum / 1mb
$GBTotal = ($files | Measure-Object -Sum Length).Sum / 1gb
$Count   = ($files | Measure-Object).count

$KBTotal = [math]::Round($KBTotal, 2)
$MBTotal = [math]::Round($MBTotal, 2)
$GBTotal = [math]::Round($GBTotal, 2)

ECHO "Count: $Count" | Out-File -encoding ascii -append $Logfile
ECHO "KB: $KBTotal" | Out-File -encoding ascii -append $Logfile
ECHO "MB: $MBTotal" | Out-File -encoding ascii -append $Logfile
ECHO "GB: $GBTotal" | Out-File -encoding ascii -append $Logfile

更多资源