Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Input Data from a text file into a batch file variable.

Hello all,

I have a small problem, I have a batch file that I wrote (see code below) that I need to input data from a text file into a variable within the batch file.  The data the the text file will contain is simply today's date in the following format (Mon 04/03/2005) which will be read into the batch file and be used the following day.  In short what I'm trying to accomplish is a date misus one calculation without writing some big log script to do the calculations.  I wrote a VB app that does exactly what I need it to do, but the securtiy nazi's around here told me that I can't use exe's or VB scripts on the servers...

Code Sample:

@echo off

rem setting the current date

Rem TempDate <--  This is the variable that I want to get from another file.

set yy=%date:~12,2%
set mm=%date:~4,2%
set dd=%date:~7,2%

set name=ex%yy%%mm%%dd%.log

rem creating file name using current date and
rem copying to another location using the same name.
rem change copy command to reflect desired locations.

copy c:\temp\ex%yy%%mm%%dd%.log c:\temp2\ex%yy%%mm%%dd%.log

1 Solution
Assuming this date is the only line in the text file, simply use something like this (NT4 or later):

@echo off
set DateFile=C:\temp\test.txt
for /f "delims=" %%a in ('type "%DateFile%"') do set TempDate=%%a
PJan8724Author Commented:
Hey oBda,

Thanks for the help, I actually came up with a similar solution yesterday after doing some more research but yours is a little better then mine so I'm going to go with.  However I did want to say thanks for all you help.

Pete J.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now