0

I'm running the script shown below and neither the directory nor the logical link are being created. I'm not sure what I'm doing wrong. Each call returns a code of 0 (zero). I'm using the information available here

Execute Command-Line Command from NSIS

https://nsis.sourceforge.io/Docs/Chapter4.html

RequestExecutionLevel admin

# includes
!include .\PrependEnv.nsh

# definitions
Outfile "007-exec-examples.exe"
InstallDir "C:\temp\nsis-examples\007-exe-examples"

Page Directory
Page InstFiles

# section to copy files and manipulate env variable
Section

    # copy files
    DetailPrint ""
    DetailPrint "Copying files to $InstDir..."
    DetailPrint ""
    SetOutPath "$InstDir\resources"
    File /a /r "resources\"

    # create a dir
    DetailPrint ""
    DetailPrint "Making dir $InstDir"
    Exec 'mkdir $InstDir\delete_me'
    Pop $0
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"

    # create a logical link
    DetailPrint ""
    DetailPrint "Creating logical link as C:\_YES to $InstDir\delete_me."
    Exec 'mklink /D $InstDir\_MY_LINK $InstDir\delete_me'
    Pop $0
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"

SectionEnd

--- EDIT ----------------------------

This also fails but does give a indication that an error occurred.

# ---
#
# This script tries to create a dir and a logical link.  
# 
# ---

RequestExecutionLevel admin

# includes
!include .\PrependEnv.nsh

# definitions
Outfile "008-exec-examples.exe"
InstallDir "C:\temp\nsis-examples\008-exe-examples"
ShowInstDetails show

Page Directory
Page InstFiles

# section to copy files and manipulate env variable
Section

    # copy files
    DetailPrint ""
    DetailPrint "Copying files to $InstDir..."
    DetailPrint ""
    SetOutPath "$InstDir\resources"
    File /a /r "resources\"

    # create a dir
    DetailPrint ""
    DetailPrint "Making dir $InstDir\delete_me"
    nsExec::ExecToStack 'mkdir $InstDir\delete_me'
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

    # create a logical link
    DetailPrint ""
    DetailPrint "Creating logical link as $InstDir\_MY_LINK to $InstDir\delete_me."
    nsExec::ExecToStack 'mklink /D $InstDir\_MY_LINK $InstDir\delete_me'
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

SectionEnd

enter image description here

--- EDIT --------------------------------

Here's a full working example based on the accepted answer. This example creates a directory, a shortcut, and a link.

# ---
#
# This script tries to create a dir and a logical link.  
# 
# ---

RequestExecutionLevel admin

# includes
!include .\PrependEnv.nsh

# definitions
Outfile "009-exec-examples.exe"
InstallDir "C:\temp\nsis-examples\009-exe-examples"
ShowInstDetails show

Page Directory
Page InstFiles

# section to copy files and manipulate env variable
Section

    # copy files
    DetailPrint ""
    DetailPrint "Copying files to $InstDir..."
    DetailPrint ""
    SetOutPath "$InstDir\resources"
    File /a /r "resources\"

    # create a dir
    DetailPrint ""
    DetailPrint "Making dir $InstDir\delete_me"
    CreateDirectory $InstDir\delete_me
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

    # create a short cut
    DetailPrint ""
    DetailPrint "Creating SHORTCUT as $InstDir\_MY_SHORTCUT to $InstDir\delete_me."
    CreateShortCut $InstDir\_MY_SHORTCUT.lnk $InstDir\delete_me
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

    # create a logical link
    DetailPrint ""
    DetailPrint "Creating LINK as $InstDir\_MY_SHORTCUT to $InstDir\delete_me."
    nsExec::ExecToStack 'cmd.exe /C mklink /D $InstDir\_MY_LINK $InstDir\delete_me'
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

SectionEnd
John
  • 3,458
  • 4
  • 33
  • 54

2 Answers2

1

For a directory, just use CreateDirectory. mklink is an internal command inside cmd.exe, you would have to execute cmd.exe /C mklink /D "c:\link" "c:\target".

Alternatively, call the API directly:

Section
!define /IfNDef SYMBOLIC_LINK_FLAG_DIRECTORY 1
System::Call 'KERNEL32::CreateSymbolicLink(t "c:\link", t "c:\target", i ${SYMBOLIC_LINK_FLAG_DIRECTORY})b.r0
DetailPrint Success=$0
SectionEnd
Anders
  • 97,548
  • 12
  • 110
  • 164
0

This worked to create a short cut (not a logical link):

# ---
#
# This script tries to create a dir and a logical link.  
# 
# ---

RequestExecutionLevel admin

# includes
!include .\PrependEnv.nsh

# definitions
Outfile "009-exec-examples.exe"
InstallDir "C:\temp\nsis-examples\009-exe-examples"
ShowInstDetails show

Page Directory
Page InstFiles

# section to copy files and manipulate env variable
Section

    # copy files
    DetailPrint ""
    DetailPrint "Copying files to $InstDir..."
    DetailPrint ""
    SetOutPath "$InstDir\resources"
    File /a /r "resources\"

    # create a dir
    DetailPrint ""
    DetailPrint "Making dir $InstDir\delete_me"
    CreateDirectory $InstDir\delete_me'
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

    # create a logical link
    DetailPrint ""
    DetailPrint "Creating logical link as $InstDir\_MY_LINK to $InstDir\delete_me."
    CreateShortCut $InstDir\_MY_LINK.lnk $InstDir\delete_me'
    Pop $0
    Pop $1
    DetailPrint "RegPrependString:Error=$0 (Should be 0)"
    DetailPrint "$1"

SectionEnd
John
  • 3,458
  • 4
  • 33
  • 54