中国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-11-24 08:49
47,820 topics / 349,938 posts / today 0 new / 48,298 members
DOS批处理 & 脚本技术(批处理室) » [Solved] How to extract the first line of text as the name of a new folder
Printable Version  4,830 / 21
Floor16 jinthree Posted 2007-12-25 14:34
新手上路 Posts 8 Credits 18
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 ]
Floor17 cdmtys168 Posted 2008-11-14 03:55
新手上路 Posts 7 Credits 11
How to extract each line from a text file and use it as a name to create multiple folders?
Floor18 iyou Posted 2008-11-14 16:39
新手上路 Posts 20 Credits 18
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?
Floor19 tireless Posted 2008-11-14 17:56
银牌会员 Posts 1,122 Credits 2,025
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 ]
Floor20 slw218856 Posted 2008-11-14 18:25
初级用户 Posts 75 Credits 133
### 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这个不是很清楚,解释一下如何实现第一行为文件名的?
先谢谢
Floor21 tireless Posted 2008-11-14 19:05
银牌会员 Posts 1,122 Credits 2,025
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.

Floor 11:
Floor22 kissbill Posted 2008-11-15 00:26
初级用户 Posts 35 Credits 61
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 ]
Prev  1 2
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023