例如 a.txt 的内容是:
第一行
第二行
第三行
for /f "delims=" %%a in (a.txt) do if not defined fss set fss="%%a"
因为 for 是每读取一行,然后把读取到的字符套入 for 语句中:
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="第一行"
C:\>if not defined fss set fss="第二行"
C:\>if not defined fss set fss="第三行"
在执行这个 for 之前,并没有定义 fss 变量,所以当执行
if not defined fss set fss="第一行" 这句后,就设置了变量 fss 的值为
第一行。从第二行开始就不再设置变量 fss 的值了,因为
if not defined fss ... 而我已经 defined fss 过了(第一行)... ...
——————————————————————————————————
这是一个方法,但最好的两种方法是:
10 楼:读取文本中的第一行的字符,执行相应命令后就跳出 for 循环。
@echo off
for /f "delims=" %%i in (a.txt) do md "%%i" &exit
11 楼:
set/p file=<a.txt
md %file%
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%