问题
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 typed out, store a temporary variable with the contents from that file and echo it ONLY when the file is modified.
What im trying to achieve is get the content from a text file, type it to the console and when the file is modified append with new contents from the text file. How do i achieve this?
heres my code
@ECHO OFF &setlocal
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
回答1:
There is an "Archive attribute". Whenever Windows changes a file, it sets this attribute. We can use this here:
@ECHO OFF &setlocal
SET "File=run.txt"
:check
timeout 1 >nul
attrib "%file%"|findstr /b "A" >nul || goto :check
REM attribute has changed, so the file has.
type "%file%"
attrib -A "%file%" &REM remove the archive attribute
goto :check
If you want to show the new lines only, a tiny little bit more code is necessary (get the number of lines):
@ECHO OFF &setlocal
SET "File=run.txt"
set "lines=0"
:check
timeout 1 >nul
attrib "%file%"|findstr /b "A" >nul || goto :check
more +%lines% "%file%"
attrib -A "%file%" &REM remove the archive attribute
for /f %%a in ('type "%file%"^|find /c /v ""') do set "lines=%%a"
goto :check
回答2:
The way I am thinking of it depends on who access the data, and what is being added when :/. Someone most certainly will have a better option, but here is my thought if you only want what is newly added to the run.txt.
NOTE set /p
will only grab the first line. So assuming there will only be one complete line entered at a time when the file is recreated empty the below should work. As you know your for loop will only grab the last line.
REM Still get first line if there is one
set /p prior_content=<Run.txt
REM Create new run.txt file that will be empty for new content
break>run.txt
REM after your date check of course
:notsame
REM Get new content from the empty run.txt that was created at the beginning
set /p new_content=<Run.txt
REM Echo it to CMD
ECHO %new_content%
REM Output to a log file
(
echo %new_content%
echo.
)>>Run_log.txt
I have done this in the past a bit differently, but I believe this will work depending on your need. If not I am sure someone smarter will come along and correct my errors.
If this is a time sensitive file I would also suggest logging a timestamp of when changes are made. You are doing a timeout of 1 second but are only checking the 10 character date so you will only ever see the change in the file once a day. I am sure you are aware but just in case you are not.
EDIT
As mentioned in the comments I failed to originally include (and remember myself) that set /p contents=<run.txt
will only pull the first line of the text file.
I have changed my half brained solution to reflect this fact. :D
来源:https://stackoverflow.com/questions/65712964/how-to-save-text-file-contents-into-a-temporary-variable-and-echo-it-only-if-tha