首页 > 解决方案 > 使用 .bat 命令对 .txt 文件的值求和

问题描述

我在下面有这个脚本:

set "$file=futuroscielotan.txt"
set "$search=Previsto"

set $repl[2]=TOTAL       
set $repl[3]=Visa        
set $repl[4]=Mastercard  
set $repl[5]=Amex        
set $repl[6]=Sorocred    
set $repl[7]=Elo         
set $repl[8]=Diners      
set $repl[9]=Agiplan     
set $repl[10]=Banescard   
set $repl[11]=Cabal       
set $repl[12]=Credsystem  
set $repl[13]=Hipercard   
set $repl[14]=Credz       
set $repl[15]=Hiper       

setlocal enabledelayedexpansion
set $count=1

(for /f "delims=" %%a in (%$file%) do (
   call:replace "%%a" !$count!
   set/a $count+=1
   )
)> "Futuros Cielo Tan.txt"
echo. >> "Futuros Cielo Tan.txt"
echo ---  FUTUROS CIELO TAN --- >> "Futuros Cielo Tan.txt"

mkdir "C:\Users\lojas\Desktop\Extratos Banco" 
move "Futuros Cielo Tan.txt" "C:\Users\lojas\Desktop\Extratos Banco\Futuros Cielo Tan.txt"

exit/b

:replace
set "$line=%~1"
set $repl=!$repl[%2]!
set "$line=!$line:%$search%=%$repl%!"
echo !$line!
if "%2"=="1" echo.
if "%2"=="2" echo.

该脚本的输出.txt文件如下:

01/06/2018 à 20/03/2019

TOTAL       :  R$ 22.250,74  

Visa        :  R$ 1.086,51  
Mastercard  :  R$ 492,63  
Amex        :  R$ 0,00  
Sorocred    :  R$ 20.109,98  
Elo         :  R$ 0,00  
Diners      :  R$ 0,00  
Agiplan     :  R$ 0,00  
Banescard   :  R$ 0,00  
Cabal       :  R$ 0,00  
Credsystem  :  R$ 0,00  
Hipercard   :  R$ 0,00  
Credz       :  R$ 0,00  

---  FUTUROS CIELO TAN --- 

但我需要一种方法来总结卡片的价值并创建另一条名为TOTAL VALUE OF CARDS CHECKED: xxxxxx

它必须如下所示:

01/06/2018 à 20/03/2019

TOTAL       :  R$ 22.250,74  

Visa        :  R$ 1.086,51  
Mastercard  :  R$ 492,63  
Amex        :  R$ 0,00  
Sorocred    :  R$ 20.109,98  
Elo         :  R$ 0,00  
Diners      :  R$ 0,00  
Agiplan     :  R$ 0,00  
Banescard   :  R$ 0,00  
Cabal       :  R$ 0,00  
Credsystem  :  R$ 0,00  
Hipercard   :  R$ 0,00  
Credz       :  R$ 0,00  

TOTAL CARD VALUES CHECKED: R$ 21.689,12

---  FUTUROS CIELO TAN --- 

我该怎么做?


刚刚尝试制作另一个命令仅用于测试。关注以下:

for /f "tokens=1 delims=R$ " %%a in (Futuros Cielo Tan.txt) do set /a total+=%%a
echo TOTAL CHECKED:  %total% >test.txt

但这不是求和。它总是放一个0,像这样:TOTAL CHECKED: 0

标签: batch-file

解决方案


凯伦,给定输入文件,futuroscielotan.txt,如先前在您已删除的问题中提供的那样:

01/06/2018 à 20/03/2019
Previsto:  R$ 22.250,74  
Previsto:  R$ 1.086,51  
Previsto:  R$ 492,63  
Previsto:  R$ 0,00  
Previsto:  R$ 20.109,98  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  
Previsto:  R$ 0,00  

您问题中的脚本可能如下所示(未经测试)

@Echo Off
SetLocal EnableDelayedExpansion

Set "$file=futuroscielotan.txt"
Set "$search=Previsto"
Set "$outdir=C:\Users\lojas\Desktop\Extratos Banco"
Set "$outfile=Futuros Cielo Tan.txt"

Set "$repl[2]=TOTAL       "
Set "$repl[3]=Visa        "
Set "$repl[4]=Mastercard  "
Set "$repl[5]=Amex        "
Set "$repl[6]=Sorocred    "
Set "$repl[7]=Elo         "
Set "$repl[8]=Diners      "
Set "$repl[9]=Agiplan     "
Set "$repl[10]=Banescard   "
Set "$repl[11]=Cabal       "
Set "$repl[12]=Credsystem  "
Set "$repl[13]=Hipercard   "
Set "$repl[14]=Credz       "
Set "$repl[15]=Hiper       "

If Not Exist "%$outdir%\" (MD "%$outdir%" 2>Nul || Exit /B)

Set /A $count=1,$real=$cent=0
(
    For /F "UseBackQ Delims=" %%A In ("%$file%") Do (
        Call :Replace "%%A" "!$count!"
        Set /A $count+=1
    )
    If Not "!$cent:~,-2!"=="" Set /A $real=$real+!$cent:~,-2!
    If Not "!$real:~,-3!"=="" Set "$real=!$real:~,-3!.!$real:~-3!"
    If Not "!$real:~,-7!"=="" Set "$real=!$real:~,-7!.!$real:~-7!"
    If Not "!$real:~,-11!"=="" Set "$real=!$real:~,-11!.!$real:~-11!"
    Set "$cent=0!$cent!"
    Echo(
    Echo TOTAL CARD VALUES CHECKED:  R$ !$real!,!$cent:~-2!
    Echo(
    Echo ---  FUTUROS CIELO TAN ---
)>"%$outdir%\%$outfile%"

Pause
Exit /B

:Replace
Set "$line=%~1"
If %~2 GEq 3 Call :Calcs !$line:*$=!
Set "$repl=!$repl[%~2]!"
Set "$line=!$line:%$search%=%$repl%!"
Echo %$line%
If %~2 Equ 1 Echo(
If %~2 Equ 2 Echo(
Exit /B

:Calcs
If %1 Gtr 0 (Set "$_r=%1"
    Set "$_r=!$_r:.=!"
    Set /A $real+=$_r)
If %2 Gtr 0 (Set /A $_c=10%2%%100
    Set /A $cent+=$_c)
Exit /B

推荐阅读