Skip to content
⌘ NSIS Forum Archive

how to get system drive letter

15 posts

heinkoi459#

how to get system drive letter

Hi, how can I get the system drive letter to complete this InstallDir "_:\MyFolder\"

I know that 'ReadEnvStr $0 SYSTEMDRIVE' can do it but only in 'Section or Function'. So the 'InstallDir' is in declaration.

Thank you!

Guillaume
rsvargas#
you can put it on the .onInit Function.

I've created a page so the user can choose the drive in which he wants to install. like this (showing only relevant parts):

!include "FileFunc.nsh"
!insertmacro GetDrives
Var drives
Function .onInit
  StrCpy $drives ""
  ${GetDrives} "HDD" "GenerateDrivesString"
  File /oname=$PLUGINSDIR\\Drive.ini "Drive.ini"
  WriteINIStr "$PLUGINSDIR\\Drive.ini" "Field 2" "ListItems" "$drives"
FunctionEnd
Function GenerateDrivesString
  StrCmp $drives "" 0 +2
  StrCpy $drivesState "$9"
  StrCmp $drives "" +2
  StrCpy $drives "$drives|"
  StrCpy $drives "$drives$9"
  Push $0
FunctionEnd
Function ShowDrive
  WriteINIStr "$PLUGINSDIR\\Drive.ini" "Field 1" "Text" "Select drive"
  InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\\Drive.ini"
  Pop $0
  InstallOptions::show
  Pop $0
FunctionEnd
Function LeaveDrive
  ReadINIStr $0 "$PLUGINSDIR\\Drive.ini" "Field 2" "State"
  StrCpy $INSTDIR "$0\\MyDirectory"
FunctionEnd 
and, of course, you need the Drive.ini file

[Settings]
NumFields=2

[Field 1]
Type=Label
Text=Select which drive to install
Left=84
Right=256
Top=49
Bottom=57

[Field 2]
Type=DropList
Text=DropList
State=C:
ListItems=A:|B:|C:|D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:
Left=84
Right=212
Top=62
Bottom=76

heinkoi459#
Hi, when I compile there are an error on this line 'File /oname=$PLUGINSDIR\Drive.ini Drive.ini' (File: "Drive.ini" -> no files found.) So I search on Web but there are nothing interesting to solve this problem.

Thank you

Guillaume
rsvargas#
You need to create a file called Drive.ini with the following contents and put it in the same folder of your .nsi script.

Originally posted by rsvargas


[Settings]
NumFields=2

[Field 1]
Type=Label
Text=Select which drive to install
Left=84
Right=256
Top=49
Bottom=57

[Field 2]
Type=DropList
Text=DropList
State=C:
ListItems=A:|B:|C:|D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:
Left=84
Right=212
Top=62
Bottom=76

heinkoi459#
Greet but I have another stupid error in this line 'StrCpy $drivesState "$9" ' (Usage: StrCpy $(user_var: output) str [maxlen] [startoffset]). So what I have to do to solve this?!

Thank you again

Guillaume
rsvargas#
You need to declare the variable, you can add
Var drivesState 
just below the line with
Var drives 
Or you can just delete the line which refers to this variable...
heinkoi459#
Okay, but in 'InstallDir "_\imagem\PSadmin\"' (at declaration what I have to put in '_' to have the right drive letter?)
Afrow UK#
Just use a variable in InstallDir.

Var SystemDrive
InstallDir `$SystemDrive\some\dir`

You just have to use ReadEnvStr $SystemDrive SYSTEMDRIVE in .onInit.

Stu
heinkoi459#
!define APPNAME "NAME"
!define APPNAMEANDVERSION "VERSION"
Var SystemDrive
; Main Install settings
Name "${APPNAMEANDVERSION}"
InstallDir "$SystemDrive\\MY\\PATH\"
InstallDirRegKey HKLM "Software\${APPNAME}" ""
OutFile "C:\\Install.exe"
!define PROGPATH "$SystemDrive\\\MY\"
RequestExecutionLevel admin
; Modern interface settings
!include "MUI.nsh"
!include "WinMessages.nsh"
!include "FileFunc.nsh" 
; MUI Welcome page on 3 lines
!define MUI_WELCOMEPAGE_TITLE_3LINES
; MUI Settings / Icons
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Ent\Icons\Ent.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Ent\Icons\Ent.ico"
 
