Amrith Gopikrishnan
Amrith Gopikrishnan

Reputation: 11

Read a file and obtain a value from it in batch script

I have a set of servers wherein there is a file in a location called "archive.info" in all servers. I have the following data in the file:

#Fri, 14 Feb 2014 20:57:23 -0500

buildname=coreCode_714_317
builddate=2014-02-14 20.57 EST
majorversion=06
minorversion=01

I want to create a batch script which can read the buildname from the file and display the output. I use psexec to run remote commands which I could use to redirect the result to a file.

Could you please help me with creating a batch script which can read the build name and display itplease?

Thanks Amrith

Upvotes: 1

Views: 87

Answers (2)

SomethingDark
SomethingDark

Reputation: 14304

This will read the file one line at a time, ignoring any line that starts with a #, and creating variables from the contents of archive.info

for /f "eol=# tokens=*" %%A in (archive.info) do set "%%A"

Since your lines are in the format variable=value, the code effectively runs

set buildname=coreCode_714_317
set builddate=2014-02-14 20.57 EST
set majorversion=06
set minorversion=01

and you can now use all four variables in your batch script. The quotes are there to preserve the spaces in builddate.

Upvotes: 1

Stephan
Stephan

Reputation: 56155

if you are interested only in one value:

for /f "tokens=*" %%i in ('findstr /i /b "buildname=" archive.info') do set "%%i"

If you need more values, I'd prefer SomethingDarks answer.

Upvotes: 0

Related Questions