Jump to content

Using list to replace a value in multiple files


Recommended Posts

Hi,

it's been awhile since i last coding.

anyway, i have a list in a file in animal.txt.

e.g

cat
dog
chicken
mouse

then i want to use that list to replace string "something" in multiple files.

e.g

in mypet-1.txt

i have a something.

mypet-2.txt

i have a something.

mypet-3.txt

i have a something.

mypet-4.txt

i have a something.

Can somebody can come out with a simple script?

 

Link to comment
Share on other sites

Arrays and For loops.

#include <File.au3>
Global $array
Global $array2
_FileReadToArray("C:\users\%username%\desktop\animal.txt", $array)
_FileReadToArray("C:\users\%username%\desktop\file2.txt", $array2)

For $i = 1 To $array[0]
_ReplaceStringInFile($array2[$i], "something", $array[$i])
Next

In this example file2.txt has:

C:\users\%username%\desktop\mypet-1.txt
C:\users\%username%\desktop\mypet-2.txt
C:\users\%username%\desktop\mypet-3.txt
C:\users\%username%\desktop\mypet-4.txt

If you want to change the text for all txt files in a directory until you run out of words in your animals.txt you can replace:

_FileReadToArray("C:users%username%desktopfile2.txt", $array2)

With

$array2 = _FileListToArray("C:path-to-folder-with-myfiles")

And change the ReplaceString function to_ReplaceStringInFile("C:path-to-folder-with-myfiles" & $array2[$i], "something", $array[$i])

*Edited for a few revisions after I reread replacing the string instead of simply writing it to the file.

Edited by blckpythn
Link to comment
Share on other sites

You're the man! My mind is clearer now.

except that : 

ReplaceString function to_ReplaceStringInFile("C:\path-to-folder-with-myfiles\" & $array[$i], "something", $array[$i])

to

ReplaceString function to_ReplaceStringInFile("C:\path-to-folder-with-myfiles\" & $array2[$i], "something", $array[$i])

Anyway thanks.

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...