JedBartlett
18th January 2005 20:03 UTC
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
Joel
18th January 2005 20:26 UTC
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>...
Yathosho
18th January 2005 20:27 UTC
there are various scripts for text-replacement in the archive
FitzChivalry
18th January 2005 20:30 UTC
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
JedBartlett
18th January 2005 21:44 UTC
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