Reading a text file into a text box using nsdialog? NSIS - nsis

I am new to NSIS scripting installer. I need to create a TextBox with Multiline support, in Custom page. Need to read a text file and set the text content to TextBox. Please find my code block below:
StrCpy $3 ""
FileOpen $4 "C:\Users\Surya\Desktop\Installer\License.txt" r
loop:
FileRead $4 $1
StrCpy $3 "$3$1" ; append the line and copy it to another variable
IfErrors +1 loop
FileClose $4
${NSD_SetText} $ctrlTextBox "$3"
The above code able to read only 8119 characters only, but my file contains 30,000+ characters.
Please help me to read the large file and set the content to TextBox.
Thank You

You can fill a textbox with a little bit of text at the time (inside your loop) if you use EM_SETSEL (twice) to move the caret to the end and then use EM_REPLACESEL to append text.
If you can use a rich edit box instead then use some code I wrote a long time ago, you can find the forum thread here...
Edit:
As long as the textbox is empty when you begin you don't have to deal with the caret:
function custcreate
nsDialogs::Create 1018
Pop $0
nsDialogs::CreateControl ${__NSD_Text_CLASS} ${__NSD_Text_STYLE}|${ES_MULTILINE}|${WS_VSCROLL}|${ES_READONLY} ${__NSD_Text_EXSTYLE} 0 0 100% 50u ""
Pop $0
FileOpen $4 ${__FILE__}" r
loop:
FileRead $4 $1
SendMessage $0 ${EM_REPLACESEL} 0 "STR:$1"
IfErrors +1 loop
FileClose $4
nsDialogs::Show
functionend

Related

nsis read directory from txt files?

I am trying read the directory from text file but it did't working!
the test.txt is a two lines file ,a line per directory.
anyone can help me?
; example1.nsi
;
; This script is perhaps one of the simplest NSIs you can make. All of the
; optional settings are left to their default settings. The installer simply
; prompts the user asking them where to install, and drops a copy of example1.nsi
; there.
;--------------------------------
; The name of the installer
Name "Example1"
; The file to write
OutFile "example1.exe"
; The default installation directory
InstallDir $DESKTOP
; Request application privileges for Windows Vista
RequestExecutionLevel admin
!include "LogicLib.nsh"
!include FileFunc.nsh
ShowInstDetails show
;--------------------------------
; Pages
;--------------------------------
Function .onInit
functionend
; The stuff to install
Section "" ;No components page, name is not important
FileOpen $0 "test.txt" r
;FileSeek $4 1000 ; we want to start reading at the 1000th byte
FileRead $0 $1 ; we read until the end of line (including carriage return and new line) and save it to $1
FileRead $0 $2
;FileRead $4 $2 10 ; read 10 characters from the next line
FileClose $0 ; and close the file
DetailPrint $1
DetailPrint $2
CopyFiles $1 "D:\Desktop\taobao"
; Set output path to the installation directory.
SetOutPath $INSTDIR
SectionEnd ; end the section
FileOpen $0 "test.txt" r is problematic for two reasons.
You are not using a full path.
I'm assuming this file does not already exist on the end-users machine. You probably need to extract it from the installer before you can read it.
Another problem is that FileRead includes the newline characters in the returned string and you must remove them if you don't need them. Newline characters are not valid in paths on Windows.
; Create simple text file for this example:
!delfile /nonfatal "myexample.txt"
!appendfile "myexample.txt" "Hello$\r$\n"
!appendfile "myexample.txt" "W o r l d$\r$\n"
!include "StrFunc.nsh"
${StrTrimNewLines} ; Tell StrFunc.nsh to define this function for us
Section
SetOutPath $InstDir
InitPluginsDir ; Make sure $PluginsDir exists (it is automatically deleted by the installer when it quits)
File "/oname=$PluginsDir\data.txt" "myexample.txt" ; Extract our myexample.txt file to $PluginsDir\data.txt
FileOpen $0 "$PluginsDir\data.txt" r
FileRead $0 $1
${StrTrimNewLines} $1 $1
FileRead $0 $2
${StrTrimNewLines} $2 $2
FileClose $0
DetailPrint "Line 1=$1"
DetailPrint "Line 2=$2"
SectionEnd

Deregistering a font with NSIS

When I try to uninstall a font like that...
Section "un.Uninstall"
StrCpy $FONT_DIR $FONTS
!insertmacro RemoveTTFFont "$FONTS\Vani.ttf"
!insertmacro RemoveTTFFont "$FONTS\Vanib.ttf"
SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
SectionEnd
I get the following error message:
Error in macro GetFileNameCall on macroline 2
Error in macro RemoveTTFFont on macroline 9
(...) aborting process
In other words, there's something wrong with the following section in the FontReg.nsh file:
!ifmacrondef GetFileNameCall
!macro GetFileNameCall _PATHSTRING _RESULT
Push `${_PATHSTRING}`
Call GetFileName
Pop ${_RESULT}
!macroend
!endif
!ifndef GetFileName
!define GetFileName `!insertmacro GetFileNameCall`
Function GetFileName
Exch $0
Push $1
Push $2
StrCpy $2 $0 1 -1
StrCmp $2 '\' 0 +3
StrCpy $0 $0 -1
goto -3
StrCpy $1 0
IntOp $1 $1 - 1
StrCpy $2 $0 1 $1
StrCmp $2 '' end
StrCmp $2 '\' 0 -3
IntOp $1 $1 + 1
StrCpy $0 $0 '' $1
end:
Pop $2
Pop $1
Exch $0
FunctionEnd
!endif
Can someone, if not tell me how to fix the bug, at least point me in the right direction?
It would be useful for the community as many have had this problem but no one has solved it yet, like here - http://forums.winamp.com/showthread.php?t=245701
I haven't received any answers unfortunately, but I must share the solution I came up with, since I saw that lots of people have had the same problem.
There is a bug in macros to remove fonts, namely "RemoveTTF", "RemoveTTFFont" and similiar sounding ones in the following files : FontReg.nsh, FontRegAdv.nsh. All of them use the same function called "GetFileNameCall" which causes the error. The problem with this function is that it sees "FontName" and "FontFileName" as the same item! As a matter of fact, font file name differs from font name. I solved the problem by copying the needed code from FontRegAdv.nsh and replacing FontFileName and FontName variables with the actual font file names and font names.

NSIS write to a file on APPDATA inside a subdir

I'm trying to append 1 line of text into a file on the $APPDATA folder which is inside of a folder that's generated randomly, so I don't know it's full path like:
C:\Users\MyUser\AppData\Roaming\MyApp\RANDOM_CRAP\config.json
While RANDOM_CRAP looks like some random string for a folder, like G4F6Hh3L.
What are my options here? Do I need to use either Search For a File or Search for a File or Directory (Alternative) ? It's a given that the only subfolder of MyApp folder is the RANDOM_CRAP folder, that contains the file I want to edit.
If there's no other way to access this file without searching for it, I've tried doing so but couldn't get this to work. (I'm very new to NSIS)
This is what I've tried (With the alternative approach):
Push "config.json"
Push "$APPDATA"
Push $0
GetFunctionAddress $0 "myCallback"
Exch $0
Push "1" ; include subfolders because my desired file is in the random folder
Push "0" ; no need the . option
Call SearchFile
Than I've copied the SearchFile code from this post and put a callback:
Function myCallback
Exch 3
Pop $R4
MessageBox MB_OK "Callback executing!"
MessageBox MB_OK "File is at : $R4"
FunctionEnd
I know that SearchFile is running (I've put a MessageBox inside) but myCallback isn't seemed to be called.
Many thanks.
If you are looking for a known file and only one directory in the path is unknown then you can probably just do a basic FindFirst search:
Section
; Create "random" folders:
CreateDirectory "$temp\MyApp\foo"
System::Call kernel32::GetTickCount()i.r1 ; random enough
CreateDirectory "$temp\MyApp\bar$1"
FileOpen $0 "$temp\MyApp\bar$1\config.json" a
FileWrite $0 '{bogus:"data"}$\n'
FileClose $0
CreateDirectory "$temp\MyApp\baz"
!include LogicLib.nsh
; Do the actual search:
StrCpy $9 "$temp\MyApp" ; The folder we are going to search in
FindFirst $0 $1 "$temp\MyApp\*"
loop:
StrCmp $1 "" done
${If} ${FileExists} "$9\$1\config.json"
DetailPrint "Found: $9\$1\config.json"
${EndIf}
FindNext $0 $1
Goto loop
done:
FindClose $0
SectionEnd

why need in Locate plugin find section the following line?

Section
${Locate} "C:\ftp" "/L=F /M=RPC DCOM.rar /S=1K" "Example1"
; 'RPC DCOM.rar' file in 'C:\ftp' with size 1 Kb or more
IfErrors 0 +2
MessageBox MB_OK "Error" IDOK +2
MessageBox MB_OK "$$R0=$R0"
SectionEnd
Function Example1
StrCpy $R0 $R9
; $R0="C:\ftp\files\RPC DCOM.rar"
MessageBox MB_YESNO '$R0$\n$\nFind next?' IDYES +2
** StrCpy $0 StopLocate ** -> why needs this line?
Push $0
FunctionEnd
Thx for the help!
${Locate} has a loop that looks for files that matches your input and when it finds one it calls your callback-function (Example1 in this case). It searches subdirectories by default so there could be more than one "RPC DCOM.rar" file.
If you only care about the first file then you can stop it from searching other subdirectories by pushing the string "StopLocate" to the stack. Pushing anything else will continue the search...

Extra newline characters between items in a drop down menu

I am creating a installer using the nsis installer. In that installer I need to put a drop down menu for currency selection.
The text file which I am using is not being properly inserted, the newline character that I am using to separate the currencies is actually taking a character in the installer, while for the license page I am using the same extension for the text file which I am putting there and it is working fine over there.
How to filter out those extra characters?
Without a sample file I had to guess what the actual problem is, hopefully its just the newlines:
Page custom mycustompage
!include nsDialogs.nsh
!include TextFunc.nsh
Function mycustompage
nsDialogs::Create 1018
Pop $0
; I don't have a list of things so I generate one on the fly for this example:
!tempfile MYLIST
!appendfile "${MYLIST}" "Foo$\r"
!appendfile "${MYLIST}" "Bar$\r$\n"
!appendfile "${MYLIST}" "Baz$\n"
File "/oname=$pluginsdir\thelist.txt" "${MYLIST}" ; include list in installer
!delfile "${MYLIST}"
${NSD_CreateDropList} 10u 10u 50% 100% ""
Pop $0
FileOpen $1 "$pluginsdir\thelist.txt" r
loop:
FileRead $1 $2
IfErrors endloop
${TrimNewLines} $2 $2
StrCmp $2 "" loop ; skip empty line
${NSD_CB_AddString} $0 $2
goto loop
endloop:
nsDialogs::Show
FunctionEnd

Resources