Change text in file

(imported topic written by anthonymap91)

I am trying to just change text in a file. The action i have completes successful, but does not appear to change the file. Please help.

original content of file:

“managedby=DI-Managed-EIT,patchstatus=patch-now,”

Need changed to:

“managedby=DI-Managed-EIT,patchstatus=patch-complete,”

Action:

// store the text to replace

parameter “textToReplace” = “patch-now”

//store text to put in place***

//OS:win

if { name of operating system as string as lowercase contains “win”}

parameter “newtext” = “patch-complete”

parameter “filename-win” = “{pathname of file (“C:\WINDOWS\system32\DIINFO.txt”) whose (exists lines whose (it contains “patch-now”)of it )}”

// 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-win”)}

1 Like

(imported comment written by anthonymap91)

I also tried this, but the move __appendfile “{parameter “filename-win”}” fails:

// store the text to replace

parameter “textToReplace” = “patch-now”

parameter “newtext” = “patch-complete”

//store text to put in place****

//OS:win

if { name of operating system as string as lowercase contains “win”}

parameter “filename-win” = “{pathname of file (“C:\WINDOWS\system32\DIINFO.txt”) whose (exists lines whose (it contains “patch-now”)of it )}”

// 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-win”)}

// backup the old file

copy “{parameter “filename-win”}” “{parameter “filename-win”}.bak”

// replace with the new file

move __appendfile “{parameter “filename-win”}”

(imported comment written by NoahSalzman)

I haven’t tried this myself, but I believe you need

parameter “” of action as string

instead of

parameter “”

in the “appendfile” line.

(imported comment written by anthonymap91)

Still no good…

// store the text to replace

parameter “textToReplace” = “patch-now”

parameter “newtext” = “patch-complete”

//**store text to put in place

//OS:win

if { name of operating system as string as lowercase contains “win”}

parameter “filename-win” = “{pathname of file (“C:\WINDOWS\system32\DIINFO.txt”) whose (exists lines whose (it contains “patch-now”)of it )}”

// 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-win”)}

// backup the old file

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

// replace with the new file

move __appendfile (parameter “filename-win”) of action as string

Action output:

STATUS: Running action…

Command succeeded parameter “textToReplace” = “patch-now”

Command succeeded parameter “newtext” = “patch-complete”

Command succeeded parameter “filename-win” = “C:\WINDOWS\system32\DIINFO.txt”

Command succeeded 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-win”)}

Command succeeded (file created) appendfile “managedby=DI-Managed-EIT,patchstatus=patch-complete,”

Command succeeded appendfile “managedby=DI-Managed-EIT,patchstatus=patch-complete,”

Command succeeded move “C:\WINDOWS\system32\DIINFO.txt” “C:\WINDOWS\system32\DIINFO.txt.bak”

Command failed (Move failed (2)) move __appendfile (parameter “filename-win”) of action as string

  • Result —

Evaluation failed!

(imported comment written by NoahSalzman)

Sorry, I wasn’t clear, I meant the first appendfile line. Try this (once again, I haven’t tried this myself):

if { name of operating system as string as lowercase contains “win”}

parameter “filename-win” = “{pathname of file (“C:\WINDOWS\system32\DIINFO.txt”) whose (exists lines whose (it contains “patch-now”)of it )}”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace” of action as string)) then ((preceding text of first (parameter “textToReplace” of action as string) of it) & (parameter “newtext” of action as string) & (following text of first (parameter “textToReplace” of action as string) of it) ) else it ) of lines of file (parameter “filename-win” of action as string)}

// backup the old file

copy “{parameter “filename-win”}” “{parameter “filename-win”}.bak”

// replace with the new file

move __appendfile “{parameter “filename-win”}”