Shai UI
Shai UI

Reputation: 51958

How to read a text file into an environment variable?

I have a one line text file (it will always be just one line). For example:

helloworld.txt contains "hello world"

I want to read this into an environment variable via the command prompt.

so Set MyVar=somehow reads helloworld.txt

Does anyone know how to do this?

Upvotes: 5

Views: 18030

Answers (2)

Jason Swager
Jason Swager

Reputation: 6501

set /P VARNAME=<FILENAME.TXT

This will only work with first line and will handle everything up to the end of the line.

Found at http://ss64.com/nt/set.html

Upvotes: 10

agent-j
agent-j

Reputation: 27943

This only works because you want the last line (or a one-line file).

for /f "delims=" %f in (helloworld.txt) DO Set MyVar=%f

For more information, use

for /?

Upvotes: 7

Related Questions