Page 1 of 1

Set a variable to file contents

Posted: 04 Feb 2013 05:56
by matt_thumper
:?:
Hi,

How to I grab the (simple) contents of a file (which will be YYYYMMDD each day) and assign it to a variable?

So, the file mydatefile.txt contains 20130204 and I want the batch script to assign '20130204' to the variable a.
(All of the ancillary things I've worked thru, and can do - except for assigning - the contents of a file - to a variable.)

For you unixy-types:

a=`cat mydatefile.txt`

(This will be for a batch script that contains a once-per-day routine and executed from Startup)

tia,
Matt

Re: Set a variable to file contents

Posted: 04 Feb 2013 06:01
by foxidrive

Code: Select all

@echo off
set /p "a="<mydatefile.txt
echo "%a%"
pause

Re: Set a variable to file contents

Posted: 04 Feb 2013 06:08
by shirulkar
Hi,
Following will work.

Code: Select all

@echo off
for /f "usebackq tokens=* delims=" %%a in ("mydatefile.txt") do call :Fun "%%a"

exit /b

:Fun
set str1=%~1
echo %str1%

exit /b

Re: Set a variable to file contents

Posted: 04 Feb 2013 07:05
by matt_thumper
foxidrive wrote:

Code: Select all

@echo off
set /p "a="<mydatefile.txt
echo "%a%"
pause


Worked great.
Just what I was looking for!

Thanks,
Matt
:D