Page 1 of 1

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

Posted: 13 Jan 2021 21:14
by parnwham5bam
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

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

Posted: 15 Jan 2021 17:25
by penpen
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

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

Posted: 15 Jan 2021 18:37
by Squashman
This question seems to have something to do with your previous question.

Are you just trying to TAIL a file?