在批处理文件中创建 "Memory"

Creating "Memory" In Batch Files

我不确定如何在这里表达我的问题。但是例如,假设我有一个批处理文件:

@echo off

echo test > text.txt
set example_var=<text.txt

我知道这段特定的代码是多余的,因为您可以将 "example_var" 设置为等于 "test",但我的问题是如何创建 "memory"(如数据文本文档包含)在批处理中?

像这样:

@echo off

echo test 0>&1
set example_var=<&1

基本上我只是想避免创建临时文本文档来保存信息。我确定我之前在这个网站上找到了一个例子,但我找不到。

编辑:

情况是这样的:

(%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe .\Code.ps1) | find "::/kl" | set /p code

我希望 %systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe .\Code.ps1find "::/kl" 的输出作为 set /p code 的输入,最终设置一串文本放入变量 code.

cmd.exe 的每个实例都从起始进程继承环境,而起始进程又从主环境继承。
因此,您可以使用 setx.

持久存储在主环境中

或者就像您尝试将单个变量存储在文件中并使用

检索
(Echo:%Example_var%)>text.txt
Set /p "Example_var="<text.txt

这将只存储变量的内容,或者

(Set Example_var)>text.txt
For /f "delims=" %%A in (text.txt) Do Set %%A

哪个stores/restoresvariablename=variablecontent

作为不指定变量名的变体,后者可以 store/restore the whole environment.

您的管道方法将无法为当前脚本设置变量,因为管道中的每个命令都 运行 在不同的进程中。

处理将命令的输出检索到变量中的任务的常用方法是使用 for /f 命令

for /f "delims=" %%a in ('
    %systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe .\Code.ps1
    ^| find "::/kl"
') do set "code=%%a"