使用批处理脚本从文本文件中查找值

使用批处理脚本从文本文件中查找值

我想创建一个批处理文件来从文本文件中获取两个值文本文件(default.txt)中的值是

submask=255.255.254.0
gateway=10.13.62.1

脚本:

for %%a in ('"%~dp0default.txt"^|find "submask="') do set "%%a=subm"
  echo %subm%
for %%a in ('"%~dp0default.txt"^|find "gateway="') do set "%%a=gate"
  echo %gate%

但脚本不起作用。请帮忙。

修改

for %%a in ('"%~dp0Submask_Gateway.txt"^|find "submask="') do set subm=%%a
  echo %subm%

for %%a in ('"%~dp0Submask_Gateway.txt"^|find "gateway="') do set gate=%%a
  echo %gate%

在此处输入图片描述

第二次修订

@echo on
type "%~dp0Submask_Gateway.txt"
for %%a in ('type "%~dp0Submask_Gateway.txt"^|find "submask="') do set subm=%%a
  echo %subm%

for %%a in (type ' "%~dp0Submask_Gateway.txt"^|find "gateway="') do set gate=%%a
  echo %gate%

pause

在此处输入图片描述

最终的

REM Extract Submask IP address
for /f "tokens=2 delims== " %%a in ('type "ZZ_Submask_Gateway.txt"^| find "submask="') do set "subm=%%a"
echo %subm%

REM Extract Gateway IP address
for /f "tokens=2 delims== " %%a in ('type "%~dp0ZZ_Submask_Gateway.txt"^| find "gateway="') do set "gate=%%a"
echo %gate%

答案1

另一种 powershell 解决方案可能是:

gc Gateway_mask.txt | %{ echo $_.Split("=")[1]}

相关内容