Set a variable to file contents

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
matt_thumper
Posts: 2
Joined: 04 Feb 2013 05:50

Set a variable to file contents

#1 Post by matt_thumper » 04 Feb 2013 05:56

:?:
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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Set a variable to file contents

#2 Post by foxidrive » 04 Feb 2013 06:01

Code: Select all

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

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: Set a variable to file contents

#3 Post by shirulkar » 04 Feb 2013 06:08

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

matt_thumper
Posts: 2
Joined: 04 Feb 2013 05:50

Re: Set a variable to file contents

#4 Post by matt_thumper » 04 Feb 2013 07:05

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

Post Reply