Well,qdcr可能考虑得太复杂了吧,在这个例子中其实并不需要把文件每一行的内容保存下来,只需要简单地使用for语句的文本处理方面的功能似乎就可以完成任务了:
@echo off
for /f %%i in (list.txt) do call :dosomething "%%i"
echo 开始创建目录...
for %%m in (%strAll%) do md %%m\backup
pause
goto :eof
:dosomething
set /p str=输入 %1 的目录:
if "%str%" == "" goto error
dir %str% >nul 2>nul || goto :error
set strAll=%strAll% "%str%"
goto :eof
:error
echo 目录不存在!
echo 按任意键退出...
pause>nul
exit
goto :eof
当然,在批处理中将文件每行的内容都保存下来也不是不行:
@echo off
for /f "delims=: tokens=1,2*" %%i in ('"findstr /n . list.txt"') do set %%i=%%j && set num=%%i
setlocal ENABLEDELAYEDEXPANSION
for /l %%l in (1,1,%num%) do call :dosomething "!%%l!"
echo 开始创建目录...
for %%m in (%strAll%) do md %%m\backup
pause
goto :eof
:dosomething
set /p str=输入 %1 的目录:
if "%str%" == "" goto error
dir %str% >nul 2>nul || goto :error
set strAll=%strAll% "%str%"
goto :eof
:error
echo 目录不存在!
echo 按任意键退出...
pause>nul
exit
goto :eof
在最后,还是想说的是:用批处理来处理文本内容的时候还是很麻烦的,上面的脚本并没有做更多的出错检测及处理。当文本文件的内容中包含有&,|,&&,||等特殊字符时,还是很可能引起错误的,如果你的要求比较高可以自己尝试加入一些判断并处理错误的语句。