; MUI Settings / Header
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_RIGHT
!define MUI_HEADERIMAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Ent\Header\Header.bmp"
!define MUI_HEADERIMAGE_UNBITMAP "${NSISDIR}\Contrib\Graphics\Ent\Header\Header.bmp"
 
; MUI Settings / Wizard
!define MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Ent\Wizard\Wizard.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Ent\Wizard\Wizard.bmp"
;!define MUI_WELCOMEPAGE_TITLE '${WELCOME_TITLE}'
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
; Set languages (first is default language)
;!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "French"
!insertmacro MUI_RESERVEFILE_LANGDLL
!insertmacro GetDrives 
; ******************************************************************************************************
; * INSTALL
; ******************************************************************************************************
Section "NAME" Section1
    ; Set Section properties
    SetOverwrite on
    ; Set Section Files and Shortcuts
    SetOutPath "$INSTDIR\"
    File "..\Name.exe"
    File "..\LIB.jar"
    
    SetOutPath "$INSTDIR\images\"
    File "..\images\*.*";
    
    SetOutPath "$INSTDIR\images\_icons\"
    File "..\images\icons\*.*";
    
    SetOutPath "$INSTDIR\images\right\"
    File "..\images\right\*.*";
    
    SetOutPath "$INSTDIR\lang\"
    File "..\lang\fr.xml"
    
    SetOutPath "$INSTDIR\reports\"
    File "..\reports\*.rpt"
    
    SetOutPath "$INSTDIR\tmp\"
    
    ; menu démarrer
    CreateDirectory "$SMPROGRAMS\MY\PATH"
    CreateShortCut "$SMPROGRAMS\MY\PATH\NAME.lnk" "$INSTDIR\NAME.exe"
    CreateShortCut "$SMPROGRAMS\MY\PATH\Uninstall.lnk" "$INSTDIR\uninstall.exe"
SectionEnd
; ******************************************************************************************************
; * FINISH SECTION
; ******************************************************************************************************
Section -FinishSection
    WriteRegStr HKLM "Software\${APPNAME}" "" "$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${APPNAME}"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$INSTDIR\uninstall.exe"
    
    WriteUninstaller "$INSTDIR\uninstall.exe"
    
SectionEnd
; Modern install component descriptions
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${Section1} "Jesus love bacon"
!insertmacro MUI_FUNCTION_DESCRIPTION_END
; ******************************************************************************************************
; * UNINSTALL
; ******************************************************************************************************
Section Uninstall
    
    ;Remove from registry...
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
    DeleteRegKey HKLM "SOFTWARE\${APPNAME}"
    
    ;Remove the Main Folder
    RMDir /r "$INSTDIR\"
    RMDir "${IMAGEMPATH}"
    
    ; Delete Shortcuts
    Delete "$SMPROGRAMS\MY\PATH\NAME.lnk"
    Delete "$SMPROGRAMS\MY\PATH\Uninstall.lnk"
    RMDir "$SMPROGRAMS\MY\PATH"
    RMDir "$SMPROGRAMS\MY"
SectionEnd
Function .onInit 
    ReadEnvStr $SystemDrive SYSTEMDRIVE
FunctionEnd 
When I Install the program, in Installation folde it write: \MY\PATH, so I when the lettre _:\MY\PATH

Thank yout guys

PS: there are missing somes "\" just don't take care about it

Guillaume
rsvargas#
Try moving the

InstallDir "$SystemDrive\MY\PATH\" 
statement to the .onInit function. The .onInit function should be like this:

Function .onInit 
    ReadEnvStr $SystemDrive SYSTEMDRIVE 
    InstallDir "$SystemDrive\\MY\\PATH\\" 
FunctionEnd 
Afrow UK#
You're right. InstallDir is being evaluated while $SystemDrive is still empty.

Instead just use this in .onInit:
Function .onInit
ReadEnvStr $R0 SYSTEMDRIVE
StrCpy $INSTDIR `$R0\MY\PATH\`
FunctionEnd
@ rsvargas:
InstallDir is a compile time instruction. It is not valid within a Function or Section.

Stu
rsvargas#
@Afrow UK
My bad, I use the StrCpy $INSTDIR way to set the directory in my installers, and didn't remember that the InstallDir directive would not work in a function, thanks!