sml41
asked on
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
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.
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.
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
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
ASKER
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
thanks
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!