将文本文件的内容读取到环境变量中

发布于 2024-10-09 16:37:24 字数 281 浏览 0 评论 0原文

实际上我想将 txt 文件的内容传递给 if 条件

我正在尝试以下

set /p var = < file name 
if not %var% == “string” goto x else goto y

但它不起作用。

当我检查 var 的值时,它显示变量未定义

Set %var%

环境变量 %var% 未定义

Actually I want to pas the content of txt file to if condition

I am trying the following

set /p var = < file name 
if not %var% == “string” goto x else goto y

But it's not working .

When I check value of var it shows variable not defied

Set %var%

Environment variable %var% not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别挽留 2024-10-16 16:37:24
echo sometext > testfile.txt
set /P someVar=<testfile.txt
if %someVar%==sometext (echo true) else (echo false)

据我了解,这是要基于的示例。

echo sometext > testfile.txt
set /P someVar=<testfile.txt
if %someVar%==sometext (echo true) else (echo false)

From what I understood, this is the example to be based on.

情栀口红 2024-10-16 16:37:24

我想你想

echo %var%

显示变量 var 的当前内容。

I think you want

echo %var%

to show the current contents of the variable var.

〆一缕阳光ご 2024-10-16 16:37:24

现在它正在工作。我在设置变量cmd中使用了空格。

设置 /P someVar=< file.txt

if "%someVar%"=="some text" (echo true) else (echo false)

now it's working. I was using space in set variable cmd.

set /P someVar=< file.txt

if "%someVar%"=="some text" (echo true) else (echo false)

风吹雪碎 2024-10-16 16:37:24

值得注意的是 "%var%" 必须用引号引起来。如果您不带引号的 %var% 并且 %var% 包含 string 您的语句将变为:

if not string == "string"转到 x 否则转到 y

It may be worth noting that "%var%" has to be in quotes. If you leave %var% without quotes and %var% contains string your statement becomes:

if not string == "string" goto x else goto y

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文