中国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-23 02:54
47,812 topics / 349,917 posts / today 0 new / 48,273 members
DOS批处理 & 脚本技术(批处理室) » [Solved] How to extract the first line of text as the name of a new folder
Printable Version  4,414 / 21
Floor1 chenai79921 Posted 2007-12-19 09:31
初级用户 Posts 48 Credits 120
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 ]
Floor2 fastslz Posted 2007-12-19 09:54
铂金会员 Posts 2,315 Credits 5,493 From 上海
@echo off
for /f "tokens=2 delims=:" %%i in ('findstr /n . 1.txt^|findstr /b "1:"') do md %%i
Floor3 cad55 Posted 2007-12-19 10:54
高级用户 Posts 329 Credits 620
Floor4 chenai79921 Posted 2007-12-19 11:00
初级用户 Posts 48 Credits 120
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.
Floor5 tempuser Posted 2007-12-19 16:56
高级用户 Posts 261 Credits 547
### 分析原代码及问题
#### 关于`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...
Floor6 chenai79921 Posted 2007-12-19 17:14
初级用户 Posts 48 Credits 120
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
Floor7 huahua0919 Posted 2007-12-19 18:45
银牌会员 Posts 780 Credits 1,608
那是否也可以将文本的最后一行,当作文件的名字呢
是最后一行,不是第三行;)

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;)
Floor8 terse Posted 2007-12-19 19:11
银牌会员 Posts 946 Credits 2,404
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:

Also, the first line can be like this:
Floor9 huahua0919 Posted 2007-12-19 19:16
银牌会员 Posts 780 Credits 1,608
Simple! Simple! Even simpler!

The first line is very clever!
Floor10 jmz573515 Posted 2007-12-19 20:09
银牌会员 Posts 464 Credits 1,212
```
@echo off
for /f "delims=" %%i in (a.txt) do md "%%i" &exit
```
Floor11 lxmxn Posted 2007-12-20 00:48
版主 Posts 4,938 Credits 11,386
```set/p file=<a.txt
md %file%
```
Floor12 huahua0919 Posted 2007-12-23 10:43
银牌会员 Posts 780 Credits 1,608
Originally posted by lxmxn at 2007-12-20 12:48 AM:


I have to admire the moderator's skills!
Can the same method be used to create a folder name for the second line?
Floor13 lxmxn Posted 2007-12-24 00:01
版主 Posts 4,938 Credits 11,386
No, this method can only get the first line of the file.
Floor14 jinthree Posted 2007-12-25 13:46
新手上路 Posts 8 Credits 18
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
Floor15 zh159 Posted 2007-12-25 14:03
金牌会员 Posts 1,467 Credits 3,687
Put FINDSTR inside for
FOR /F "eol=; tokens=7 delims= " %%i in ('FINDSTR /i %1 log.txt') do Echo %%i
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023