在set输出的内容中/作为分隔符,分为两列,从第一个/号左边作为一段赋值%%a,从第一个/号右边开始全部视为了一段赋值%%b
如DIR的内容为
c:\pdf\1.txt
c:\pdf\2.txt
c:\pdf\3.rar
c:\pdf\4.bmp
那么\作分隔符,
c:
c:
c:
c:
第一列赋值%%a
pdf\1.txt
pdf\2.txt
pdf\3.rar
pdf\4.bmp
这些全部赋值%%b
但是你这里用到的是DIR/B的参数只会输出文件名,所以tolens=1* delims=\是多余的.你这句是规范的.
改成如下即可for /f "delims=*" %%a in ('dir /b d:\qq\') do (
echo %%a
结合你上面的举两个例你看看
以下为例1
@echo off
md d:\pdf 2>nul
type>d:\name.txt 2>nul
type>>d:\pdf\1.txt 2>nul
type>>d:\pdf\2.txt 2>nul
type>>d:\pdf\3.rar 2>nul
type>>d:\pdf\4.bmp 2>nul
for /r d:\pdf\ %%i in (*) do (
echo %%~i >>d:\name.txt
)
for /f "tokens=1* delims=\" %%a in (d:\name.txt) do (
echo %%a
)
pause
例2
@echo off
md d:\pdf 2>nul
type>d:\name.txt 2>nul
type>>d:\pdf\1.txt 2>nul
type>>d:\pdf\2.txt 2>nul
type>>d:\pdf\3.rar 2>nul
type>>d:\pdf\4.bmp 2>nul
for /r d:\pdf\ %%i in (*) do (
echo %%~i >>d:\name.txt
)
for /f "tokens=1* delims=\" %%a in (d:\name.txt) do (
echo %%b
)
pause
Last edited by haiou327 on 2008-8-10 at 01:59 PM ]
In the content output by set, use / as the delimiter, divide into two columns. Take the part to the left of the first / number as a segment to assign to %%a, and take the entire part starting from the right of the first / number as a segment to assign to %%b.
For example, the content of DIR is
c:\pdf\1.txt
c:\pdf\2.txt
c:\pdf\3.rar
c:\pdf\4.bmp
Then use \ as the delimiter,
c:
c:
c:
c:
The first column is assigned to %%a
pdf\1.txt
pdf\2.txt
pdf\3.rar
pdf\4.bmp
All are assigned to %%b
But the parameter you used here is DIR/B, which only outputs the file name, so tolens=1* delims=\ is redundant. Your sentence is standard.
Just change it to the following: for /f "delims=*" %%a in ('dir /b d:\qq\') do (
echo %%a
Combine with your above two examples and you can take a look
The following is example 1
@echo off
md d:\pdf 2>nul
type>d:\name.txt 2>nul
type>>d:\pdf\1.txt 2>nul
type>>d:\pdf\2.txt 2>nul
type>>d:\pdf\3.rar 2>nul
type>>d:\pdf\4.bmp 2>nul
for /r d:\pdf\ %%i in (*) do (
echo %%~i >>d:\name.txt
)
for /f "tokens=1* delims=\" %%a in (d:\name.txt) do (
echo %%a
)
pause
Example 2
@echo off
md d:\pdf 2>nul
type>d:\name.txt 2>nul
type>>d:\pdf\1.txt 2>nul
type>>d:\pdf\2.txt 2>nul
type>>d:\pdf\3.rar 2>nul
type>>d:\pdf\4.bmp 2>nul
for /r d:\pdf\ %%i in (*) do (
echo %%~i >>d:\name.txt
)
for /f "tokens=1* delims=\" %%a in (d:\name.txt) do (
echo %%b
)
pause
Last edited by haiou327 on 2008-8-10 at 01:59 PM ]