windows 在文件中查找文本并将其设置为变量。批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22198458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find text in file and set it as a variable. Batch file
提问by byrqowy
I try to find one line in whole text file. Next I need to set this line as a variable.
我尝试在整个文本文件中找到一行。接下来我需要将此行设置为变量。
When i try do this:
当我尝试这样做时:
set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE
echo MY_VARIABLE
设置 MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE
回声 MY_VARIABLE
the result of echo is "findstr /I "MY_TEXT" MY.FILE" i want to see result of "findstr /I "MY_TEXT" MY.FILE" not a command
echo 的结果是 "findstr /I "MY_TEXT" MY.FILE" 我想看到 "findstr /I "MY_TEXT" MY.FILE" 的结果而不是命令
when i try do this first enter in cmd
当我尝试这样做时,首先输入 cmd
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
对于 /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') 设置 "batToolDir=%%a"
second enter in cmd
第二次输入cmd
echo "%batToolDir%"
echo "%batToolDir%"
i see "the %%a variable is unsuspected"
我看到“ %%a 变量是出乎意料的”
when i make a file SCRIPT.bat
当我制作文件 SCRIPT.bat
@echo off
for /F "delims=" %%a in ('set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
echo "%batToolDir%"
@回声关闭
对于 /F "delims=" %%a in ('set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE') 设置 "batToolDir=%%a"
echo "%batToolDir%"
i se anwser ""
我看到答案“”
What is wrong ? How to make this ?
怎么了 ?如何制作这个?
回答by MC ND
Almost done
快完成了
For command line
对于命令行
for /F "delims=" %a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%a"
For batch file double the percent signs
对于批处理文件,将百分号加倍
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"