Search for a File and Replace the content of the file

(imported topic written by waynelee91)

Hi ,

Does anyone know how to search for a file in C:\ and replace the some part of the content in the file?

Example of the content of the file as follows:

Server abc Internet = 123.123.123.123 12345

I want to replace abc and 123.123.123.123 with new values.

How can i go about it?

Thanks!

(imported comment written by BenKus)

Hey waynelee,

There are ways to do this, but searching the whole hard drive takes a long time and it causes lots of disk usage, which can slow the system down.

Do you have an idea of where this file might be? Also, roughly how big do you expect the file to be?

Thanks,

Ben

(imported comment written by waynelee91)

Hi Ben,

The location could be either in C:\Matlab or C:\Program Files\Matlab

The file itself is very small. I think around 2k

Thanks!

Wayne

(imported comment written by BenKus)

See here for a similar question:

http://forum.bigfix.com/viewtopic.php?pid=5796#p5796

You will make a Fixlet with relevance that looks something like this:

exists file (“C:\matlab\myfile.ext”;“C:\Program Files\Matlab\myfile.ext”) whose (exists lines whose (it as lowercase contains “123.123.123.123”)of it )

The action will be similar to the one in the other post (change “myfile.ext” to the filename):

// store the file location – you can list multiple files locations, but if more than one of the locations exist, then the next line will fail
parameter “filename” = “{pathname of file (“C:\matlab\myfile.ext”;“C:\Program Files\Matlab\myfile.ext”) whose (exists lines whose (it as lowercase contains “123.123.123.123”)of it )}”

parameter “textToReplace” = "123.123.123.123"
parameter “newText” = “Replaced text”

// iterate through the file replacing lines as necessary
appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file
move “{parameter “filename”}” “{parameter “filename”}.bak”
// replace with the new file
move __appendfile “{parameter “filename”}”

I didn’t test at all so let me know if it fails and which line it failed…

Ben

(imported comment written by SystemAdmin)

So, to replicate this I’ve tried without success. What am I missing?

// store the file location

parameter “filename” = “{exists file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it as lowercase contains “HostPort=”)of it ) }”

parameter “textToReplace” = “HostPort=”

parameter “newText” = “//HostPort=”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

(imported comment written by BenKus)

Hi mjohns,

If it failed when you ran it, what line did it fail on?

Ben

(imported comment written by SystemAdmin)

Thanks Ben, as always for your prompt response.

Action Script Execution Detail

Completed

// store the file location

Completed

parameter “filename” = “{exists file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it as lowercase contains “HostPort=”)of it ) }”

Completed

parameter “textToReplace” = “HostPort=”

Completed

parameter “newText” = “//HostPort=”

Completed

// iterate through the file replacing lines as necessary

Failed

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

(imported comment written by BenKus)

Hmmm… I think I see the issue… I had the relevance reversed for the filename definition and the Fixlet relevance… I modified it above and try again and see if that works…

Sorry about that…

Ben

(imported comment written by SystemAdmin)

So this is what I’ve changed my action to:

// store the file location – store the file location or (“C:\Program Files\Software\config.cfg”;“C:\Software\config.cfg”)

parameter “filename” = “{pathname of file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it as lowercase contains “HostPort=”)of it )}”

parameter “textToReplace” = “HostPort=”

parameter “newText” = “//HostPort=”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

and I’m getting

Completed

// store the file location – you can list multiple files locations, but if more than one of the locations exist, then the next line will fail

Failed

parameter “filename” = “{pathname of file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it as lowercase contains “HostPort=”)of it )}”

parameter “textToReplace” = “HostPort=”

parameter “newText” = “//HostPort=”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

(imported comment written by SystemAdmin)

Bump

(imported comment written by BenKus)

Ah… this can never be true because you are comparing a “as lowercase” to something with a capital letter… Try this:

parameter “filename” = “{pathname of file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it as lowercase contains “hostport=”)of it )}”

Ben

(imported comment written by SystemAdmin)

That’s weird, now its failing at

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

(imported comment written by BenKus)

It might still be a case issue… can you verify the capitalization in the file is the same as what you provided in the action?

Ben

(imported comment written by SystemAdmin)

The file has show the text as such

HostPort=00000

I’ve changed the action a bit, this is what I have

// store the file location – store the file location or (“C:\Program Files\Software\config.cfg”;“C:\Software\config.cfg”)

parameter “filename” = “{pathname of file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it contains “HostPort=”)of it )}”

parameter “textToReplace” = “HostPort=”

parameter “newText” = “//HostPort=”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

It still fails at

Failed

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) ) of lines of file (parameter “filename”)}

(imported comment written by BenKus)

OK… I think this was all my fault because I forgot the “else” clause:

// store the file location – store the file location or (“C:\Program Files\Software\config.cfg”;“C:\Software\config.cfg”)
parameter “filename” = “{pathname of file (“C:\Program Files\Software\config.cfg”) whose (exists lines whose (it contains “HostPort=”)of it )}”

parameter “textToReplace” = "HostPort="
parameter “newText” = “//HostPort=”

// iterate through the file replacing lines as necessary
appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace”)) then ((preceding text of first (parameter “textToReplace”) of it) & (parameter “newText”) & (following text of first (parameter “textToReplace”) of it) ) else it ) of lines of file (parameter “filename”)}

// backup the old file
move “{parameter “filename”}” “{parameter “filename”}.bak”
// replace with the new file
move __appendfile “{parameter “filename”}”

I still haven’t fully tested it, but it should eliminate that problem… Sorry about that…

Ben

(imported comment written by SystemAdmin)

Thank you again

That did it