save text file contents into a temporary variable and echo it only if that text file is modified

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
parnwham5bam
Posts: 2
Joined: 13 Jan 2021 03:47

save text file contents into a temporary variable and echo it only if that text file is modified

#1 Post by parnwham5bam » 13 Jan 2021 21:14

Currently what I have checks to see if a text file has been modified, if it hasn't then recheck else type out the file contents.

What im attempting to do is after the file contents have been tyuped out, store a temporary variable with the contents from that file and echo it ONLY when the file is modified. How do i achieve this?

heres my code

Code: Select all

@ECHO OFF &setlocal
color 2

set currentDate=%date%
SET File=run.txt
:check
FOR %%f IN (%File%) DO SET filedatetime=%%~tf
IF %filedatetime:~0, 10% == %currentDate% goto same

goto notsame

:same
goto next

:notsame
type %File%
for /f "tokens=*" %%A in (%File%) do (echo %%A)

:next
TIMEOUT /T 1 >nul
goto check

echo.
pause

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: save text file contents into a temporary variable and echo it only if that text file is modified

#2 Post by penpen » 15 Jan 2021 17:25

Depending on the file content it might be impossible to store that within an environment variable.

You could use the task scheduler to react on file changed events..., but that horribly sounds like cracking a walnut with a sledgehammer,
because you would get tons of events for dozens of temporary file accesses performed by windows itself.

Also your wording might be a hint, that you would know from within the batch, that you finsihed editing the file.
If that is the case, then you might want to redesign your program.


penpen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: save text file contents into a temporary variable and echo it only if that text file is modified

#3 Post by Squashman » 15 Jan 2021 18:37

This question seems to have something to do with your previous question.

Are you just trying to TAIL a file?

Post Reply