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-06-23 09:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Extract the line-by-line content of the file and perform operations. Problem! Please ask the experts to help solve it! View 4,037 Replies 24
Original Poster Posted 2006-06-07 09:31 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
How to extract a column of characters from each line in the file and perform the following algorithm. (Sum of lines of function shu down time) minus (Sum of lines of boot star time) such as 54883 + 54891 + 55849 - 54877 - 54887 - 55843 =?

for /f "delims=! tokens=2" %%i in (1.txt) do(??????????)
The following is file.1.txt!
boot star time 2006-06-06 星期二 15:14:37.57 record! 54877
shut down time 2006-06-06 星期二 15:14:43.27 record! 54883
boot star time 2006-06-06 星期二 15:14:47.66 record! 54887
shut down time 2006-06-06 星期二 15:14:51.36 record! 54891
boot star time 2006-06-06 星期二 15:30:43.18 record! 55843
shut down time 2006-06-06 星期二 15:30:49.99 record! 55849
Floor 2 Posted 2006-06-07 12:12 ·  中国 广东 广州 中移铁通
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
boot star time is a single line
shut down time is a double line
Then the following code can achieve the requirements of the landlord.

@echo off
set n=0
set sum=0
for /f "delims=! tokens=2" %%i in (1.txt) do (
call :s %%i
)
echo %sum%
goto :EOF

:s
set /A n+=1
if "%n%"=="1" (set /A sum-=%1)else set /A sum+=%1 & set n=0
goto :EOF


[ Last edited by doscc on 2006-6-7 at 12:15 ]
Floor 3 Posted 2006-06-07 17:13 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
Thanks!
Learned another thing again.!
GOTO:EOF is to return to which FOR statement, right.!
%1 is to inherit the %i of the FOR statement, right!.

If I want to implement the nesting of two for statements, to achieve, for example, for /f "delims=! tokens=2" %%i in (1.txt) do(......)
Define these variables to set1~set6 respectively. For example (set1=54877 ,set2=54883
set3=54887......)

I want to see how the variables of the two for statements are passed.!
Thanks
Floor 4 Posted 2006-06-07 17:30 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
It should be able to be more concise. The following code:
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a l=%%i+!l!
)
echo %l%
Floor 5 Posted 2006-06-07 17:42 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
re : bagpipe
set /a l=%%i+!l! is to accumulate six numbers. This does not match my proposal.
Floor 6 Posted 2006-06-08 10:23 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a n+=1
if !n!==1 (
set /a l=!l!-%%i
) else (
set /a l=!l!+%%i&set n=0
)
)
echo %l%
Weeping and leaving.........
Floor 7 Posted 2006-06-08 11:26 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
Floor 8 Posted 2006-06-08 11:59 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re: piziliu2004 (Floor 3):

Actually, your proposition doesn't need and is difficult to implement with nested loops, and the variable transfer between loops is not very complicated. Basically, it can be said that in the inner loop, you can directly use the variables of the outer loop, as long as it is not overwritten by the variables of the inner loop, which depends on the naming of the variables. For example:

for /l %%i in (1,1,5) do for %%j in (a,b,c,d) do echo.Counter:%%i%%j

As for what you mentioned about getting the file content line by line into environment variables, it doesn't seem to make much sense, because usually we all get a line of content and then immediately perform various processing and output it. But still, sample code is given for reference.


@echo off & setlocal EnableDelayedExpansion
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
set set!c!=%%i
)
set set
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2006-06-08 13:44 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
Thanks willsort
Moderator.
Little brother wants to ask a few questions
1:.set set is to display all current set variables? Why does my rem "set set" fail to set variables?

2: Also, why does inserting "echo %set!c!%" after set set!c!=%%i display a failure?
Floor 10 Posted 2006-06-08 14:07 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
1: set set will display the value of %set%!
2: After set set!c!=%%i, there cannot be other commands, otherwise all will become variables!
Floor 11 Posted 2006-06-08 14:40 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
Still a bit confused.
1: "set set" will display the value of %set%! Then why does just displaying %set% make the entire batch script unable to achieve the function of setting variables when using "set set"?
2: After "set set!c!=%%i", there cannot be other commands, otherwise they will all become variables! Why is that? Is it because of!c!?
3: By the way, why can't!c! be replaced with %c%?
Floor 12 Posted 2006-06-08 14:55 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 13 Posted 2006-06-08 15:12 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
re:qwe1234567
Moderator, thank you! I misunderstood.
This is how I judged. rem set set--> After the batch processing is executed and returns to the command line, ,--> I execute "set" but can't see the set1~setn variables.
After verification, it turns out that if I don't remove set set. I execute set and I still can't see the set1~setn variables.

1: Why can't I see the set1~n variables I set. Is it because of the batch processing setlocal EnableDelayedExpansion?

2: If I want the batch processing to end and then execute set under the dos command and still see the variables I set, how to achieve it?
Floor 14 Posted 2006-06-08 20:40 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── Moderation Record ─────────────────
Performed by: Will Sort
Operation: Delete topic: "21098 - If I want to implement the nested transfer of variables between two for statements. As the title says"
Explanation: The original topic and part of the content of the current topic are repeated
Punishment: Deduct 6 points of points awarded for posting this topic, deduct 2 points of points for the topic violation penalty
───────────────── Moderation Record ─────────────────


Re piziliu2004:

!c! is the usage of variable delayed expansion. There have been multiple explanations in this forum. It is recommended that you search first. In addition, you can refer to the command line help information of cmd /?.

The failure of set1~n is indeed due to setlocal. For specific details, please refer to setlocal /? and the relevant information in Windows Help and Support Center.

Usually, we don't need to keep the variable modifications in the batch file after the batch file ends, especially in the case of multiple variables. If you have such a need, it means that there is a certain defect in the program structure design. Of course, it is possible to keep variable modifications outside the batch file. The simplest way is not to use the setlocal command. If you must use it, such as using it to enable the variable delayed expansion feature, you need to use another trick.

I rarely use this trick. It is only used when it is difficult to avoid using setlocal and variables must be saved for external program reference. The method is to use the rarely used endlocal command. For example, if you want to keep the modification of variable envar before the program ends, you can insert the following statement at the end of the program.
endlocal & set envar=%envar%

For the code on floor 8, it is relatively more complicated because multiple variables with dynamic names need to be retained. The following code can be used.


@echo off & setlocal EnableDelayedExpansion
if exist _setvar.bat del _setvar.bat
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
echo set set!c!=%%i>>_setvar.bat
)
endlocal & if exist _setvar.bat (call _setvar.bat & del _setvar.bat)
set set


[ Last edited by willsort on 2006-6-8 at 20:44 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2006-06-10 15:28 ·  中国 山东 菏泽 电信
银牌会员
★★★
Credits 1,246
Posts 488
Joined 2003-11-11 00:00
22-year member
UID 12699
Gender Male
Status Offline
Bookmarked
Forum Jump: