Archive: write in the middle of text file


write in the middle of text file
i would like to know how is possible to write in the middle of text file; for example i suppose to have a text file with 5 line

row1
row2
row3
row4
row5

i would like to have a output file as:

row1
row2
rowx
row3
row4
row5

with a new line inserted in the middle, i would like to know if i have to use fileseek command and how use it. thanks so much for help. bye. Andre


You can do it using Text Functions Header, there is an example in documentation tailored for your case.

http://nsis.sourceforge.net/Docs/AppendixE.html#E.2


see above
i've however some problem beacuse i have to use a tmp file i suppose, have i cut the line below my line, copy it to another tmp file, insert my line and copy lines from tmp to my file? are there other ways? thanks so much, bye. Andre


The example4 on LineFind does all the job.


see above
i have the text below, i don't understand why don't work, i've prova.txt file with line of text and i would like to write something 2 lines up the last row but don't write, nothing, why? thanks so much

!define PRODUCT_NAME "a"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "La mia compagnia, Inc."
!define PRODUCT_WEB_SITE "http://www.lamiacompagnia.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\nsis-2.24-setup.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

!include "MUI.nsh"
!include "TextFunc.nsh"


!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "lic.txt"
; Directory page
!insertmacro MUI_PAGE_DIRECTORY

!insertmacro linefind

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES

; Finish page
!define MUI_FINISHPAGE_RUN "$INSTDIR\nsis-2.24-setup.exe"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\a"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

Section "SezionePrincipale" SEC01
SetOutPath "$INSTDIR"
SetOverwrite ifnewer
File "nsis-2.24-setup.exe"
CreateDirectory "$SMPROGRAMS\a"
CreateShortCut "$SMPROGRAMS\a\a.lnk" "$INSTDIR\nsis-2.24-setup.exe"
CreateShortCut "$DESKTOP\a.lnk" "$INSTDIR\nsis-2.24-setup.exe"


iffileexists "$INSTDIR\prova.txt" occhio
File "prova.txt"
occhio:

${LineFind} "$INSTDIR\prova.txt" "" "-2" "Example4"
IfErrors 0 +2
MessageBox MB_OK "Error"

SectionEnd

Section -AdditionalIcons
CreateShortCut "$SMPROGRAMS\a\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
WriteUninstaller "$INSTDIR\uninst.exe"
WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\nsis-2.24-setup.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\nsis-2.24-setup.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

Function Example4
FileOpen $R4 "$INSTDIR\prova.txt" a
FileWrite $R4 "---First Line---$\r$\n"
FileClose $R4
FunctionEnd

Function un.onInit
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Sei sicuro di voler completamente rimuovere $(^Name) e tutti i suoi componenti?" IDYES +2
Abort
FunctionEnd

Function un.onUninstSuccess
HideWindow
MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) è stato completamente rimosso dal tuo computer."
FunctionEnd

Section Uninstall
Delete "$INSTDIR\uninst.exe"
Delete "$INSTDIR\prova.txt"
Delete "$INSTDIR\nsis-2.24-setup.exe"

Delete "$SMPROGRAMS\a\Uninstall.lnk"
Delete "$DESKTOP\a.lnk"
Delete "$SMPROGRAMS\a\a.lnk"

RMDir "$SMPROGRAMS\a"
RMDir "$INSTDIR"

DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
SetAutoClose true
SectionEnd


see above
ok, i've solved tha problem, work properly, seeing another example on forum, thanks so much however.


I think you're using the example incorrectly,

Section
${LineFind} "$INSTDIR\prova.txt" "" "-2" Example4
IfErrors 0 +2
MessageBox MB_OK "Error"
SectionEnd

Function Example4
FileWrite $R4 "---First Line---$\r$\n"
FileWrite $R4 "---Second Line ...---$\r$\n"

Push $0
FunctionEnd

Also looking your code, (IfFileExists "$INSTDIR\prova.txt"), I think you're writing to the file in both cases, either exists or extract it, is this what you want?

see above
yes red wine, i've understand your suggest, and also i've understood error for differentiate the 2 ways. thanks so much.bye. Andre