在批处理文件中将文本文件中的特定行读取为一组“ lineinfo =第1行或第2行的文本”

时间:2018-07-25 16:14:32

标签: batch-file text

我试图使批处理文件从文本文件中读取特定行,并设置NewHDLocation =“ then here is the text from the line in txt”,然后执行2次,所以我以后可以执行%NewHDLocation %和%WOTinstalldir%告诉命令该信息

我的代码示例:

for /F "skip=0 delims=" %%i in (Settings.txt) do if not defined NewHDLocation set "NewHDLocation=%%i"&goto next

:next
for /F "skip=1 delims=" %%i in (Settings.txt) do if not defined WOTinstalldir set "WOTinstalldir=%%i"&goto next2

:next2
@echo %NewHDLocation%
@echo %WOTinstalldir%
pause

最后2个@echo只是测试它是否从txt中获取信息,但它只会输出:

C:\WINDOWS\system32>for /F "skip=0 delims=" %i in (Settings.txt) do if not defined NewHDLocation set "NewHDLocation=%i"  & goto next
 delims=" was unexpected at this time.

C:\WINDOWS\system32>for /F "skip=1 delims=" %i in (Settings.txt) do if not defined WOTinstalldir set "WOTinstalldir=%i"  & goto next2

C:\WINDOWS\system32>if not defined WOTinstalldir set "WOTinstalldir=ECHO is on."  & goto next2
ECHO is on.
ECHO is on.

C:\WINDOWS\system32>pause
Press any key to continue . . .

在我需要阅读的文本文件中(仅用于测试):

New_HDfiles_Drive
WOT_Install_Location

1 个答案:

答案 0 :(得分:2)

在这种特殊情况下,我可能只是将文件流式传输到代码块中。

@ECHO OFF
<settings.txt (
set /p "NewHDLocation="
set /p "WOTinstalldir="
)

echo %NewHDLocation%
echo %WOTinstalldir%
pause