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-24 04:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » In the for statement, if you want to handle several lines in the middle, how to write the skip? View 2,615 Replies 20
Original Poster Posted 2007-09-16 19:22 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
Let's discuss this problem. If in a for statement you need to handle the middle several lines of the output of a certain command, how should it be handled? For example, skip=n skips the first n lines. If you also need to skip the last several lines at the same time, then how should this skip be expressed...
Floor 2 Posted 2007-09-16 21:38 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
You can use a goto command to jump out of the for command after handling the intermediate lines. You can also use if + for to handle it.
Floor 3 Posted 2007-09-16 22:30 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
May I ask how you determine exactly which position it jumps out to on the second floor? Can you give a specific example?
Floor 4 Posted 2007-09-16 23:21 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
For example, I now have an ok.txt file with the following content:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
10 10 10 10 10
11 11 11 11 11

Now I only display the content from the third to the seventh line, and I can use the following:
@echo off
rem Process lines from the third to the seventh
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n .* ok.txt') do (
echo Line %%a -- %%b
if -%%a==-7 goto :end
)
:end
echo the end
pause
Floor 5 Posted 2007-09-17 09:01 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
The example upstairs is a bit complicated, so it is specially changed as follows:
-----------------------------------------------------------------------------------------------
@echo off
rem Process lines 3 to 7
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n . ok.txt') do (
echo Line %%a -- %%b
if %%a==7 goto :end
)
:end
echo the end
pause
-------------------------------------------------------------------------------------------------

[ Last edited by renrenrenshk on 2007-9-17 at 09:04 AM ]
Floor 6 Posted 2007-09-17 09:12 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
The method above is not very flexible. For example, the number of lines in the output of the dir command often changes (varies with different folders), only the first and last few lines are unchanged. If you want to process all lines except those few unchanged ones, the above method is not suitable.

Attachment:
C:\>dir /a-d /-c
Volume in drive C is not labeled.
Volume Serial Number is 98C6-B5CC

Directory of C:\

2006-09-22 11:32 0 AUTOEXEC.BAT
2006-09-22 11:25 211 boot.ini
2004-08-17 20:00 322730 bootfont.bin
2006-09-22 11:32 0 CONFIG.SYS
2006-08-12 19:30 0 dfinstall.log
2007-09-17 08:17 266919936 hiberfil.sys
2006-09-22 11:32 0 IO.SYS
2006-09-22 11:32 0 MSDOS.SYS
2004-08-17 20:00 47564 NTDETECT.COM
2004-02-17 14:49 285344 ntldr
2007-09-17 08:28 206 ok.txt
2007-09-17 08:17 402653184 pagefile.sys
2007-09-17 08:18 8871936 Persi0.sys
2006-09-24 11:19 27214 _NavCClt.Log
14 File(s) 679128325 bytes
0 Dir(s) 11131838464 bytes free

At this time, if you want to process all lines except the first 5 lines and the last 2 lines, the above method won't work. I wonder if any expert can provide a better method.

[ Last edited by renrenrenshk on 2007-9-17 at 09:15 AM ]
Floor 7 Posted 2007-09-17 10:30 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
for /f "delims=" %%i in ('dir /a-d /-c^|findstr /r "*--"') do echo %%i

Only for the question on floor 6

In fact, sed can solve the problem of the original poster very well

[ Last edited by wudixin96 on 2007-9-17 at 10:31 AM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
6692836 +2 2007-09-17 16:28
renrenrenshk +1 2007-09-17 18:01
Floor 8 Posted 2007-09-17 10:42 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Process specified line:
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set m=!m:*:=!
if !mn! GEQ 3 if !mn! LEQ 7 echo.!m!>>test2.txt
endlocal

[ Last edited by terse on 2007-9-18 at 12:30 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
6692836 -2 2007-09-17 16:27
Floor 9 Posted 2007-09-17 15:58 ·  中国 河北 保定 联通
初级用户
Credits 44
Posts 21
Joined 2007-09-12 22:27
18-year member
UID 97221
Gender Male
Status Offline
The program on floor 4 is okay. I've learned it. Could you explain the role of delims=: in the for statement and the syntax of findstr /n .* ok.txt? I don't understand. Thanks.

[ Last edited by 6692836 on 2007-9-17 at 04:26 PM ]
Floor 10 Posted 2007-09-17 16:30 ·  中国 河北 保定 联通
初级用户
Credits 44
Posts 21
Joined 2007-09-12 22:27
18-year member
UID 97221
Gender Male
Status Offline
Sorry, I clicked by mistake.
Floor 11 Posted 2007-09-17 16:39 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by 6692836 at 2007-9-17 15:58:
The program on floor 4 is okay. Learned it
Can you explain the role of delims=: in the for statement and the syntax of findstr /n .* ok.txt
I don't understand. Thanks

[ Last edited by 6692836 on 2007-9-17 at 04:26 PM ]

Take a look at the help of the findstr command, see what the /n parameter does, and you will understand why the for here uses "delims=:".
Floor 12 Posted 2007-09-17 16:42 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
I don't have the permission to rate 2 points, so I can't help you. 6692836
Floor 13 Posted 2007-09-17 17:50 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
7th floor: Well written, very incisive!!!!!
-----------------------------------

The following is my personal view on the 9th and 4th floors... Not malicious attacks. Don't misunderstand.

We are not saying whether there is a problem with the code on the 4th floor, but that the code on the 4th floor is not incisive enough. There are some redundant things in the code that make it difficult to understand. Just like when writing a program, we need to pay attention to algorithm optimization and readability. The code on the 4th floor has made these two mistakes...

[ Last edited by renrenrenshk on 2007-9-17 at 06:40 PM ]
Floor 14 Posted 2007-09-17 18:16 ·  中国 福建 厦门 电信
初级用户
Credits 39
Posts 15
Joined 2007-09-16 09:11
18-year member
UID 97510
Gender Male
From 福建
Status Offline
Although the problem of the搂住 can be solved through various ways, if the skip in the for statement can complete this function, then it should be simpler to implement... I don't know if skip has this function... Waiting for experts to guide...

[ Last edited by renrenrenshk on 2007-9-17 at 06:17 PM ]
Floor 15 Posted 2007-09-17 18:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by renrenrenshk at 2007-9-17 09:12:
The method upstairs is not very flexible. For example, the number of lines in the output of the dir command often changes (varies with different folders), only the first and last few lines are unchanged. If you want to process those except the unchanged few...

Brother, in the post before building 4, which point did you mention about the dir command?
Forum Jump: