Jump to content

Search Text File For Words in other text


 Share

Recommended Posts

hello all, ive got 2 text files, text file #1 contains a list of files/folders each file/folder is displayed on a different line, text file #1 also contains the same list on files/folders

$sFileName = @TempDir & "\copytemp.txt"

FileOpen($sFileName, 0)

While 1

$sWord = FileReadLine(@TempDir & "\origtemp.txt")
$sText = FileRead($sFilename,FileGetSize($sfilename))

If StringInStr($sText,$sWord) Then 
   MsgBox(0,'', "Word is found in file!")
Else 
   Msgbox(0,'', "not found")
EndIf
Wend

FileClose($sFileName)

i am using that script to search the second file for the files/folders contained int he first. problem is it doesnt stop when the EOF is reached and i get an infinite amount of "Word is found in file", also, im not sure if it is going to a new line of text file #1 each time, i am using this to check if 2 directories contain the same files, any help plz :idiot:

Edited by burrup

qq

Link to comment
Share on other sites

$sFile = @TempDir & "\copytemp.txt"

$sOrig = @TempDir & "\origtemp.txt"

$sFileLines = _FileCountLines($sFile)

$sOrigLines = _FileCountLines($sOrig)

For $i = 1 to $sOrigLines

$sOrigLineText = FileReadLine($sOrig, $i)

$sFileLineText = FileReadLine($sFile, $i)

If StingInStr($sFileLineText, $sOrigLineText) Then

MsgBox(0, "", "File Found")

Else

MsgBox(0, "", "Not Found")

EndIf

Next

That should work, but it might take a while, actually I realized that the word would have to be in a corresponding line.

Edited by the_lord_mephy
My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]
Link to comment
Share on other sites

at the end, sorry, I was in a rush when I wrote it.

Edited by the_lord_mephy
My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]
Link to comment
Share on other sites

  • Developers

try this version:

; read whole file into a variable
$sFileName = @TempDir & "\origtemp.txt"
$sText = FileRead($sFileName, FileGetSize($sFileName))
; open second file 
$H_Input = FileOpen(@TempDir & "\copytemp.txt", 0)
;
While 1
    $sWord = FileReadLine($H_Input)
    If @error Then ExitLoop
    If StringInStr($sText, $sWord) Then
        MsgBox(0, '', "Word is found in file!")
    Else
        MsgBox(0, '', "not found")
    EndIf
WEnd

FileClose($sFileName)
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

damn im stuck again lol, heres my situtation... "origtemp" contains a list of file/folders from a directory, i copied the actual files/folders to another directory, and "copytemp" contains the files/folders again, so in this case both text documents contain the same list of files and folders,

i am working on the above script, so if all or one of the files/folders arent in the list, it will say ONE msg box (blah blah erroe didnt work)

wat i need the script to do is...

> compare the contents of the to folders

> even if "copytemp" as other files listed AND has all the ones contained on origtemp it will say it worked

> IF even atleast ONE of the files in the list arnt there, it will give msgbox(0, "error" blah blah)

and help plz :idiot:

edit: so wat i really need it to do is not do it line by line but compare the WHOLE files and if it has the origtemp list anywhere in it, it can have more listed, but as long as it has all the files listed that are in origtemp u get copied successfully

Edited by burrup

qq

Link to comment
Share on other sites

  • Developers

What about this approach ?

$Source="c:\source"
$Target = "c:\target"
; read whole file into a variable
$sFileName = @TempDir & "\origtemp.txt"
$sText = FileRead($sFileName, FileGetSize($sFileName))
; open second file 
$H_Input = FileOpen(@TempDir & "\copytemp.txt", 0)
;
While 1
    $sWord = FileReadLine($H_Input)
    If @error Then ExitLoop
    $sWord = $Target & StringTrimLeft($sWord,StringLen($Source))    
    If StringInStr($sText, $sWord) Then
        MsgBox(0, '', "Word is found in file!")
    Else
        MsgBox(0, '', "not found")
    EndIf
WEnd

FileClose($sFileName)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

hey, i just figured it out lol, so happy, what i done is count the lines in the first file and if it finds the string the counted number its all good :idiot:

#include <file.au3>

$CountLines = _FileCountLines(@TempDir & "\origtemp.txt")
$sFileName = @TempDir & "\origtemp.txt"
$sText = FileRead($sFileName, FileGetSize($sFileName))
$H_Input = FileOpen(@TempDir & "\copytemp.txt", 0)
$count = 0
While 1
    $sWord = FileReadLine($H_Input)
    If @error Then ExitLoop
    If StringInStr($sText, $sWord) Then
        $count = $count + 1
    Else
        $count = $count
    EndIf
WEnd

If $Count = $CountLines Then
msgbox(0, "", "did this work?")
Else
msgbox(0, "", "it didnt work")
EndIf

qq

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...