Computing.Net > Forums > Programming > Batch file for comparing strings

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

Batch file for comparing strings

Reply to Message Icon

Name: zendainc
Date: January 13, 2009 at 21:30:59 Pacific
OS: Windows XP
CPU/Ram: pentium 4
Manufacturer/Model: Custom / NOT APPLICABLE
Subcategory: Batch
Comment:

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.


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: January 14, 2009 at 03:35:15 Pacific
Reply:

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

Response Number 2
Name: zendainc
Date: January 14, 2009 at 04:02:43 Pacific
Reply:

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

Response Number 3
Name: lee123abc
Date: January 15, 2009 at 04:55:58 Pacific
Reply:

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

Response Number 4
Name: klint
Date: January 15, 2009 at 06:43:54 Pacific
Reply:

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

Response Number 5
Name: zendainc
Date: January 15, 2009 at 17:04:16 Pacific
Reply:

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

Related Posts

See More



Response Number 6
Name: Judago
Date: January 16, 2009 at 01:37:22 Pacific
Reply:

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

Response Number 7
Name: klint
Date: January 16, 2009 at 05:21:58 Pacific
Reply:

Alternatively, just put the setlocal command at the top of the file, before the :start label.


Report Offensive Follow Up For Removal

Response Number 8
Name: reno
Date: January 16, 2009 at 08:01:40 Pacific
Reply:

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
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file for comparing strings

Batch file for Outlook www.computing.net/answers/programming/batch-file-for-outlook/15439.html

Batch file for text replacement www.computing.net/answers/programming/batch-file-for-text-replacement/12883.html

Batch file for renaming folders www.computing.net/answers/programming/batch-file-for-renaming-folders/15430.html