text file

how can i check if a text or sentence already exist in a text file before i write to it and dublicate it.

thanks
sml41Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark2150Commented:
Open the file and use INSTR() to compare your new string to the contents of the file.

AppendFlag = True
MySentence = "This is my sentence"
FileID = FreeFile
Open "TextFile.Txt" for input as #FileID
Do While Not EOF(FileID)
  Line Input #FileID, ALineOfText
  IF INSTR( UCASE(ALineOfText), UCASE(MySentence)) > 0 Then
  ... MySentence is already in file
    AppendFlag = False
    Exit Do
  ENDIF
LOOP
'
Close #FileID
'
If not appendFlag then
  FileId = Freefile
  Open "TextFile.Txt" FOR APPEND as #FileID
  Print #FileID, MySentence
  Close #FileID
Endif
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcriderCommented:
mark2150,

In your example, you forgot the starting point of InStr...  It should be:


   IF INSTR(1, UCASE(ALineOfText), UCASE(MySentence)) > 0 Then


Also, you don't have to take the time to convert your arguments in the InStr function to uppercase...

Instead of doing:

   IF INSTR(1, UCASE(ALineOfText), UCASE(MySentence)) > 0 Then
 

You can do:

   IF INSTR(1, ALineOfText, MySentence,1) > 0 Then


Cheers!
0
grumpy147Commented:
mcrider,

If one does not specify the starting point for the Instr function then it defaults to 1 - so your comment is unnecessary as it gives the impression that Mark2150's comment is in error.

I assume the value of "1" for the last argument is equivalent to using the constant "vbTextCompare" which indicates that the comparison is case insensitive - using the pre-defined VB constant makes your point clearer.


Mark2150,

Your code suggests that the text being sought will be contained within a single line of the file - however this may not be the case; not clear from the question.


sml41,

If you are sure that the text you are looking for will be contained within a single line of the text file, then Mark2150's suggestion is correct; otherwise I would suggest the following (the syntax is probably incorrect as I do not have VB on this PC, but look at the on-line help) though I am open to correction, as ever !


        strContents = vbnullstring
        MySentence = "This is my sentence"
        FileID = FreeFile
        Open "TextFile.Txt" for input as #FileID len 8192  
        Do until Eof(#FileID)
           AlineOfText = Input (8192, #FileID)
           strContents = strContents & ALineOfText
        Loop
        Close #FileID
       
    if( InStr( strContents, MySentence, vbTextCompare)= 0) then
           FileId = Freefile
           Open "TextFile.Txt" FOR APPEND as #FileID
           Print #FileID, MySentence
           Close #FileID
    Endif
 



0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

DaveWaltonCommented:
mcrider,

mark2150 is performing the conversion to upper case as it is not specific in the question as to whether an exact match is required.  Converting both the line and the search strings to upper case will find a non-specific case match.
0
mcriderCommented:
DaveWalton,

Like I said, you don't have to take the processor time to convert the case... This will perform the comparison with insensitive case:

IF INSTR(1, ALineOfText, MySentence,1) > 0 Then


0
sml41Author Commented:
thanks all, I never thought this will cause a debate, i have tried the solution with 819.... it gave errors, i could not follow most of the discussion due to my capabilities, but it is intersting..

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.