Old 18th January 2005, 19:03   #1
JedBartlett
Junior Member
 
Join Date: Jan 2005
Posts: 4
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
JedBartlett is offline   Reply With Quote
Old 18th January 2005, 19:26   #2
Joel
Debian user
(Forum King)
 
Joel's Avatar
 
Join Date: Jan 2003
Location: Arch land
Posts: 4,917
at this time there isn't "teh" xml parser plugin ....

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


* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with MATE.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with MATE.
Joel is offline   Reply With Quote
Old 18th January 2005, 19:27   #3
Yathosho
Forum King
 
Yathosho's Avatar
 
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
there are various scripts for text-replacement in the archive
Yathosho is offline   Reply With Quote
Old 18th January 2005, 19:30   #4
FitzChivalry
Junior Member
 
Join Date: Nov 2004
Location: Charleston, SC
Posts: 2
Send a message via Yahoo to FitzChivalry
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
FitzChivalry is offline   Reply With Quote
Old 18th January 2005, 20:44   #5
JedBartlett
Junior Member
 
Join Date: Jan 2005
Posts: 4
Re: how to edit a text file?

Quote:
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
JedBartlett is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump