批处理文件,以从文本文件总结数字和编写总值为文本文件 [英] Batch file to sum numbers from text files and write that total value to text file

查看:123
本文介绍了批处理文件,以从文本文件总结数字和编写总值为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件夹1 300文本文件和300的文本文件中的文件夹2

下面是一个文本文件的样本含量

 印度,汽车,10
英国,汽车,20
我们的车,50

我想总结的第3列和写入总数达到同一个文本文件。
例如,

  FOR / F令牌= 3 delims =%%一中(文件夹1 \\ textfile1.txt)并设置/总+ = %%一
回声%的总%GT;文件夹1 \\ textfile1.txt

以上code会写在textfile1.txt 80

请告诉如何为所有使用批处理文件300的文本文件做同样的。


解决方案

 关闭@echo
为%% f由于(
  文件夹1 \\ *。TXT
  文件夹2 \\ *。TXT
)做(
  集/总= 0
  FOR / F令牌有usebackq = 3 delims =%%一中(%% F)并设置/总+ = %%一
  回声%的总%GT;%% F

要点:

1)每个要更换的文件(许多线)与含有总一行的内容。如果您想preserve原始数据和总追加到年底,那么你需要使用>> 而不是:

 回声%的总%GT;>中%% F

2)最大整数值,批次可以计算为2,147,483,647。的总和必须小于或等于该数字为每个文件,否则它将会失败。如果有可能,你总不能超过该值,那么你将不得不使用一些其他的语言。也许JScript中,VBScript中,或PowerShell的。

i have 300 text files in folder1 and 300 text files in folder2

below is a sample content of one text file

india,car,10
uk,car,20
us,car,50

i want to sum the 3rd column and write that total to same text file. example,

for /f "tokens=3 delims=," %%a in (folder1\textfile1.txt) do set /a total+=%%a
echo %total% >folder1\textfile1.txt 

the above code will write 80 in textfile1.txt

please tell how to do the same for all the 300 text files using a batch file.

解决方案

@echo off
for %%F in (
  folder1\*.txt
  folder2\*.txt
) do (
  set /a total=0
  for /f "usebackq tokens=3 delims=," %%a in ("%%F") do set /a total+=%%a
  echo %total% >"%%F"
)

Important points:

1) You are replacing the content of each file (many lines) with a single line containing the total. If you want to preserve the original data and append the total to the end then you need to use >> instead:

echo %total% >>"%%F"

2) The maximum integer value that batch can compute is 2,147,483,647. The sum total must be less than or equal to that number for each file, else it will fail. If it is possible that your total can exceed that value then you will have to use some other language. Perhaps JScript, VBScript, or PowerShell.

这篇关于批处理文件,以从文本文件总结数字和编写总值为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