Archive: how to edit a text file?


how to edit a text file?
Hi,

I'm installing an app with a config file that looks something like this:

<config>
<path>c:\my_dir\my.file</path>
</config>

I need to change "c:\my_dir" to the installation directory at install time (ex. to "d:\another_dir")

Can anyone help me get started? A simple find and replace text in a file would be perfect.

Any help is greatly appreciated, Jed


at this time there isn't "teh" xml parser plugin .... :rolleyes:

but you can parse that file.... isn't that hard... just loop with strstr until you'll find <path>...


there are various scripts for text-replacement in the archive


There are several text replacement examples provided with NSIS, but I found that I needed to dig a little deeper to come up with what I needed to do exactly what you're requesting. Here's the code that I came up with (which is cobbled together from a couple different sources, and then slightly modified):

Function searchReplace
ClearErrors
StrCpy $OLD_TEXT_EOL "$OLD_TEXT$CRLF"
FileOpen $SOURCE_FILE $FILENAME "r"
GetTempFileName $TMP_FILE
FileOpen $TARGET_FILE $TMP_FILE "w"
loop:
FileRead $SOURCE_FILE $CURRENT_STRING
IfErrors done
StrCmp $CURRENT_STRING $OLD_TEXT_EOL 0 compare_without_eol
FileWrite $TARGET_FILE $NEW_TEXT
FileWrite $TARGET_FILE $CRLF
; MessageBox MB_OK "Replaced text $OLD_TEXT"
Goto loop

compare_without_eol:
StrCmp $CURRENT_STRING $OLD_TEXT 0 write_original_string
FileWrite $TARGET_FILE $NEW_TEXT
; MessageBox MB_OK "Replaced text $OLD_TEXT"
Goto loop

write_original_string:
FileWrite $TARGET_FILE $CURRENT_STRING
Goto loop

done:
FileClose $SOURCE_FILE
FileClose $TARGET_FILE
Delete $SOURCE_FILE
CopyFiles /SILENT $TMP_FILE $FILENAME
Delete $TMP_FILE
FunctionEnd


To call this function:

StrCpy $OLD_TEXT "c:\my_dir"
StrCpy $NEW_TEXT "${INSTDIR}"
Call searchReplace


Re: how to edit a text file?

Originally posted by Yathosho
there are various scripts for text-replacement in the archive
<p>
Great, thanks! Found one that did the trick.
<p>
Jed