中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-27 16:17
47,813 topics / 349,918 posts / today 0 new / 48,276 members
DOS批处理 & 脚本技术(批处理室) » In the for statement, if you want to handle several lines in the middle, how to write the skip?
Printable Version  3,173 / 20
Floor1 renrenrenshk Posted 2007-09-16 19:22
初级用户 Posts 15 Credits 39 From 福建
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...
Floor2 lxmxn Posted 2007-09-16 21:38
版主 Posts 4,938 Credits 11,386
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.
Floor3 renrenrenshk Posted 2007-09-16 22:30
初级用户 Posts 15 Credits 39 From 福建
May I ask how you determine exactly which position it jumps out to on the second floor? Can you give a specific example?
Floor4 lxmxn Posted 2007-09-16 23:21
版主 Posts 4,938 Credits 11,386
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:
Floor5 renrenrenshk Posted 2007-09-17 09:01
初级用户 Posts 15 Credits 39 From 福建
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 ]
Floor6 renrenrenshk Posted 2007-09-17 09:12
初级用户 Posts 15 Credits 39 From 福建
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 ]
Floor7 wudixin96 Posted 2007-09-17 10:30
银牌会员 Posts 931 Credits 1,928
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 ]
Floor8 terse Posted 2007-09-17 10:42
银牌会员 Posts 946 Credits 2,404
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 ]
Floor9 6692836 Posted 2007-09-17 15:58
初级用户 Posts 21 Credits 44
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 ]
Floor10 6692836 Posted 2007-09-17 16:30
初级用户 Posts 21 Credits 44
Sorry, I clicked by mistake.
Floor11 lxmxn Posted 2007-09-17 16:39
版主 Posts 4,938 Credits 11,386
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=:".
Floor12 wudixin96 Posted 2007-09-17 16:42
银牌会员 Posts 931 Credits 1,928
I don't have the permission to rate 2 points, so I can't help you. 6692836
Floor13 renrenrenshk Posted 2007-09-17 17:50
初级用户 Posts 15 Credits 39 From 福建
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 ]
Floor14 renrenrenshk Posted 2007-09-17 18:16
初级用户 Posts 15 Credits 39 From 福建
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 ]
Floor15 lxmxn Posted 2007-09-17 18:46
版主 Posts 4,938 Credits 11,386
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?
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023