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 21:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to sum the 20th number in the 10th line of all txt files View 3,892 Replies 20
Original Poster Posted 2006-09-10 00:04 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
May I ask how to sum up the 20th character (which must be an Arabic numeral) in the 10th line of all txt files in a directory and display the total value in the cmd window? I don't know if it's possible, thank you.

[ Last edited by namejm on 2007-2-5 at 10:34 AM ]
Floor 2 Posted 2006-09-10 01:13 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Put the following code to run in this directory, and make sure that only the txt files involved in the operation exist in this directory:

@echo off
set num=0
setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set file=%%i
for /f "tokens=1* delims=:" %%j in ('findstr /n . !file!') do (
set var=%%k
if %%j equ 10 (set num_=!var:~19,1!)&& set /a num=!num!+!num_!
)
)
echo The result after addition is: %num%
pause


[ Last edited by namejm on 2006-9-10 at 06:13 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-09-10 01:48 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
Thank you, I ran your program for trial. Originally, I didn't make it clear before. It must be line 10, but I don't know which character it is, because it seems that this number has to cross 2 or 3 characters.

I uploaded several txt files, and the numbers inside range from ten-digit to thousand-digit. Thank you for helping me take a look
Attachments
新建文件夹.rar (743 bytes, Credits to download 1 pts, Downloads: 17)
Floor 4 Posted 2006-09-10 01:51 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
@echo off&setlocal
for %%a in (*.txt) do (set c=)&call :ok "%%a"
:ok
for /f "skip=9 usebackq tokens=*" %%x in (%1) do (
if not defined c set "c=%%x"
call set /a pp=pp+%%c:~19,1%%
goto :eof
)
echo %pp%

Use another method to complete!!! But I don't know how it works??? Hehe..........
我今后在论坛的目标就是做个超级坏人!!!
Floor 5 Posted 2006-09-10 02:03 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, the code of pip directly gets the content of line 10, which is much more efficient than my code. I didn't notice the efficiency issue, which is a big flaw in my code.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-09-10 02:07 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by lulala at 2006-9-10 01:48:
Thank you. I ran your program for trial. I didn't make it clear before. It must be line 10, but I don't know which character. Because it seems that this number spans 2 or 3 characters.

I uploaded several txt files, ...


  I read your txt and found that the numbers are displayed in columns. This is even easier, but I don't know which column of numbers you want to add. Please specify.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-09-10 02:11 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
Namejm, you are being too modest. Your position in the union is unshakable. My admiration for you is like a surging river that goes on and on without stopping!
我今后在论坛的目标就是做个超级坏人!!!
Floor 8 Posted 2006-09-10 05:49 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
Thanks to the several brothers upstairs

I want to add all of them, no matter how many columns, like 243+1246+21....
Floor 9 Posted 2006-09-10 06:15 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Oh, I'm dizzy. Regardless of 3721, just add all the numbers? This is super BT.

You always have to specify which columns' contents to add. Otherwise, what's the point of your operation?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-09-10 06:37 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
It's not that we add up no matter how many columns. I mean, in the txt file in the compressed package I uploaded, there are all three forms, namely ten-digit, hundred-digit, thousand-digit. The 10th line in a txt file may be ten-digit, or hundred-digit, or thousand-digit, but there is always a number, and the position of this number is basically fixed. I found that the last digit (units digit) of the numbers in several txt files is always in a fixed character position.

For example, in 1.txt, the 10th line is 123; in 2.txt, the 10th line is 1234; in 3.txt, the 10th line is 34. The positions of the units digits 3, 4, 4 above should be aligned.

Thank you for your enthusiastic help!!! :P
Floor 11 Posted 2006-09-10 06:48 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
This can ignore where the number is in the character position, you just need to tell me which column of numbers to add—you can't even not understand the meaning of a column, because I find that you haven't always told me which column to process, but always talking about a certain character position. And the content you exemplified doesn't match the uploaded text content, I can't know your real meaning—you'd better give examples based on the content of the file you uploaded.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-09-10 07:56 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
Floor 13 Posted 2006-09-10 08:00 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
You just need to tell me which column's numbers to add. The numbers are random, and they might be ten-digit, hundred-digit, or thousand-digit. How can I determine which column's numbers to add?
Floor 14 Posted 2006-09-10 08:04 ·  中国 上海 黄浦区 电信
初级用户
Credits 44
Posts 17
Joined 2006-09-05 13:00
19-year member
UID 61900
Status Offline
And the content you exemplified does not match the uploaded text content. The form is the same, just changing a number. There are these txt files in the directory. There is no regulation that there must be several hundred-digit numbers, several thousand-digit numbers, and several single-digit numbers, but their position, that is, the position of the single-digit number, is always fixed in the 10th line of this txt file. I can't count exactly which column it is fixed in because I don't know where to start counting the first column when making the program.
Floor 15 Posted 2006-09-10 08:16 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by lulala at 2006-9-12 07:56:
Character position = column

  Oh my god, is there such an equation? If you think so, then I really can't help you.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: