Batch file for comparing strings

January 13, 2009 at 21:30:59
Specs: Windows XP, pentium 4
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
echo Significant Incidents currently open as of
date /t
time /t
type "text.txt"
Ping.exe localhost -n 2 >NUL

GOTO start


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.

See More: Batch file for comparing strings

Report •

January 14, 2009 at 03:35:15
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 •

January 14, 2009 at 04:02:43
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 •

January 15, 2009 at 04:55:58
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.

Report •

Related Solutions

January 15, 2009 at 06:43:54
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 •

January 15, 2009 at 17:04:16
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 •

January 16, 2009 at 01:37:22
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 •

January 16, 2009 at 05:21:58
Alternatively, just put the setlocal command at the top of the file, before the :start label.

Report •

January 16, 2009 at 08:01:40
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 •

Ask Question