简体   繁体   中英

Get a specific text from a text file using batch

I've got a text file named 'Settings.txt' Within the file I have “Access: False” Am I able to grab the text after 'Access:' to see what they have inputted?

Here is what I have tried:

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

You can incorporate findstr and also you did not use else correctly.

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

Obviously there are easier ways, this example does not need else it will fall through to start if the value is True else it will skip that label and goto fail label:

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

You will need to check for the word Access (unless there is only one line in Settings.txt ), to add the SPACE behind the : to the list of delimiters, and you will need to adapt the if syntax and probably make it case-insensitive ( /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)
)

Which can be rewritten as this (to make the if blocks more obvious):

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
            )
        )
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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