Windows 使用批处理脚本连接相同名称的内容并将每个内容的值相加

Windows 使用批处理脚本连接相同名称的内容并将每个内容的值相加,windows,batch-file,cmd,Windows,Batch File,Cmd,几年前,@Mofi帮助我创建了一个惊人的.bat脚本,对许多内容的值进行求和,并将其名称合并起来,得出它们的总和 请检查关于这个问题的旧答案,重点是外交部的答案 但我注意到了一个问题 小于$1,00的值的总和不正确 大于$99.999,99的值总和不正确,有时输出错误 一个例子: 使用要重新格式化的.txt文件: 04 VERO BANRICOMPRAS A PRAZO 651326

几年前,@Mofi帮助我创建了一个惊人的
.bat
脚本,对许多内容的值进行求和,并将其名称合并起来,得出它们的总和

请检查关于这个问题的旧答案,重点是外交部的答案

但我注意到了一个问题

  • 小于$1,00的值的总和不正确
  • 大于$99.999,99的值总和不正确,有时输出错误
  • 一个例子:

    使用要重新格式化的
    .txt
    文件:

                         04  VERO BANRICOMPRAS A PRAZO                         651326             885,92
                             VERO BANRICOMPRAS A VISTA                         038937             470,90
                             VERO CARTAO CREDITO                               100810           2.126,17
                             VERO CARTAO CREDITO                               100811           1.354,05
                             VERO CARTAO CREDITO                               100812             148,91
                             CIELO S/A CARTAO CREDITO                          084036              45,75
                             CIELO S/A CARTAO CREDITO                          084037              24,18
                             CIELO S/A CARTAO CREDITO                          084038             108,34
                             REDECARD S.A CARTAO CREDITO                       154346             163,81
                             VERO CARTAO DEBITO                                214591           1.338,66
                             VERO CARTAO DEBITO                                214592             556,16
                             VERO CARTAO DEBITO                                214593             113,96
                             CIELO S/A CARTAO DEBITO                           186651             491,87
                             CIELO S/A CARTAO DEBITO                           186652             678,53
                         05  VERO BANRICOMPRAS A PRAZO                         976373             504,55
                             VERO BANRICOMPRAS A VISTA                         440325             397,56
                             VERO CARTAO CREDITO                               496411             725,69
                             VERO CARTAO CREDITO                               496412             545,53
                             CIELO S/A CARTAO CREDITO                          488259             135,32
                             CIELO S/A CARTAO CREDITO                          488260              33,72
                             REDECARD S.A CARTAO CREDITO                       519807              22,81
                             VERO CARTAO DEBITO                                555957             619,03
                             VERO CARTAO DEBITO                                555958             452,29
                             CIELO S/A CARTAO DEBITO                           542472             946,15
                         06  VERO BANRICOMPRAS A PRAZO                         419616             257,17
                             VERO BANRICOMPRAS A VISTA                         771632             379,49
                             VERO CARTAO CREDITO                               818228           1.317,35
                             VERO CARTAO CREDITO                               818229             708,32
                             VERO CARTAO CREDITO                               818230              84,28
                             CIELO S/A CARTAO CREDITO                          809536              14,88
                             REDECARD S.A CARTAO CREDITO                       846159              51,57
                             VERO CARTAO DEBITO                                883969             229,31
                             VERO CARTAO DEBITO                                883970              85,83
                             CIELO S/A CARTAO DEBITO                           870136              49,18
                             CIELO S/A CARTAO DEBITO                           870137              42,28
                             RESGATE AUTOMATICO                                000000           6.200,00
                         07  VERO BANRICOMPRAS A PRAZO                         742635             148,84
                             VERO BANRICOMPRAS A VISTA                         135047             111,52
                             VERO CARTAO CREDITO                               178947             867,82
                             VERO CARTAO CREDITO                               178948              41,02
                             CIELO S/A CARTAO CREDITO                          169813             133,68
                             CIELO S/A CARTAO CREDITO                          169814              24,98
                             VERO CARTAO DEBITO                                244107             596,02
                             VERO CARTAO DEBITO                                244108             364,95
                             CIELO S/A CARTAO DEBITO                           229583             651,76
                         08  VERO BANRICOMPRAS A PRAZO                         085581             274,00
                             VERO BANRICOMPRAS A VISTA                         489251             950,36
                             VERO CARTAO CREDITO                               532717             601,15
                             VERO CARTAO CREDITO                               532718             488,93
                             CIELO S/A CARTAO CREDITO                          523033              69,76
                             CIELO S/A CARTAO CREDITO                          523034              58,36
                             CIELO S/A CARTAO CREDITO                          523035              34,30
                             REDECARD S.A CARTAO CREDITO                       562700             185,53
                             VERO CARTAO DEBITO                                603103           2.608,68
                             VERO CARTAO DEBITO                                603104           1.124,42
                             CIELO S/A CARTAO DEBITO                           588005           1.419,60
                         11  VERO BANRICOMPRAS A PRAZO                         451443             770,43
                             VERO BANRICOMPRAS A VISTA                         800736           1.377,51
                             VERO CARTAO CREDITO                               859775           1.673,38
                             VERO CARTAO CREDITO                               859776             836,50
                             VERO CARTAO CREDITO                               859777              27,42
                             VERO CARTAO CREDITO                               859778              39,24
                             CIELO S/A CARTAO CREDITO                          844066              26,44
                             CIELO S/A CARTAO CREDITO                          844067              45,37
                             CIELO S/A CARTAO CREDITO                          844068              54,34
                             REDECARD S.A CARTAO CREDITO                       910400             127,17
                             VERO CARTAO DEBITO                                972748           3.560,31
                             VERO CARTAO DEBITO                                972749           1.939,33
                             CIELO S/A CARTAO DEBITO                           944629             175,81
                             CIELO S/A CARTAO DEBITO                           944630           2.161,99
                             CIELO S/A CARTAO DEBITO                           944631              58,72
                             CIELO S/A CARTAO DEBITO                           944632           1.559,94
                             RESGATE AUTOMATICO                                000000          15.500,00
                         12  VERO BANRICOMPRAS A PRAZO                         742222             159,97
                             VERO BANRICOMPRAS A VISTA                         190951           1.247,06
                             VERO CARTAO CREDITO                               334660             669,75
                             VERO CARTAO CREDITO                               334661             263,14
                             VERO CARTAO CREDITO                               334662              99,13
                             CIELO S/A CARTAO CREDITO                          325837              17,40
                             CIELO S/A CARTAO CREDITO                          325838             161,58
                             REDECARD S.A CARTAO CREDITO                       358417             106,89
                             VERO CARTAO DEBITO                                394014             128,39
                             VERO CARTAO DEBITO                                394015             349,08
                             CIELO S/A CARTAO DEBITO                           381003             892,94
                             RESGATE AUTOMATICO                                000000           6.900,00
                             DEVOLUCAO DOC. ELETRONICO                         000060           3.949,49
    
                         13  VERO BANRICOMPRAS A PRAZO                         147127             136,58
                             VERO BANRICOMPRAS A VISTA                         564315             225,14
                             VERO CARTAO CREDITO                               917004             656,04
                             VERO CARTAO CREDITO                               917005              84,70
                             VERO CARTAO CREDITO                               917006             103,07
                             CIELO S/A CARTAO CREDITO                          907873             190,28
                             CIELO S/A CARTAO CREDITO                          907874              65,24
                             REDECARD S.A CARTAO CREDITO                       945528             155,34
                             VERO CARTAO DEBITO                                981014             488,95
                             VERO CARTAO DEBITO                                981015             112,82
                         14  VERO BANRICOMPRAS A PRAZO                         534959              19,21
                             VERO BANRICOMPRAS A VISTA                         123609             343,33
                             VERO CARTAO CREDITO                               174070             225,84
                             VERO CARTAO CREDITO                               174071             237,41
                             CIELO S/A CARTAO CREDITO                          164650              40,68
                             CIELO S/A CARTAO CREDITO                          164651              26,54
                             VERO CARTAO DEBITO                                237408             608,63
                             VERO CARTAO DEBITO                                237409           1.061,97
                             CIELO S/A CARTAO DEBITO                           224104             232,16
                         15  VERO BANRICOMPRAS A PRAZO                         091718             212,73
                             VERO BANRICOMPRAS A VISTA                         412318             528,61
                             VERO CARTAO CREDITO                               464462           1.255,15
                             VERO CARTAO CREDITO                               464463              82,57
                             VERO CARTAO CREDITO                               464464              72,84
                             CIELO S/A CARTAO CREDITO                          454334              25,47
                             CIELO S/A CARTAO CREDITO                          454335             204,90
                             CIELO S/A CARTAO CREDITO                          454336             218,38
                             REDECARD S.A CARTAO CREDITO                       495065              81,70
                             VERO CARTAO DEBITO                                532669             340,94
                             VERO CARTAO DEBITO                                532670              63,15
                             CIELO S/A CARTAO DEBITO                           518884             699,42
                         18  VERO BANRICOMPRAS A PRAZO                         383614             691,40
                             VERO BANRICOMPRAS A VISTA                         719756             955,41
                             VERO CARTAO CREDITO                               766119           3.335,37
                             VERO CARTAO CREDITO                               766120           1.460,17
                             VERO CARTAO CREDITO                               766121             637,74
                             CIELO S/A CARTAO CREDITO                          749384             185,38
                             REDECARD S.A CARTAO CREDITO                       818055             341,85
                             VERO CARTAO DEBITO                                876849           1.686,83
                             VERO CARTAO DEBITO                                876850             623,29
                             VERO CARTAO DEBITO                                876851              24,33
                             CIELO S/A CARTAO DEBITO                           852038             157,38
                             CIELO S/A CARTAO DEBITO                           852039             892,36
                             CIELO S/A CARTAO DEBITO                           852040             818,90
                         19  VERO BANRICOMPRAS A PRAZO                         660866             118,53
                             VERO BANRICOMPRAS A VISTA                         080453             244,18
                             VERO CARTAO CREDITO                               126719             533,66
                             VERO CARTAO CREDITO                               126720              95,94
                             VERO CARTAO CREDITO                               126721              47,87
                             CIELO S/A CARTAO CREDITO                          118299              46,64
                             CIELO S/A CARTAO CREDITO                          118300             137,53
                             REDECARD S.A CARTAO CREDITO                       149953              27,27
                             VERO CARTAO DEBITO                                184851           1.325,03
                             VERO CARTAO DEBITO                                184852             511,96
                             CIELO S/A CARTAO DEBITO                           171809             437,77
                         20  VERO BANRICOMPRAS A PRAZO                         044755             415,29
                             VERO BANRICOMPRAS A VISTA                         349028             493,91
                             VERO CARTAO CREDITO                               403626             673,75
                             VERO CARTAO CREDITO                               403627             342,59
                             VERO CARTAO CREDITO                               403628              58,28
                             REDECARD S.A CARTAO CREDITO                       433775              67,90
                             REDECARD S.A CARTAO CREDITO                       433776              67,61
                             VERO CARTAO DEBITO                                472002             297,51
                             VERO CARTAO DEBITO                                472003             131,56
                             RESGATE AUTOMATICO                                000000           9.700,00
                         21  VERO BANRICOMPRAS A PRAZO                         319063             305,54
                             VERO BANRICOMPRAS A VISTA                         679182             473,10
                             VERO CARTAO CREDITO                               724092             594,46
                             VERO CARTAO CREDITO                               724093             290,86
                             CIELO S/A CARTAO CREDITO                          716700             133,04
                             CIELO S/A CARTAO CREDITO                          716701             244,63
                             REDECARD S.A CARTAO CREDITO                       744273             109,34
                             VERO CARTAO DEBITO                                779495             461,80
                             VERO CARTAO DEBITO                                779496              61,27
                             CIELO S/A CARTAO DEBITO                           765528           1.369,41
                         22  VERO BANRICOMPRAS A PRAZO                         625121             272,86
                             VERO BANRICOMPRAS A VISTA                         947266             173,69
                             VERO CARTAO CREDITO                               049605             794,90
                             VERO CARTAO CREDITO                               049606             181,81
                             VERO CARTAO CREDITO                               049607             123,49
                             CIELO S/A CARTAO CREDITO                          040363             115,22
                             CIELO S/A CARTAO CREDITO                          040364              21,09
                             CIELO S/A CARTAO CREDITO                          040365             116,42
                             REDECARD S.A CARTAO CREDITO                       079047             156,19
                             VERO CARTAO DEBITO                                114585             404,29
                             VERO CARTAO DEBITO                                114586             199,87
                             CIELO S/A CARTAO DEBITO                           101926             262,24
                         25  VERO BANRICOMPRAS A PRAZO                         898216             619,60
                             VERO BANRICOMPRAS A VISTA                         263855             333,14
                             VERO CARTAO CREDITO                               656240           2.182,05
                             VERO CARTAO CREDITO                               656241           1.153,70
                             CIELO S/A CARTAO CREDITO                          641077              41,86
                             CIELO S/A CARTAO CREDITO                          641078              93,03
                             REDECARD S.A CARTAO CREDITO                       705335             156,27
                             VERO CARTAO DEBITO                                761345           1.229,09
    
                             VERO CARTAO DEBITO                                761346             307,53
                             CIELO S/A CARTAO DEBITO                           737468             290,03
                             CIELO S/A CARTAO DEBITO                           737469             412,93
                             RESGATE AUTOMATICO                                000000          74.100,00
                         27  VERO BANRICOMPRAS A PRAZO                         916537             254,76
                             VERO BANRICOMPRAS A VISTA                         463886             910,60
                             VERO CARTAO CREDITO                               027887             801,29
                             VERO CARTAO CREDITO                               027888             479,59
                             VERO CARTAO CREDITO                               027889              41,91
                             VERO CARTAO CREDITO                               224868             267,26
                             VERO CARTAO CREDITO                               224869             161,01
                             VERO CARTAO CREDITO                               224870             144,73
                             CIELO S/A CARTAO CREDITO                          019299              42,74
                             CIELO S/A CARTAO CREDITO                          019300              72,49
                             CIELO S/A CARTAO CREDITO                          217578             141,70
                             REDECARD S.A CARTAO CREDITO                       055906              22,87
                             REDECARD S.A CARTAO CREDITO                       244646              49,75
                             VERO CARTAO DEBITO                                279344             458,92
                             VERO CARTAO DEBITO                                279345             393,74
                             CIELO S/A CARTAO DEBITO                           266081             380,41
                             CIELO S/A CARTAO DEBITO                           266082             848,21
                         28  VERO BANRICOMPRAS A PRAZO                         427354             332,90
                             VERO BANRICOMPRAS A VISTA                         269379             232,29
                             VERO CARTAO CREDITO                               314994             912,53
                             VERO CARTAO CREDITO                               314995             536,39
                             CIELO S/A CARTAO CREDITO                          305504              33,87
                             CIELO S/A CARTAO CREDITO                          305505              56,26
                             REDECARD S.A CARTAO CREDITO                       344054              89,10
                             VERO CARTAO DEBITO                                381678             810,07
                             VERO CARTAO DEBITO                                381679             463,54
                             CIELO S/A CARTAO DEBITO                           367679             285,99
                         29  VERO BANRICOMPRAS A PRAZO                         228273             139,72
                             VERO BANRICOMPRAS A VISTA                         637529             499,77
                             VERO CARTAO CREDITO                               678028             831,50
                             VERO CARTAO CREDITO                               678029             318,86
                             CIELO S/A CARTAO CREDITO                          668143              38,70
                             REDECARD S.A CARTAO CREDITO                       708785              29,82
                             VERO CARTAO DEBITO                                746619           1.159,92
                             CIELO S/A CARTAO DEBITO                           732531             839,64
    
    一个内容的结果是:
    RESGATE AUTOMATICO 12.400,00

    但事实上,正确的输出应该是:
    RESGATE AUTOMATICO 112.400,00

    它缺少第一个数字

    这个脚本对于我的了解水平来说太复杂了,但在尝试阅读它之后,我猜问题出在脚本的这一部分:

    if not "%DataName:~2,1%" == " " goto CheckName
    if "%DataName:~0,1%" == ";" goto CheckName
    for /F "delims=01234567890" %%A in ("%DataName:~0,2%") do goto CheckName
    set "DataName=%DataName:~3%"
    

    在我的理解(可能是我以前说过的错误)中,在中间列中的<代码> 000000代码>使脚本理解删除第一个字符(12.400,而不是112.400)。


    有人可以更好地向我解释如何修复它?

    下面是解决这两个问题的增强批处理脚本

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "DataCount=0"
    set "MaxNameLength=19"
    set "TempFile1=%TEMP%\Output.tmp"
    set "TempFile2=%TEMP%\Sorted.tmp"
    set "OutputFile=Output.txt"
    
    "%ProgramFiles(x86)%\CoolUtils\Total PDF Converter\PDFConverter.exe" "%UserProfile%\Desktop\teste.oxps" "%UserProfile%\Desktop\teste.txt"
    del /F "%UserProfile%\Desktop\teste.pdf"
    %SystemRoot%\System32\findstr.exe /V /R /C:"http" /C:"Banrisul" /C:"Sac" /C:":" /C:"-" /C:"SAC" /C:"OUVIDORIA" /C:"B A N R I S U L" /C:"+" "%UserProfile%\Desktop\teste.txt" >"%TempFile1%"
    if not exist "%TempFile1%" goto EndBatch
    
    rem Process each line in the output file line by line with leading
    rem spaces and tabs already removed by FOR, but no other characters.
    for /F usebackq^ tokens^=*^ eol^= %%I in ("%TempFile1%") do (
        set "DataRow=%%~I"
        call :ProcessLine
    )
    goto FormatOutput
    
    
    :ProcessLine
    rem Remove all double quotes within the data row.
    set "DataRow=%DataRow:"=%"
    rem Remove all exclamation marks within the data row.
    set "DataRow=%DataRow:!=%"
    rem Remove all horizontal tabs within the data row by spaces.
    set "DataRow=%DataRow:  = %"
    rem Replace all $ by the string #DollarSign# within the data row.
    set "DataRow=%DataRow:$=#DollarSign#%"
    rem Replace two spaces in series by a dollar sign.
    set "DataRow=%DataRow:  =$%"
    rem Replace all occurrences of dollar sign plus space by dollar sign.
    set "DataRow=%DataRow:$ =$%"
    
    rem Split up the data row using dollar sign as delimiter which can result
    rem in four or just three tokenized substrings depending on existence of
    rem 01, 02, ... in first data column. Of interest is the name string in
    rem first or second data column and the value string in third or fourth
    rem data column.
    for /F tokens^=1-4^ delims^=$^ eol^= %%J in ("%DataRow%") do (
        if "%%M" == "" (
            set "DataName=%%~J"
            set "DataValue=%%L"
        ) else (
            set "DataName=%%~K"
            set "DataValue=%%M"
        )
    )
    
    rem It is safe now to replace the string #DollarSign# back to dollar sign.
    set "DataName=%DataName:#DollarSign#=$%"
    
    rem If the first 2 characters of data name are two digits and third
    rem character is a space then remove those 3 characters from data name.
    if not "%DataName:~2,1%" == " " goto CheckName
    if "%DataName:~0,1%" == ";" goto CheckName
    for /F "delims=01234567890" %%J in ("%DataName:~0,2%") do goto CheckName
    set "DataName=%DataName:~3%"
    
    rem Data names not containing one of the following four strings
    rem should be at end of the list and not included in the total sum.
    :CheckName
    if not "%DataName:DEBITO=%"  == "%DataName%" goto ReformatValue
    if not "%DataName:CREDITO=%" == "%DataName%" goto ReformatValue
    if not "%DataName:A VISTA=%" == "%DataName%" goto ReformatValue
    if not "%DataName:A PRAZO=%" == "%DataName%" goto ReformatValue
    set "DataName=z_%DataName%"
    
    rem Floating point arithmetic is not supported by Windows command processor,
    rem just signed 32-bit integer arithmetic with values in range -2147483648
    rem to 2147483647. Therefore remove all dots and commas from data value.
    :ReformatValue
    set "DataValue=%DataValue:.=%"
    set "DataValue=%DataValue:,=%"
    for /F "tokens=* delims=0" %%J in ("%DataValue%") do set "DataValue=%%J"
    if not defined DataValue set "DataValue=0"
    
    rem Add the data value as integer to total sum. There is no special
    rem overflow handling implemented yet in case of total sum exceeds
    rem the maximum 32-bit positive signed integer value 2147483647.
    if not "%DataName:~0,2%" == "z_" set /A "#TotalSum+=DataValue"
    
    rem Find out if a data value of current data name is already in list
    rem of environment variables and in this case just add the data value
    rem to the existing sum for data data name and exit the subroutine.
    for /F "tokens=1* delims==" %%J in ('set $\ 2^>nul') do if /I "%%K" == "%DataName%" set /A "#%%~nJ+=DataValue" & goto :EOF
    
    rem This is a new data name with its first data value. So set
    rem the appropriate environment variables for name and value.
    set /A DataCount+=1
    set "$\%DataCount%=%DataName%"
    set "#%DataCount%=%DataValue%"
    
    rem For a later aligned output find out the length of the data name and
    rem remember its length if being greater than longest data name up to now.
    rem z_ added at beginning of some data names must be always ignored for
    rem length of data name.
    set "NameLength=1"
    :GetNameLength
    set "DataName=%DataName:~1%"
    if not "%DataName%" == "" set /A "NameLength+=1" & goto GetNameLength
    if "%DataName:~0,2%" == "z_" set /A NameLength-=2
    if %NameLength% GTR %MaxNameLength% set "MaxNameLength=%NameLength%"
    
    rem Exit the subroutine.
    goto :EOF
    
    
    :FormatOutput
    rem Add 3 to maximum name length to have always at least 3 spaces
    rem between longest data name and the sum of the data values.
    set /A MaxNameLength+=3
    
    setlocal EnableDelayedExpansion
    rem Build a string consisting of spaces according to maximum name length.
    set "SpacesName="
    for /L %%I in (1,1,%MaxNameLength%) do set "SpacesName=!SpacesName! "
    
    rem Find out the maximum value which is usually the total sum, but
    rem could be also a different value like sum of RESGATE AUTOMATICO.
    set "MaximumValue=%#TotalSum%"
    setlocal EnableDelayedExpansion
    for /F "tokens=2 delims==" %%I in ('set #') do if %%I GTR !MaximumValue! set "MaximumValue=%%I"
    endlocal & set "MaximumValue=%MaximumValue%"
    
    rem Format value of maximum value.
    call :FormatNumber %MaximumValue%
    
    rem For a later aligned output find out the length of the maximum value.
    set "ValueLength=1"
    :GetValueLength
    set "DataValue=%DataValue:~1%"
    if not "%DataValue%" == "" set /A "ValueLength+=1" & goto GetValueLength
    
    rem Build a string consisting of spaces according to maximum name length.
    set "SpacesValue="
    for /L %%I in (1,1,%ValueLength%) do set "SpacesValue=!SpacesValue! "
    
    rem Output the data names with appropriate number of aligning spaces
    rem and the data sum for each data name formatted and with aligning
    rem spaces into the output file.
    set /A ExtraNameLength=MaxNameLength+2
    del "%TempFile1%" 2>nul
    (for /F "tokens=1* delims==" %%I in ('set $\ 2^>nul') do (
        set "DataName=%%J%SpacesName%"
        if not "!DataName:~0,2!" == "z_" (
            set "DataName=!DataName:~0,%MaxNameLength%!"
        ) else set "DataName=!DataName:~0,%ExtraNameLength%!"
        call :FormatNumber !#%%~nI!
        set "DataValue=%SpacesValue%!DataValue!"
        set "DataValue=!DataValue:~-%ValueLength%!"
        echo !DataName!!DataValue!
    ))>>"%TempFile1%"
    
    rem Sort the lines in output file according to name.
    %SystemRoot%\System32\sort.exe "%TempFile1%" /O "%TempFile2%"
    del "%TempFile1%" 2>nul
    
    rem 16 is the length of string CARDS OF MONTH with a space on both sides.
    set /A LineLength=MaxNameLength+ValueLength
    set /A HeadLength=(LineLength-16) / 2
    
    rem Build the heading for the output file with centered CARDS OF MONTH
    rem and create the output file with this heading as first line.
    set "HyphensHead="
    for /L %%I in (1,1,%HeadLength%) do set "HyphensHead=!HyphensHead!-"
    set "Heading=%HyphensHead% CARDS OF MONTH %HyphensHead%"
    set /A HeadLength=HeadLength*2 + 16
    if not %HeadLength% == %LineLength% set "Heading=%Heading%-"
    echo %Heading%>"%OutputFile%"
    
    set "TotalSumWrite=1"
    for /F usebackq^ tokens^=*^ eol^= %%I in ("%TempFile2%") do (
        set "DataRow=%%I"
        if defined TotalSumWrite (
            if not "!DataRow:~0,2!" == "z_" (
                echo !DataRow!>>"%OutputFile%"
            ) else (
                rem Append the total summary to the output file.
                set "TotalSumWrite="
                >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
                set "DataName=TOTAL OF ALL:%SpacesName%"
                set "DataName=!DataName:~0,%MaxNameLength%!"
                call :FormatNumber %#TotalSum%
                set "DataValue=%SpacesValue%!DataValue!"
                set "DataValue=!DataValue:~-%ValueLength%!"
                >>"%OutputFile%" echo !DataName!!DataValue!
                >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
                >>"%OutputFile%" echo !DataRow:~2!
            )
        ) else echo !DataRow:~2!>>"%OutputFile%"
    )
    
    if defined TotalSumWrite (
        >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
        set "DataName=TOTAL OF ALL:%SpacesName%"
        set "DataName=!DataName:~0,%MaxNameLength%!"
        call :FormatNumber %#TotalSum%
        set "DataValue=%SpacesValue%!DataValue!"
        set "DataValue=!DataValue:~-%ValueLength%!"
        >>"%OutputFile%" echo !DataName!!DataValue!
    )
    
    del "%TempFile2%" 2>nul
    endlocal
    goto EndBatch
    
    
    rem The subroutine below reformats 0 to 99 to 0,00 to 0,99 and inserts
    rem dots on larger values after a series of 3 digits left to the comma.
    
    :FormatNumber
    set "DataValue=%1"
    if "%DataValue:~1,1%" == "" set "DataValue=0%DataValue%"
    if "%DataValue:~2,1%" == "" set "DataValue=0%DataValue%"
    set "DataValue=%DataValue:~0,-2%,%DataValue:~-2%"
    if not "%DataValue:~6,1%" == "" set "DataValue=%DataValue:~0,-6%.%DataValue:~-6%"
    if not "%DataValue:~10,1%" == "" set "DataValue=%DataValue:~0,-10%.%DataValue:~-10%"
    goto :EOF
    
    :EndBatch
    endlocal
    
    插入以下两行是为了解决第一个问题,即范围
    0,08
    0,99
    的值在删除逗号后由于前导
    0
    而被错误解释为无效或有效的八进制数,而不是十进制数

    for /F "tokens=* delims=0" %%J in ("%DataValue%") do set "DataValue=%%J"
    if not defined DataValue set "DataValue=0"
    
    值中包含数字
    8
    9
    0,08
    0,99
    范围内的所有值被解释为无效的八进制数,导致在算术表达式中使用值
    0

    这两行从数据值中删除所有前导的
    0
    ,如果由于值为
    0,00
    ,循环后不再定义数据值,则环境变量
    DataValue
    将用值
    0
    重新定义

    for /F "tokens=* delims=0" %%J in ("%DataValue%") do set "DataValue=%%J"
    if not defined DataValue set "DataValue=0"
    
    我还将此修复应用于发布在

    第二个问题是,
    RESGATE AUTOMATICO
    的输出和错误,原因是:

    rem Format value of total sum.
    call :FormatNumber %#TotalSum%
    
    rem For a later aligned output find out the length of the total
    rem sum which is expected to be the greatest value in output.
    
    所提供的数据示例没有实现总和始终是输出中最大值的期望,因为总和是
    98.847,01
    ,而
    RESGATE AUTOMATICO
    的和是
    112.400,00
    ,具有更多字符

    此问题已通过以下方式解决:

    rem Find out the maximum value which is usually the total sum, but
    rem could be also a different value like sum of RESGATE AUTOMATICO.
    set "MaximumValue=%#TotalSum%"
    setlocal EnableDelayedExpansion
    for /F "tokens=2 delims==" %%I in ('set #') do if %%I GTR !MaximumValue! set "MaximumValue=%%I"
    endlocal & set "MaximumValue=%MaximumValue%"
    
    rem Format value of maximum value.
    call :FormatNumber %MaximumValue%
    
    rem For a later aligned output find out the length of the maximum value.
    

    还有一些不影响输出的其他小改进。

    关于
    0,99
    和更少的数字的问题,我很确定,领先的
    0
    会导致将值误解为八进制值的问题;有一个相当长的行包含值的总和,
    set/a“#%%nA+=DataValue”
    ;在这一行之前,您需要确保
    DataValue
    中没有前导零;尝试插入以下内容:
    (对于/F“tokens=*delims=0 eol=0”%%%Z in(“%DataValue%”)。设置“DataValue=%%%Z”)&设置/A“DataValue+=0”
    。对于另一个关于大数字的问题,我没有得到一个建议……你所做的令人惊讶。它真的很好用!做了几次测试,一切都如我所料。像你这样的人,像这里的每个人一样,花自己的时间让别人的工作更轻松,这是非常好的。每次我搜索一些东西,找到你的答案,我知道我可以放心地相信,我从来没有遇到过问题。你是一个了不起的程序员!非常感谢。