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:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] How to extract the first line of text as the name of a new folder View 3,954 Replies 21
Original Poster Posted 2007-12-19 09:31 ·  中国 上海 联通
初级用户
Credits 120
Posts 48
Joined 2007-12-01 19:05
18-year member
UID 104373
Gender Male
Status Offline
For example: The content of 1.txt is as follows
2131241
6564556
5474577

Now we need to extract the content of the first line and create a new folder with the name being the content of the first line~~~? How to solve it? Thanks~~

[ Last edited by chenai79921 on 2007-12-25 at 10:54 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
kissbill +2 2008-11-15 00:27
Floor 2 Posted 2007-12-19 09:54 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
@echo off
for /f "tokens=2 delims=:" %%i in ('findstr /n . 1.txt^|findstr /b "1:"') do md %%i
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
kissbill +2 2008-11-15 00:27
第一高手 第二高手

Floor 3 Posted 2007-12-19 10:54 ·  中国 江苏 扬州 电信
高级用户
★★★
Credits 620
Posts 329
Joined 2007-12-05 15:34
18-year member
UID 104754
Gender Male
Status Offline
Floor 4 Posted 2007-12-19 11:00 ·  中国 上海 联通
初级用户
Credits 120
Posts 48
Joined 2007-12-01 19:05
18-year member
UID 104373
Gender Male
Status Offline
Thanks to fastslz~~~ Every time I ask for help, someone helps, so moved...
Are there any batch processing tutorials? I also want to learn it well.
Floor 5 Posted 2007-12-19 16:56 ·  中国 黑龙江 大庆 联通
高级用户
★★
Credits 547
Posts 261
Joined 2006-04-15 16:50
20-year member
UID 53887
Status Offline
### 分析原代码及问题
#### 关于`tokens=2 delims=:`
- `tokens=2`:是指在使用`for /f`循环处理文本时,按照指定的分隔符分割后,获取第2个令牌(即分割后的第2部分内容)。
- `delims=:`:是指定分隔符为冒号`:`。这里虽然文本中可能没有分号,但这里是设置分隔符的规则。

#### 关于`('findstr /n . 1.txt^|findstr /b "1:"')`
- `findstr /n . 1.txt`:`findstr`是用于查找字符串的命令,`/n`参数表示在每一行前面加上行号,`. `表示匹配所有字符(查找1.txt文件中的所有行并带上行号)。
- `^|`:这里是转义字符,因为在命令行中`|`是管道符号,需要转义才能在`for /f`的括号内正确使用,所以`^|`表示管道符号。
- `findstr /b "1:"`:`/b`参数表示从行首开始匹配,`"1:"`表示匹配行首为`1:`的行。所以整个部分是先找出1.txt文件中所有带行号且行首为`1:`的行。

### 翻译后的内容
Is `tokens=2` to select the content of the second column of the text?
Is `delims=:` to use `:` as the delimiter? There are no semicolons in the text!
Can you explain the meaning of `('findstr /n . 1.txt^|findstr /b "1:"')`?
Thank you, waiting...
Floor 6 Posted 2007-12-19 17:14 ·  中国 上海 联通
初级用户
Credits 120
Posts 48
Joined 2007-12-01 19:05
18-year member
UID 104373
Gender Male
Status Offline
1: Content
2: Content

tokens=2 The second parameter is the content,, : is the delimiter

findstr /n is to print the line number before each matched line,, pair with 1: at the beginning of each line

I saw it in the help,, and what does ^ mean, I don't know either
Floor 7 Posted 2007-12-19 18:45 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
那是否也可以将文本的最后一行,当作文件的名字呢
是最后一行,不是第三行;)

Is it also possible to take the last line of the text as the name of the file? It's the last line, not the third line;)
Floor 8 Posted 2007-12-19 19:11 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by huahua0919 at 2007-12-19 18:45:


Can the last line of the text also be used as the file name?
It's the last line, not the third line ;)

The last line should be okay:

@echo off
for /f "delims=" %%a in (a.txt) do set "fss=%%a"
md "%fss%"

Also, the first line can be like this:

@echo off
for /f "delims=" %%a in (a.txt) do if not defined fss set fss="%%a"
md %fss%
简单!简单!再简单!
Floor 9 Posted 2007-12-19 19:16 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Simple! Simple! Even simpler!

The first line is very clever!
Floor 10 Posted 2007-12-19 20:09 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```
@echo off
for /f "delims=" %%i in (a.txt) do md "%%i" &exit
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
kissbill +2 2008-11-15 00:28
Floor 11 Posted 2007-12-20 00:48 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```set/p file=<a.txt
md %file%
```
Floor 12 Posted 2007-12-23 10:43 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Originally posted by lxmxn at 2007-12-20 12:48 AM:
set/p file=<a.txt
md %file%


I have to admire the moderator's skills!
Can the same method be used to create a folder name for the second line?
Floor 13 Posted 2007-12-24 00:01 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
No, this method can only get the first line of the file.
Floor 14 Posted 2007-12-25 13:46 ·  中国 上海 移动
新手上路
Credits 18
Posts 8
Joined 2006-12-07 01:26
19-year member
UID 72769
Gender Male
Status Offline
It's really powerful. I encountered a problem before.
I wanted to use findstr to find the line of text where a string appears in the file. Then read the 7th string from this line is handled like this. Let's see if there is a simpler way?

%1 is the content to be found,

@FINDSTR /i %1 log.txt >temp01.txt
FOR /F "eol=; tokens=7 delims= " %%1 in (temp01.txt) do (
Echo %%1 > temp02.txt
goto :jump1
)

:jump1
Floor 15 Posted 2007-12-25 14:03 ·  中国 广西 玉林 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Put FINDSTR inside for
FOR /F "eol=; tokens=7 delims= " %%i in ('FINDSTR /i %1 log.txt') do Echo %%i
Forum Jump: