Sign in to follow this  
Followers 0
TheCrimsonCrusader

Read contents from one file to search and replace in another

3 posts in this topic

I am wanting to read the contents of one text file (c:\readfile.txt) and then should the contents of the c:\readfile.txt exist in another file (c:\writefile.txt), have those contents deleted from the c:\writefile.txt.

So after reading the contents of c:\readfile.txt, which is:

Sentence 1

Sentence 2

Sentence 3

Sentence 4

Sentence 5

If at least the same contents exist in c:\writefile.txt as shown below:

Sentence 1

Sentence 2

Sentence 3

Sentence 4

Sentence 5

Sentence 6

Sentence 7

Sentence 8

Sentence 9

Sentence 10

Then c:\writefile.txt would be edited to look like the following:

Sentence 6

Sentence 7

Sentence 8

Sentence 9

Sentence 10

============================================

I understand the string search and replace option when specifying a specific string to replace, but not when reading the entire file contents to then have it replaced in another file should the same contents exist. I tried the code below, but I wasn't confident it would work despite it looking logical to me. :) Any ideas? The file might be better off being read to an array, but I still wasn't grasping the concept of getting the contents replaced in the other file should the contents exist. Thanks for any assistance on this.

CODE
#include <File.au3>

$readfile=FileRead("c:\readfile.txt")

_ReplaceStringInFile("c:\writefile.txt",$readfile,"")

Share this post


Link to post
Share on other sites



In file.au3, the _ReplaceStringInFile function reads the input function and splits it in an array according to lines (separations of @CR). Your search query spans multiple lines so it will never have a match.

Maybe you can concoct your own function from StringReplace() and StringInStr()

$file1=FileRead("c:\readfile.txt")
$file2=FileRead("c:\writefile.txt")

If StringInStr($file1, $file2) Then
   $file2=StringReplace($file2, $file1, "")
   $handle=FileOpen("c:\writefile.txt", 2)
   Filewrite($handle, $file2)
   FileClose($handle)
Endif

Share this post


Link to post
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
Sign in to follow this  
Followers 0