将批处理文件(.bat文件)中的值返回到文本文件

时间:2022-05-01 16:03:37

I have a .bat file shown below

我有一个.bat文件如下所示

@echo off 
for /f "delims=" %%a in ('C:\MyProj\Sources\SearchString.vbs') do (
set ScriptOut=%%a)
#echo Script Result = %ScriptOut%
echo %ScriptOut% >C:\ThreePartition\Sources\myfile.txt

I want my output variable which is ScriptOut to be stored into a text file. Can anyone suggest any method to be added to my existing batch file.

我希望将输出变量ScriptOut存储到文本文件中。任何人都可以建议将任何方法添加到我现有的批处理文件中。

Thanks Maddy

2 个解决方案

#1


Do I understand correctly that your file gets overwritten and you want it appended? If so, try this:

我是否正确理解您的文件被覆盖并且您想要附加?如果是这样,试试这个:

echo %ScriptOut% >> C:\ThreePartition\Sources\myfile.txt

(note the double ">>")

(注意双“>>”)

#2


The for loop you have there executes that script and runs for every line the script returns. Basically this means that your environment variable %ScriptOut% contains only the last line of the output (since it gets overwritten each time the loop processes another line). So if your script returns

你在那里的for循环执行该脚本并运行脚本返回的每一行。基本上这意味着您的环境变量%ScriptOut%仅包含输出的最后一行(因为每次循环处理另一行时它都会被覆盖)。所以如果你的脚本返回

a
b
c

then %ScriptOut% will contain only c. If the last line is empty or contains only spaces iot will effectively delete %ScriptOut% which is why when you do an

然后%ScriptOut%将只包含c。如果最后一行是空的或只包含空格,那么iot将有效地删除%ScriptOut%,这就是你做的时候的原因

echo %ScriptOut%

you'll only get ECHO is on. since after variable substition all that's left there is echo. You can use

你只会得到ECHO。因为变量后的所有剩下的都是回声。您可以使用

echo.%ScriptOut%

in which case you'll be getting an empty line (which would be what %ScriptOut% contains at that point.

在这种情况下,你将得到一个空行(这将是%ScriptOut%包含在那一点。

If you want to print every line the script returns to a file then you can do that much easier by simply doing a

如果要打印脚本返回到文件的每一行,那么只需执行一个操作即可轻松完成

cscript C:\MyProj\Sources\SearchString.vbs > C:\ThreePartition\Sources\myfile.txt

or use >> for redirection if you want the output to be appended to the file, as Stanislav Kniazev pointed out.

如果你想将输出附加到文件中,请使用>>进行重定向,正如Stanislav Kniazev指出的那样。

If you just want to store the last non-empty line, then the following might work:

如果您只想存储最后一个非空行,则以下内容可能有效:

for /f "delims=" %%a in ('C:\MyProj\Sources\SearchString.vbs') do (
  if not "%%a"=="" set ScriptOut=%%a
)

which will only set %ScriptOut% in case the loop variable isn't empty.

只有在循环变量不为空的情况下才会设置%ScriptOut%。

#1


Do I understand correctly that your file gets overwritten and you want it appended? If so, try this:

我是否正确理解您的文件被覆盖并且您想要附加?如果是这样,试试这个:

echo %ScriptOut% >> C:\ThreePartition\Sources\myfile.txt

(note the double ">>")

(注意双“>>”)

#2


The for loop you have there executes that script and runs for every line the script returns. Basically this means that your environment variable %ScriptOut% contains only the last line of the output (since it gets overwritten each time the loop processes another line). So if your script returns

你在那里的for循环执行该脚本并运行脚本返回的每一行。基本上这意味着您的环境变量%ScriptOut%仅包含输出的最后一行(因为每次循环处理另一行时它都会被覆盖)。所以如果你的脚本返回

a
b
c

then %ScriptOut% will contain only c. If the last line is empty or contains only spaces iot will effectively delete %ScriptOut% which is why when you do an

然后%ScriptOut%将只包含c。如果最后一行是空的或只包含空格,那么iot将有效地删除%ScriptOut%,这就是你做的时候的原因

echo %ScriptOut%

you'll only get ECHO is on. since after variable substition all that's left there is echo. You can use

你只会得到ECHO。因为变量后的所有剩下的都是回声。您可以使用

echo.%ScriptOut%

in which case you'll be getting an empty line (which would be what %ScriptOut% contains at that point.

在这种情况下,你将得到一个空行(这将是%ScriptOut%包含在那一点。

If you want to print every line the script returns to a file then you can do that much easier by simply doing a

如果要打印脚本返回到文件的每一行,那么只需执行一个操作即可轻松完成

cscript C:\MyProj\Sources\SearchString.vbs > C:\ThreePartition\Sources\myfile.txt

or use >> for redirection if you want the output to be appended to the file, as Stanislav Kniazev pointed out.

如果你想将输出附加到文件中,请使用>>进行重定向,正如Stanislav Kniazev指出的那样。

If you just want to store the last non-empty line, then the following might work:

如果您只想存储最后一个非空行,则以下内容可能有效:

for /f "delims=" %%a in ('C:\MyProj\Sources\SearchString.vbs') do (
  if not "%%a"=="" set ScriptOut=%%a
)

which will only set %ScriptOut% in case the loop variable isn't empty.

只有在循环变量不为空的情况下才会设置%ScriptOut%。