nsis read directory from txt files? - nsis

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

Related

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...

How to include one jre based on os in nsis

I have two jre file in my source code.I have created two exe files based on user inputs and changed the outfile named.
Now based on outfile I want to include jre file.if outfile contains 64 then include 64 bit jre else 32 bit jre.
I have tried below code.but its not working?
!ifdef PLATFORM64
OutFile setup64.exe
!else
OutFile setup32.exe
!endif
Function .Oninit
${StrContains} $0 "64" OutFile
File /oname=$JRE_INSTALLER_FILE_NAME "$TEMP\jre-6u29-windows-x64.exe"
FunctionEnd
Questions:
1.How to include any one jre file based on outfile named?
Probably something like this:
!ifdef PLATFORM64
OutFile setup64.exe
!else
OutFile setup32.exe
!endif
Function .Oninit
var JRE_PLATFORM_FILE
${StrContains} $0 "64" $OutFile
StrCmp $0 "" notfound
StrCpy $JRE_PLATFORM_FILE "file-name-for-64-bit-JRE"
Goto done
notfound:
StrCpy $JRE_PLATFORM_FILE "file-name-for-32-bit-JRE"
done:
File /oname=$JRE_INSTALLER_FILE_NAME $JRE_PLATFORM_FILE
FunctionEnd

Find a string pattern in a file from an NSIS script

In an NSIS installer script, I'm trying to check if a given httpd.conf file contains the following line :
Include "c:\xxx\yyy.conf"
If so, then my installer script would not append it to the file, otherwise, it would append it.
I've come through {LineFind} but not sure this really makes what i'm trying to achieve.
What could be the simplest way to do a kind of "grep" on a text file from an NSIS script ?
Thank you !
Here is a sample for searching for a given line into a file, using the LogicLib for ease of syntax. The search is stopped as soon as the line is found. This sample works on the sample script itself:
# find.nsi : sample for LineFind from TextFunc.nsh
!include "textfunc.nsh"
!include "logiclib.nsh"
OutFile "find.exe"
!define lookfor `Section` ;will find
;!define lookfor `Sectionn` ;will not find
Var found
Section
StrCpy $found 0
${LineFind} "find.nsi" "/NUL" "1:-1" "GrepFunc"
${if} $found = 1
MessageBox MB_OK "string found"
${else}
MessageBox MB_OK "string NOT found"
${endIf}
SectionEnd
Function GrepFunc
${TrimNewLines} '$R9' $R9
DetailPrint "test for line $R8 `$R9`"
${if} $R9 == "${lookfor}"
StrCpy $found 1 ;set flag
Push "StopLineFind" ;stop find
${else}
Push 0 ;ignore -> continue
${endIf}
FunctionEnd

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