繁体   English   中英

使用批处理从文本文件中获取特定文本

[英]Get a specific text from a text file using batch

我有一个名为“Settings.txt”的文本文件 在我的文件中我有“Access: False” 我能在“Access:”之后抓取文本来查看他们输入的内容吗?

这是我尝试过的:

for /f "token=2 delims=:" %%L in (Settings.txt) do (if %%L==True goto Start ELSE FALSE goto Fail ) 

您可以合并findstr并且您没有正确使用else

for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do (
   if /i "%%i" == "True" (
     goto :start
   ) else (
     goto :fail
 )
)

显然,有更简单的方法,这个例子并不需要else就会掉下来,通过start该值是否True否则将跳过标签并转到fail标签:

for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do if /i not "%%i" == "True" goto :fail
:start
echo do start stuff here:
goto :eof
:fail
echo do fail stuff here

您将需要检查单词Access (除非Settings.txt只有一行),将:后面的空格添加到分隔符列表中,并且您需要调整if语法并可能使其大小写 -不敏感( /I ):

for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
    if /I "%%K"=="Access" if /I "%%L"=="True" (goto Start) else if /I "%%L"=="False" (goto Fail)
)

可以这样重写(使if块更明显):

for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
    if /I "%%K"=="Access" (
        if /I "%%L"=="True" (
            goto Start
        ) else (
            if /I "%%L"=="False" (
                goto Fail
            )
        )
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM