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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mark2150Connect With a Mentor Commented:
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.