Jairo
Jairo

Reputation: 17

NSIS validate path

The following script shows how to check whether the path entered by the user is from a fixed drive:

!include nsdialogs.nsh
!define fixed 3
var path
var text
page custom page
function page
  nsdialogs::create 1018
    ${nsd_createtext} 0 0 100% 12u ""
    pop $text
  ${nsd_onchange} $text change
  nsdialogs::show
functionend
function change
  ${nsd_gettext} $text $path
  strcpy $0 $path 1
  system::call 'kernel32::GetDriveType(t"$0:\")i.r0'
  ${if} $0 <> ${fixed}
    messagebox mb_ok ""
  ${endif}
functionend
section
sectionend

How do I use PathFileExists in place of GetDriveType to check whether the path is valid as a folder? I need it for my custom page since I'm not using the built-in directory page which does the validation already. This is similar to iffileexists "$path\*.*" except the directory does not need to exist to be valid

Upvotes: 0

Views: 169

Answers (1)

Anders
Anders

Reputation: 101569

The NSIS directory page calls GetDiskFreeSpaceEx in a loop to search parent folders until it finds a valid existing path:

!include nsDialogs.nsh

Page custom MyPageCreate

Function MyPageCreate
    nsDialogs::Create 1018
    Pop $0
    ${IfThen} $0 == error ${|} Abort ${|}

    ${NSD_CreateLabel} 0 0u 100% 12u ""
    Pop $R9

    ${NSD_CreateText} 0 15u -6u 12u "$windir\foo\bar"
    Pop $1
    ${NSD_OnChange} $1 onDirChanged
    System::Call 'SHLWAPI::SHAutoComplete(p$1, 0)'
    Push $1
    Call onDirChanged ; Trigger update

    nsDialogs::Show
FunctionEnd

Function onDirChanged
Pop $0
${NSD_GetText} $0 $1
retry:
    StrLen $0 $1
    ${IfThen} $0 U< 2 ${|} Goto bad ${|} ; Don't allow "\" root path
    System::Call 'KERNEL32::GetDiskFreeSpaceEx(tr1,*l0r2,*l,*l)i.r0'
    ${If} $0 <> 0
        ${NSD_SetText} $R9 "Free space: $2 bytes @ $1"
    ${Else}
        StrLen $0 $1
        loop:
        IntOp $0 $0 - 1
        StrCpy $2 $1 1 $0
        ${If} $2 == '\'
        ${OrIf} $2 == '/'
            StrCpy $1 $1 $0
            Goto retry
        ${EndIf}
        StrCmp $2 "" 0 loop
bad:
        ${NSD_SetText} $R9 "Invalid path!"
    ${EndIf}
FunctionEnd

Section
SectionEnd

Upvotes: 0

Related Questions