batch file read date from .txt and use as var

January 14, 2011 at 11:17:00
Specs: Windows XP
Hello.

I am trying to use a batch file to read a date string in a text file, assign that date value to a variable, then take further actions using the date variable to perform date specific file copies. The text file is structured, so the date I am trying to capture is always 23 rows down, 58 characters in from the left edge, and is 10 characters long (ex. 01/14/2011). Is there anyway I can capture this date and retain it as a variable that I can then use later in the batch file? Thanks in advance for any assitance you can offer.


See More: batch file read date from .txt and use as var

Report •

#1
January 15, 2011 at 07:35:13
Hi ukyo

@echo off
setlocal EnableDelayedExpansion
for /f "skip=23 tokens=* delims=" %%a in (Test4.txt) do (
set B=%%a
set D=!B:~58,10! & goto Pass
)
:Pass
echo Date=%D%


Report •

#2
January 15, 2011 at 08:11:15
Awesome! That works great. Thank you so much for the help! I think this will help me turn the corner on my copying / renaming job.

Report •

#3
January 15, 2011 at 08:27:40
Here's an expansion on my original question. I just found out that roughly 1/3 of the text files have the date at the position I noted earlier (they are all the same type of report), but other directories have reports that are not in the same structure. Is their a variant to this that will scan the whole file? I've tried a few iterations but I'm clearly not as good at this as I want to be.

Report •

Related Solutions

#4
January 15, 2011 at 12:11:52
Hi ukyo

Do any of the other lines in the files contain the "/" charactor and the date is the only numerical text on that line.


Report •

#5
January 15, 2011 at 15:34:49
Looking at the source files, no. There are other numeric characters in line with the date. However, all of the dates in these files are preceded by the words "run date" and an oddly chosen 8 digit date format like this: RUN DATE: 01/15/11. I thought I was on to something, as the date seemed to be on line 1, column 127 at first, but as I looked through the files I found some that had it on line 1, column 125. Is there a way to key in on that RUN DATE: string and grab the characters to the right of it?

Report •

#6
January 15, 2011 at 16:05:46
Hi ukyo
I see what I can come up with, if noboby posts a solution.



Report •

#7
January 16, 2011 at 14:30:00
Hi ukyo

Try this I,ve assumed that "RUN DATE" is follered by a colon and a space and the
date is in the format dd/mm/yy ie 8 chars only.
If this is not so the code will have to be modified.

Hope this helps.
---------------------------------
@echo off
SetLocal EnableDelayedExpansion

cls
for %%a in (Test4.txt) do (
call :FindIt %%a
echo File %%a RunDate=[!RunDate!]
)
del ~.txt
exit /b


:FindIt %*
findstr "RUN DATE" %*>~.txt
set /p Line=<~.txt
for /f "tokens=* delims=" %%b in ("!Line!") do (
call :StrLen !Line!
for /L %%c in (0,1,!Len!) do (
set Chars=!Line:~%%c,8!
if /i "!Chars!" EQU "RUN DATE" (
set Pos=%%c
set /a Pos+=10
for /L %%c in (!Pos!,1,!Pos!) do (
if %%c EQU !Pos! (
set RunDate=!Line:~%%c,8!
exit /b
)
)
)
)
)
exit /b


:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" (
set /a Len=%%a+1
) else (exit /b)
)
exit /b


Report •

#8
January 19, 2011 at 05:47:22
dtech10 -

Thank you for the help! I can't get this batch to work. If ran as is it closed too quickly to read the output. If I insert a pause in the upper section before the "del ~.txt" line I get a result stating the RunDate=[]. Looking closer at the text file, there are two spaces (RUN DATE: 01/18/11). I don't know how I missed that before. I tried modifying the code, but as noted above, I'm not as good at this as I would like to be.

Trying to rip this apart, I like what you have done. Extracting the first line of the source data to it's own file to limit the data the batch has to work with. Very clever! I would never have considered that option. Unfortunately, no matter how I modify it, I cannot make it work.


Report •

#9
January 19, 2011 at 12:53:58
H ukyo
When you say 2 spaces are they after the "RUN DATE:" colon before the date.
I inserted some Rem lines so you can understand the more.
You coukd use echo the test the code in certain places.
Hope this helps

:FindIt %*
REM Get line containing "RUN DATE"
findstr "RUN DATE" %*>~.tx
t
REM set var LINE to equal to above
set /p Line=<~.txt
echo Line=!Line!

for /f "tokens=* delims=" %%b in ("!Line!") do (
REM Get length of !Line!
call :StrLen !Line!

REM Count through the !Line! and display 8 chars string at position %%c
for /L %%c in (0,1,!Len!) do (
set Chars=!Line:~%%c,8!
echo Chars=!Chars!

REM if !Chars!="RUN DATE"
if /i "!Chars!" EQU "RUN DATE" (
REM Set Position to the first "R" of "RUN DATE"
set Pos=%%c
REM Add to Pos The offset ie 10 chars away is the start of the Date
REM Change this Pos offset to where the Date starts
set /a Pos+=10
echo Pos=!Pos!

REM This piece of code is used because I could use something
REM like !RunDate=:~Pos,8!, Batch isnt the best language for this.
REM but Batch doe,s accept a Loop Var.
for /L %%c in (!Pos!,1,!Pos!) do (
if %%c EQU !Pos! (
set RunDate=!Line:~%%c,8!
echo RunDate=!RunDate!
exit /b
)
)
)
)
)
exit /b


Report •

#10
January 19, 2011 at 17:39:44
Hi dtech10,

What about something like this?

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR %%a IN (*.txt) DO (
FOR /f "tokens=3 delims= " %%A IN ('TYPE %%a^| FIND /i "run date"') DO (
ECHO %%A
)
)

It's not setting them as a variable, but that's an easy fix.

Can I put a question here?


Report •

Ask Question