文本文件内容:
{"progress":"2","type":"info","message":"Automatic Installation initiated."},"5":{"progress":"20.25","type":"info","message":"Checking connectivity to Internet and management system."}},"install_progress":0,"logFileName":"FWF61E4Q16001577_2018-4-5_12_58_57.log"},"token":"gDDJ7uZx"}
{"progress":"30","type":"info","message":"Retrieving site specific information from management system."}
我正在使用此命令捕获标记值:
for /f "delims=" %%x in ('findstr "token" test.txt') do set "filecontent=%%x"
echo %filecontent%
for /f "delims=token" %%i in ("%filecontent%") do set tokenvalue=%%i
echo %tokenvalue%
预期输出:gDDJ7uZx
我无法找到一种方法可以将令牌值捕获为输出
请帮助我。
答案 0 :(得分:1)
for /f
,delims
是单字符分隔符的集合(没有字符串分隔符)。因此,delims=token
将在每个t
,o
,k
,e
和n
分隔。
@echo off
for /f "delims=" %%a in ('type t.txt^|find "token"') do set "line=%%a"
for /f delims^=^" %%a in ("%line:*"token":=%") do set "line=%%a"
echo %line%
我用两条线工作;第一个获得该行,第二个获取请求的令牌
此外,在使用第二个token
提取请求的令牌(因为它可能不确定)之前,我使用set
子串替换从开头删除了部分(包括)for /f
,字符串如何看起来像)
编辑您评论的代码(我很难正确格式化)在很多地方出错了,我不会为他们编号:
set %tokenfoundFirstTime=0
findstr /c:"token" test.file
if %errorlevel%==0 (
if %tokenfoundFirstTime%==1 (
for /f "delims=" %%a in ('type test.txt^|find "token"') do set "line=%%a"
for /f "delims^=^" %%a in ("%line:*"token":=%") do set "line=%%a"
echo %line%
SET tokenValue=%line%
set tokenfoundFirstTime=2
)
)
将其与下面的更正版本进行比较:
@echo off
setlocal enabledelayedexpansion
set tokenfoundFirstTime=0
findstr /c:"token" test.file >nul
if %errorlevel%==0 (
if %tokenfoundFirstTime%==0 (
for /f "delims=" %%a in ('type test.file^|find "token"') do set "line=%%a"
for /f delims^=^" %%a in ("!line:*"token":=!") do set "line=%%a"
SET "tokenValue=!line!"
set tokenfoundFirstTime=2
)
)
echo %tokenvalue%
(我认为tokenfoundFirsttime
和相应的if
没有必要也没有价值,但是将其留在那里因为可能有更多的代码没有显示,可能需要它以某种方式。)
答案 1 :(得分:0)
使用此语句时出现语法错误
for / f delims ^ = ^“%% a in(”%line:*“token”:=%“)设置”line = %% a“
请更正