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-25 09:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to combine FOR loops with findstr? View 4,606 Replies 25
Original Poster Posted 2010-12-10 09:41 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
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?
Floor 2 Posted 2010-12-10 10:19 ·  中国 北京 联通
高级用户
★★★
据说是李先生
Credits 609
Posts 400
Joined 2008-04-23 15:55
18-year member
UID 116706
Gender Male
Status Offline
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.

for /f "tokens=2 delims=: " %%i in ('findstr "^Project" 2.ini') do
Floor 3 Posted 2010-12-10 10:38 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
Thanks~~ It has been solved~~~
Floor 4 Posted 2010-12-10 11:15 ·  中国 广东 深圳 诺瓦科技发展有限公司
新手上路
Credits 18
Posts 18
Joined 2010-12-06 16:28
15-year member
UID 178780
Gender Male
Status Offline
Learning
Floor 5 Posted 2010-12-10 15:11 ·  中国 北京 电子政务网
新手上路
Credits 2
Posts 2
Joined 2010-12-09 15:03
15-year member
UID 178983
Gender Male
From 北京
Status Offline
Learn it! Thank you
Floor 6 Posted 2010-12-10 16:11 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
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?
Floor 7 Posted 2010-12-10 16:27 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Is this not okay?
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in (1.ini) do (
set i=%%i
if /i "!i:~0,7!"=="Project" >>bios.dat echo %%j
)
Floor 8 Posted 2010-12-10 17:02 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
### 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.
Floor 9 Posted 2010-12-10 17:26 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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...
Floor 10 Posted 2010-12-10 17:30 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
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?
Floor 11 Posted 2010-12-10 17:38 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
如果 /i "!i:~0,7!"=="Project"
/i ignore case
!i:~0,7!截取变量的前七个字符
! is the writing of % in variable delay
Floor 12 Posted 2010-12-10 18:12 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
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?
Floor 13 Posted 2010-12-10 18:24 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Floor 14 Posted 2010-12-10 18:31 ·  中国 江苏 无锡 电信
初级用户
Credits 34
Posts 30
Joined 2010-12-02 01:43
15-year member
UID 178698
Gender Male
Status Offline
How to update the FOR command under DOS? I can't seem to find it?
Floor 15 Posted 2010-12-10 19:32 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
```
@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 ]
Forum Jump: