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 02:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] How to extract the first line of text as the name of a new folder View 3,953 Replies 21
Floor 16 Posted 2007-12-25 14:34 ·  中国 上海 移动
新手上路
Credits 18
Posts 8
Joined 2006-12-07 01:26
19-year member
UID 72769
Gender Male
Status Offline
Originally posted by zh159 at 2007-12-25 02:03 PM:
Put FINDSTR inside for
FOR /F "eol=; tokens=7 delims= " %%i in ('FINDSTR /i %1 log.txt') do Echo %%i



This will find all text lines in log.txt that contain %1. I only need the first line that contains %1. I looked for parameters in findstr and for but couldn't control finding only one line. So I used goto to jump out of the loop.

Now replace with this:
for /f "tokens=7 delims= " %%i in ('findstr /n %1 log.txt^|findstr /b "1:"') do echo %%i >> temp03.txt

[ Last edited by jinthree on 2007-12-25 at 03:14 PM ]
Floor 17 Posted 2008-11-14 03:55 ·  中国 广东 阳江 电信
新手上路
Credits 11
Posts 7
Joined 2008-11-14 03:27
17-year member
UID 130906
Gender Male
Status Offline
How to extract each line from a text file and use it as a name to create multiple folders?
Floor 18 Posted 2008-11-14 16:39 ·  中国 江苏 苏州 电信
新手上路
Credits 18
Posts 20
Joined 2008-11-05 15:36
17-year member
UID 130077
Gender Male
Status Offline
set/p file=<a.txt
md %file%
Can you explain it specifically? Why does it ignore what's before the comma and create two folders separated by spaces if there are spaces?
Floor 19 Posted 2008-11-14 17:56 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Originally posted by iyou at 2008-11-14 04:39 PM:
set/p file=<a.txt
Can you explain it specifically? Why does it ignore what's before the comma and create two folders separated by spaces if there's a space?

1、set /p str=Please enter:
Can you enter two lines of text when inputting? No! So set/p file=<a.txt only gets the first line of characters.

2、md and delimiters
If the name of the folder to be created contains spaces, it must be enclosed in double quotes.
Example:
md "1 2"
:: Create a folder named 1 2

md 1 2 3
:: Create three folders named 1, 2, and 3 respectively

md a,b;c
:: Create three folders named a, b, and c respectively
:: The commas and semicolons here are delimiters. You can also delete folders like this: rd a;b;c
___________________________________________________________
Why does it ignore what's before the comma?

??

[ Last edited by tireless on 2008-11-14 at 17:58 ]
Floor 20 Posted 2008-11-14 18:25 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 133
Posts 75
Joined 2008-08-03 01:08
17-year member
UID 122677
Gender Male
Status Offline
### Solution
The `if not defined` construct in batch scripting checks if a variable is not defined. In the given code:

- `for /f "delims=" %%a in (a.txt) do if not defined fss set fss="%%a"`: This loop reads each line from `a.txt`. For the first line read (`%%a`), since the variable `fss` is not defined initially, it sets `fss` to the value of that line (enclosed in quotes). Then `md %fss%` creates a directory with the name stored in `fss`, which is the first line of `a.txt`.

The translated text remains as the original code with the quote content:
Originally posted by terse at 2007-12-19 07:11 PM:
@echo off
for /f "delims=" %%a in (a.txt) do if not defined fss set fss="%%a"
md %fss%

if not defined这个不是很清楚,解释一下如何实现第一行为文件名的?
先谢谢
Floor 21 Posted 2008-11-14 19:05 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
For example, the content of a.txt is:

Line 1
Line 2
Line 3

for /f "delims=" %%a in (a.txt) do if not defined fss set fss="%%a"

Because for reads each line and then inserts the read characters into the for statement:
C:\>test.bat

C:\>for /F "delims=" %a in (a.txt) do if not defined fss set fss="%a"

C:\>if not defined fss set fss="Line 1"

C:\>if not defined fss set fss="Line 2"

C:\>if not defined fss set fss="Line 3"

Before executing this for, the fss variable is not defined. So when the line if not defined fss set fss="Line 1" is executed, the value of the variable fss is set to Line 1. Starting from the second line, the value of the variable fss is no longer set because if not defined fss ... and I have already defined fss (Line 1)... ...

——————————————————————————————————

This is a method, but the two best methods are:

Floor 10: Read the characters of the first line in the text, execute the corresponding command, and then jump out of the for loop.
@echo off 
for /f "delims=" %%i in (a.txt) do md "%%i" &exit

Floor 11:
set/p file=<a.txt
md %file%
Floor 22 Posted 2008-11-15 00:26 ·  中国 天津 电信
初级用户
Credits 61
Posts 35
Joined 2008-10-04 13:00
17-year member
UID 127389
Gender Male
Status Offline
All classic solutions. Learned.

Accidentally added points to the owner's post, but didn't add points to the HAT moderator's post. Sweat.

[ Last edited by kissbill on 2008-11-15 at 00:29 ]
Forum Jump: