Reading the content of a text file into an environ

2019-08-14 03:35发布

问题:

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

回答1:

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.



回答2:

I think you want

echo %var%

to show the current contents of the variable var.



回答3:

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)



回答4:

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