Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an empty directory (NSIS)

How can I do it by NSIS 2.46?

I know this is probably a dumb question, but how the heck do I create an empty directory and check for errors?

I do so:

ClearErrors
CreateDirectory $R1
${If} ${Errors} 
  DetailPrint "failed"
  MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Can not create folder $R1"
  Abort
${Else}
  DetailPrint "done"
${EndIf}

if the variable $R1 is

  • "c:\test" - done
  • "c:\con" - failed ("con" - reserved on Windows)
  • "qwer:\test2" - done
  • "qwer\test3" - done (without ":")

why "test2" and "test3" without errors?

UPDATE: How to make that it was an error?

like image 270
Andrei Krasutski Avatar asked Jan 27 '15 11:01

Andrei Krasutski


3 Answers

I decided:

ClearErrors
CreateDirectory $R1
${If} ${Errors} 
${OrIfNot} ${FileExists} "$R1\*.*"
  DetailPrint "failed"
  MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Can not create folder $R1"
  Abort
${Else}
  DetailPrint "done"
${EndIf}

have more ideas?

like image 50
Andrei Krasutski Avatar answered Sep 21 '22 03:09

Andrei Krasutski


In order to check for errors you should check the error flag, which you're doing right. I think the problem is you should use quotes:

CreateDirectory "$R1"
like image 31
Francisco R Avatar answered Sep 22 '22 03:09

Francisco R


NSIS does some unfortunate (IMHO) path mangling and only accepts : as the 2nd character, otherwise it is silently stripped. This means that "qwer\test2" and "qwer:\test2" are treated the same at run-time. You can verify this by copying it to one of the special path registers:

!macro TestNsisPathMangling p
Push $InstDir
StrCpy $InstDir "${p}"
DetailPrint "Old=${p}"
DetailPrint "New=$InstDir"
Pop $InstDir
!macroend

!insertmacro TestNsisPathMangling "qwer\test2"
!insertmacro TestNsisPathMangling "qwer:\test2"

This leaves us with "qwer\test2" and this is a relative path and those are officially not supported when calling CreateDirectory. If you try this in NSIS 3 you should get:

CreateDirectory: Relative paths not supported

Edit:

If the path is entered by the user you can call the Windows function GetFullPathName to get a absolute/full path:

Push "qwer\test3"
System::Call 'KERNEL32::GetFullPathName(ts,i${NSIS_MAX_STRLEN},t.r1,t)i.r0'
${If} $0 <> 0
    DetailPrint "GetFullPathName=$1"
${Else}
    DetailPrint "GetFullPathName failed"
${EndIf}
like image 32
Anders Avatar answered Sep 20 '22 03:09

Anders