How to count no of lines in text file and store the value into a variable using batch script?

前端 未结 16 1246
青春惊慌失措
青春惊慌失措 2020-11-27 14:28

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is

findstr /R         


        
16条回答
  •  忘掉有多难
    2020-11-27 15:14

    In the below code, the variable name are SalaryCount and TaxCount

    @ECHO OFF    
    echo Process started, please wait...    
    for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C    
    echo Salary,%SalaryCount%
    for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
    echo Tax,%TaxCount%
    

    Now if you need to output these values to a csv file, you could use the below code.

    @ECHO OFF
    cd "D:\CSVOutputPath\"
    echo Process started, please wait...
    echo FILENAME,FILECOUNT> SUMMARY.csv
    for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
    echo Salary,%Count%>> SUMMARY.csv
    for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
    echo Tax,%Count%>> SUMMARY.csv
    

    The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath

提交回复
热议问题