Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi I am currently trying to create a batch file to read text from a file, display it on the screen updating it every 2 seconds and beep if it finds something new, I have been able to get it to do everything except compare strings and google has yielded no results. So far in order to read and display the data I have the following
@echo off
color f9
title Service Desk - Significant Incident Feed
:start
cls
echo.
echo.
echo Significant Incidents currently open as of
echo.
date /t
time /t
echo.
echo.
echo.
echo.
type "text.txt"
Ping.exe localhost -n 2 >NUL
echo.
echo.GOTO start
pause
Could anyone advise how I might be able to make it write the input to a string, compare the strings then beep if they do not match.
Do you want to read the contents of the whole file text.txt into a single environment variable? There are limits to variable size that may be exceeded if text.txt gets large.
Report Offensive Follow Up For Removal
Ideally I would like the batch file to read the whole document which would be a series of lines of the following format
reference_number application time date
However as it would have a unique reference number at the beginning of each line it i suppose it could be setup to read only the first word/reference number/string of each line.
Report Offensive Follow Up For Removal
Mechanix2GO... suggestions?
I tried something but had problems comparing information in the files.Hopefully someone can come up with a suggestion as I would like to see how it is done.
Cheers
Report Offensive Follow Up For Removal
Assuming there is a space immediately after the reference number on each line, the following snippet collects all the reference numbers into one variable and compares it with the old one. You can copy & paste it into your application.
setlocal enabledelayedexpansion set oldcontents=%contents% set contents= for /f %%a in (text.txt) do ( if not defined contents ( set contents=%%a ) else ( set contents=!contents!;%%a ) ) if not !contents! == !oldcontents! ( echo New file contents! )But why go to all that trouble? Why not just check file modification time instead?
Report Offensive Follow Up For Removal
Also when I attempt to run the above suggestion in my batch file I receive "Maximum setlocal recursion level reached" over and over again. I am running the whole thing in a loop so that the batch file keeps up to date information. Is there a way to avoid this ?
Report Offensive Follow Up For Removal
Add endlocal at the end of klints script, I'm not sure if you need any of the variables that were set afterwards or not. If you need them all:
endlocal&set oldcontents=%oldcontents%&set contents=%contents%Like I said I'm not sure if you need the variables or not, in fact it could even be detrimental.
Report Offensive Follow Up For Removal
Alternatively, just put the setlocal command at the top of the file, before the :start label.
Report Offensive Follow Up For Removal
if you only need to display difference on the screen, you can use fc.exe. but if you need the variables for the next code, please use the above code:
::Compare.bat (File as %1) @echo off if not exist "%~1.old" copy %1 "%~1.old" fc %1 "%~1.old">nul || for %%a in (date/t time/t fc/n copy) do %%a %1 "%~1.old" ping -n 2 localhost >nul & %0 %1
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |