China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 22:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to sum the 20th number in the 10th line of all txt files View 3,895 Replies 20
Floor 16 Posted 2006-09-10 08:42 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
I don't know how to say it, I'm embarrassed. Anyway, it's just that after a program runs, it can add the first number in the 10th line of the txt files in the directory. For example:
1: Anonymous 48 321 166.84 100.00% 0 0.00 NaN%
Just add 48, and the following numbers don't need to be cared about.
I mean, for different txt files, just add the numbers in the 48 position.
1: Anonymous 148 321 166.84 100.00% 0 0.00 NaN%
1: Anonymous 48 321 166.84 100.00% 0 0.00 NaN%
1: Anonymous 1148 321 166.84 100.00% 0 0.00 NaN%
The above three txt files are extracted, and the positions of 8 are all the same in one column. After the program runs, calculate the sum of 148 + 48 + 1148.
I have low education, so it's possible that my Chinese level is not enough, sorry.
Floor 17 Posted 2006-09-10 09:25 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
By comparing the file you uploaded, I found that there is a space after ":", so ":" should be in the first column of the text, and numbers like 148 are in the third column. To solve your problem, you can try the following code:

@echo off
setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set file=%%i
set count=0
for /f "skip=9 tokens=3" %%j in (!file!) do (
set /a count+=1
if !count! equ 1 set /a sum+=%%j
)
)
echo The sum result is %sum%
pause


[ Last edited by namejm on 2006-9-10 at 09:28 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 18 Posted 2006-09-10 09:38 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
Thank you, it's already solved. I really didn't know what the concept of a column is. Can you help explain it? Thanks
Floor 19 Posted 2006-09-10 10:34 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
As the saying goes, horizontally and vertically. In a text file, the text in the horizontal direction is called a line, and lines are separated by carriage return symbols; the text in the vertical direction separated by spaces or tabs is called a column, as shown below (assuming that Arabic numerals are the content of the text), 1 is in the first row and first column, and 6 is in the second row and third column:

第 第 第
一 二 三
列 列 列

First line: 1 2 3
Second line: 4 5 6
Third line: 7 8 9
……


[ Last edited by namejm on 2006-9-10 at 10:37 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 20 Posted 2006-09-10 10:42 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
Hehe, thank you. This forum is really good
Floor 21 Posted 2006-09-10 10:59 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
There is another question. If these txt files are in a certain directory on another machine in the local area network, can I run a program on my own machine to calculate the sum of the numbers in the txt files under the certain directory on that machine? That machine has the default permissions of opening admin$ and ipc$.
Forum Jump: