无效号码。数字限制为32位精度

时间:2015-10-04 04:51:35

标签: batch-file cmd numbers integer numeric

在网上找到这个脚本。它适用于我下载它的基本原因。但我想知道几件事:

  1. 当脚本尝试将字节划分为KB,MG和GB时,它会出错 无效号码。数字限制为32位精度。
  2. 档案总数&目录缺少右括号。
  3. CODE:

    c:
    cd\
    @Echo ------------------------------------------------------
    @Echo ------------------------------------------------------
    @For /F "tokens=*" %%a IN ('"hostname"') do @Set summaryouthost=%%a
    @Echo Integrity Check on: %summaryouthost%
    
    @For /F "tokens=*" %%a IN ('"chdir"') do @Set summaryoutchdir=%%a
    @Echo Integrity Check For the path: %summaryoutchdir%
    @Echo ------------------------------------------------------
    
    @For /F "tokens=*" %%a IN ('"date /T"') do @Set summaryoutsdate=%%a
    @Echo Start Date: %summaryoutsdate%
    
    @For /F "tokens=*" %%a IN ('"time /T"') do @Set summaryoutstime=%%a
    @Echo Start time: %summaryoutstime%
    
    @Echo ------------------------------------------------------
    @For /F "tokens=*" %%a IN ('"dir /s /-c | find "bytes" | find /v "free""')  do @Set summaryout=%%a
    ::@Echo %summaryout%
    @For /f "tokens=1,2 delims=)" %%a in ("%summaryout%") do @set filesout=%%a&set sizeout=%%b
    @For /f "tokens=*" %%a IN ('"dir /s /-c | find "bytes free""') do @Set summaryoutdir=%%a
    ::@Echo %summaryoutdir%
    @For /f "tokens=1,2 delims=)" %%a in ("%summaryoutdir%") do @set filesoutdir=%%a&set sizeoutdir=%%b
    @Set sizeout=%sizeout:bytes=%
    ::@Echo %sizeout%
    
    @Set sizeout=%sizeout: =%
    @set /A inbytes=%sizeout%
    @set /A inkb=(%sizeout%) / 1024
    @set /A inmb=(%sizeout%) / (1024*1024)
    @set /A ingb=(%sizeout%) / (1024*1024*1024)
    
    @For /F "tokens=*" %%a IN ('"date /T"') do @Set summaryoutedate=%%a
    @Echo End Date: %summaryoutedate%
    
    @For /F "tokens=*" %%a IN ('"time /T"') do @Set summaryoutetime=%%a
    @Echo End time: %summaryoutetime%
    
    @Echo ------------------------------------------------------
    @Echo Total Number of Files is:%filesout%
    @Echo Total Number of Directory is: %filesoutdir%
    @Set sizeoutdir=%sizeoutdir:bytes=%
    ::@Echo %sizeoutdir%
    @Set sizeoutdir=%sizeoutdir: =%
    @Echo ------------------------------------------------------
    @Echo Total Size is (BY) :%sizeout%
    @Echo Total Size is (KB) :%inkb%
    @Echo Total Size is (MB) :%inmb%
    @Echo Total Size is (GB) :%ingb%
    @Echo ------------------------------------------------------
    @Echo Free Space is :%sizeoutdir%
    @Echo ------------------------------------------------------
    @Echo ------------------------------------------------------
    

    输出:

    ------------------------------------------------------ 
    ------------------------------------------------------ 
    Integrity Check on: Laptop01 
    Integrity Check For the path: C:\ 
    ------------------------------------------------------ 
    Start Date: Sun 10/04/2015  
    Start time: 12:34 AM
    ------------------------------------------------------
    Invalid number. Numbers are limited to 32-bits of presision.
    Invalid number. Numbers are limited to 32-bits of presision.
    Invalid number. Numbers are limited to 32-bits of presision.
    Invalid number. Numbers are limited to 32-bits of presision.
    End Date: Sun 10/04/2015   
    End time: 12:36 AM  
    ------------------------------------------------------ 
    Total Number of Files is:333914 File(s 
    Total Number of Directory is: 185869 Dir(s 
    ------------------------------------------------------ 
    Total Size is (BY) :248159912076  
    Total Size is (KB) : 
    Total Size is (MB) : 
    Total Size is (GB) : 
    ------------------------------------------------------ 
    Free Space is :625324642304free  
    ------------------------------------------------------ 
    ------------------------------------------------------ 
    

    提前感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

数字限制为32位精度。

这是set /a的限制。

有关变通方法,请参阅下面的“变通方法:32位”。

  

算术表达式(SET / a)

     

...

     

数字必须都在32位有符号整数的范围内   数字(-2,147,483,648到2,147,483,647)来处理更大的数字   数字使用PowerShell或VBScript。

来源set

变通方法:32位

  

32位限制的变通方法包括:

     
      
  1. 通过砍掉最后的(3)数字
  2. 除以1000(或任何10的幂)   
  3. 将数字拆分为单独的十进制数字并执行所有数学运算并“手动”执行逻辑
  4.   
  5. 其他脚本语言
  6.         

    解决方法#1可用于添加磁盘空间:

               

    诀窍是每个(大)数被视为字符串,然后是   最右边的6个字符(数字)被切断,然后才会被切断   结果被视为一个数字。

         

    这是一个相当粗略的解决方法,因为它之前“舍入”所有数字   做数学。

         

    为每个子目录添加半个MegaByte(%Count%/ 2)到%Total%   但是,确实可以弥补截断,所以总数是   比单个数字更准确。

         

    请注意,这些数字不代表“真正的”MegaBytes(1024 x 1024)   但相当于百万字节(1000 x 1000)。

         

    解决方法#2完全由Brian Williams批量证明   文件:

               

    解决方法#3,其他脚本语言,不言自明。

来源Math in NT batch files

进一步阅读

答案 1 :(得分:0)

如果您需要为磁盘空间轻松作弊,可以通过对字符串进行子集化并除以近似值来进行更粗略的计算。像这样:

Attempted to load class "Customer" from the global namespace

%sizeout:〜0,-7%substrings sizeout,这是划分1.000.000的粗略方式,不处理任何舍入问题, 并且其中1074的计算值为1024 * 1024 * 1024 / 1.000.000

对于您需要的所有尺寸(KB和MB)也会这样做。

MB:Customer

KB:@set /A ingb=%sizeout:~0,-7% / (1074)

当然,测量单位越低,数字就会越来越不准确,但对于GB来说,使用它来监控机器上的磁盘空间是非常准确的。

我意识到所有这一切也可以从DavidPostill的答案中扣除,但我只是想带一个适用于这种特定情况的复制粘贴示例。

答案 2 :(得分:0)

更好的解决方案。

对于高达几GB的大小,精确到mb精度。

阅读代码中的注释以更好地理解。

@echo off
setlocal enabledelayedexpansion

:: Asign filesize to a string variable
set FileSizeString=%~z1

:: Get the lenght of the string
for /L %%x in (1,1,25) do ( if "!FileSizeString:~%%x!"=="" set /a "Lenght=%%x" & goto LenghtResult )

:LenghtResult 
rem If lenght of the filesize string is below or equal 9. Numbers: 0 - 999,999,999
if !Lenght! leq 9 (set /a SizeMb=!FileSizeString!/1024/1024
    rem If lenght of the filesize string is not below or equal 9 then remove its last 3 characters
    ) else (set FileSizeString=!FileSizeString:~0,-3!)

rem If lenght of the filesize string is equal 10. Numbers: 1,000,000,000 - 9,999,999,999
if !Lenght! equ 10 (
    rem If first character of the filesize string is equal 1. Numbers: 1,000,000,000 - 1,999,999,999
    if "!FileSizeString:~0,1!" equ "1" (set /a SizeMb=!FileSizeString!/1049
        rem If first character of the filesize string is not equal 1. Numbers: 2,000,000,000 - 9,999,999,999
        ) else (set /a SizeMb=!FileSizeString!/1074) )

rem If lenght of the filesize string is above or equal 11. Numbers: 10,000,000,000 - 999,999,999,999,999...
if !Lenght! geq 11 (set /a SizeMb=!FileSizeString!/1074)    

:: Displays file name and it's size in mb
echo.
echo File: %~nx1
echo Size: !SizeMb!mb

答案 3 :(得分:0)

以下将获得以gig为单位的总物理内存。

@echo off
Rem This will produce a file for total ram
    Wmic Computersystem get totalphysicalmemory > c:\temp\trash\allram1.txt
Rem This will produce a clean text file with true value
     type c:\temp\trash\allram1.txt > c:\temp\trash\allram3.txt
REm  The following will skip a line (header)
    (    for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("c:\temp\trash\allram3.txt") do echo(%%L) > "c:\temp\trash\allram4.txt"

Rem following will produce a clean file with actual number
    setlocal enabledelayedexpansion 
    set /p val=<c:\temp\trash\allram4.txt
    set newval=!val:~10,1!

rem it will find the new value wheter it is more than 10 gig
    echo !newval!   > c:\temp\trash\allram2.txt
rem This will determine if more than 10 gig is found (less than 10 gig) (more than 10 gig)
 findstr "off" c:\temp\trash\allram2.txt && (set  newval2=!val:~0,1!.!val:~1,1!) || (set newval2= !val:~0,2!)

rem if it is less than 10 gig it will have decimal for example 8.2gig or 17gig (if it is more 10 gig) 
echo !newval2!gig

答案 4 :(得分:0)

以下将获得以gig为单位的总物理内存。

@echo off
Rem This will produce a file for total ram <br>
Wmic Computersystem get totalphysicalmemory > c:\temp\trash\allram1.txt <br>
Rem This will produce a clean text file with true value <br>
type c:\temp\trash\allram1.txt > c:\temp\trash\allram3.txt<br>
REm  The following will skip a line (header)<br>
(    for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("c:\temp\trash\allram3.txt") do echo(%%L) > "c:\temp\trash\allram4.txt" <br>

Rem following will produce a clean file with actual number<br>
setlocal enabledelayedexpansion <br>
set /p val=<c:\temp\trash\allram4.txt <br>
set newval=!val:~10,1! <br>

rem it will find the new value wheter it is more than 10 gig <br>
echo !newval!   > c:\temp\trash\allram2.txt <br>
rem This will determine if more than 10 gig is found (less than 10 gig) (more than 10 gig) <br>
findstr "off" c:\temp\trash\allram2.txt && (set  newval2=!val:~0,1!.!val:~1,1!) || (set newval2= !val:~0,2!) <br>

rem if it is less than 10 gig it will have decimal for example 8.2gig or 17gig (if it is more 10 gig) <br>
echo !newval2!gig <br>