中国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-08-12 09:52
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » How to combine FOR loops with findstr?
Printable Version  5,019 / 25
Floor1 van416 Posted 2010-12-10 09:41
初级用户 Posts 30 Credits 34
Today I'm working on the FOR statement and don't know how to combine the for statement with findstr. The following are the separate programs. Please help. Thanks~

Requirement: Extract the content after the project field

2.ini
Hierarchy ID : Normal Board
Chip SKU : 250
Project : 1060-0011
CDP : N/A

1.bat
@echo off
DEL 1.txt
findstr "^Project" 2.ini>1.txt

2.bat
@echo off
setlocal
for /f "delims=: tokens=2" %%i in (1.txt) do (
set biosv=%%i
)
echo.%biosv% >bios.dat

endlocal

Above, I want to combine 1.bat and 2.bat. How should I add 1.bat into 2.bat?
Floor2 qinchun36 Posted 2010-12-10 10:19
高级用户 Posts 400 Credits 609
You can use statements in this style to process the result and avoid the generation of intermediate files:

FOR /F %x IN ('Command line with text output') DO ***

That command line is enclosed in single quotes, by the way, delims is best written at the end.
Floor3 van416 Posted 2010-12-10 10:38
初级用户 Posts 30 Credits 34
Thanks~~ It has been solved~~~
Floor4 yf6122010 Posted 2010-12-10 11:15
新手上路 Posts 18 Credits 18
Learning
Floor5 a081910 Posted 2010-12-10 15:11
新手上路 Posts 2 Credits 2 From 北京
Learn it! Thank you
Floor6 van416 Posted 2010-12-10 16:11
初级用户 Posts 30 Credits 34
I don't have the findstr command under DOS, and the one under WINXP can't be used. Can someone who has this tool kindly share it with me?
Floor7 Hanyeguxing Posted 2010-12-10 16:27
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
Is this not okay?
Floor8 van416 Posted 2010-12-10 17:02
初级用户 Posts 30 Credits 34
### Translation Result ###
### 对于 `for /f "tokens=1* delims=:" %%i in (1.ini) do (` 部分:
在 `for /f` 循环中,`tokens=1* delims=:` 的意思是:`tokens=1*` 表示将每行按 `delims=:` 定义的分隔符(这里是冒号)进行分割,取第一个标记赋给 `%%i`,剩下的部分赋给 `%%j`。

### 对于 `if /i "!i:~0,7!"=="Project" >>bios.dat echo %%j` 部分:
- `if /i`:表示不区分大小写进行比较。
- `!i:~0,7!`:这是字符串截取操作,`~0,7` 表示从字符串 `!i!` 的开头(索引0)开始,截取7个字符。
- 整句意思是:如果 `!i!` 这个变量从开头开始的7个字符不区分大小写等于 "Project",就将 `%%j` 追加输出到 `bios.dat` 文件中。

So the translation of the relevant text is:

### For the part `for /f "tokens=1* delims=:" %%i in (1.ini) do (`:
In the `for /f` loop, `tokens=1* delims=:` means: `tokens=1*` means that each line is split by the delimiter defined by `delims=:` (here is the colon), the first token is assigned to `%%i`, and the remaining part is assigned to `%%j`.

### For the part `if /i "!i:~0,7!"=="Project" >>bios.dat echo %%j`:
- `if /i` means: perform a case-insensitive comparison.
- `!i:~0,7!`: this is a string intercept operation, `~0,7` means intercept 7 characters from the beginning (index 0) of the string `!i!`.
- The whole sentence means: if the 7 characters from the beginning of the `!i!` variable are case-insensitively equal to "Project", then append and output `%%j` to the `bios.dat` file.
Floor9 Hanyeguxing Posted 2010-12-10 17:26
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
I haven't used DOS, so I don't know if such syntax is supported under DOS. If it is supported, what does the LZ mean by researching it...
Floor10 van416 Posted 2010-12-10 17:30
初级用户 Posts 30 Credits 34
Um, okay, thanks, it can be used. Now what I don't understand is in "if /i "!i:~0,7!"=="Project" what does!i:~0,7 mean?
Floor11 Hanyeguxing Posted 2010-12-10 17:38
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
如果 /i "!i:~0,7!"=="Project"
/i ignore case
!i:~0,7!截取变量的前七个字符
! is the writing of % in variable delay
Floor12 van416 Posted 2010-12-10 18:12
初级用户 Posts 30 Credits 34
Thanks..Got it

Another question: The for command in pure MS-DOS doesn't seem to support the for /f statement. Can this for command be downloaded?
Floor13 Hanyeguxing Posted 2010-12-10 18:24
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
`for` is an internal command...
Floor14 van416 Posted 2010-12-10 18:31
初级用户 Posts 30 Credits 34
How to update the FOR command under DOS? I can't seem to find it?
Floor15 Hanyeguxing Posted 2010-12-10 19:32
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
```
@echo off
find /i "Project" 2.ini>han_1.txt
more +2 han_1.txt>han_2.txt
set/p k=<han_2.txt
set k=%k:*:=%
del /q han_*
echo %k:~1% >bios.dat
```

[ Last edited by Hanyeguxing on 2010-16-10 at 19:37 ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023