NSIS - Install a file to a pre-existing, uncertain subfolder? - installation

I'm trying to install a file to a pre-existing folder structured like this:
$APPDATA/somefolder/(uncertainFolder)
The "uncertainFolder" would be either "1.0" or "2.0".
The same file will be installed into the "uncertainFolder" despite the folder name difference. How can I achieve this?
Thank you in advance.

Files installed with the File instruction are extracted into the directory set by SetOutPath. Changing this path at run-time is not a problem once you know which folder you want.
If the possible folder names are known at compile-time you can use if/or if/else:
!include LogicLib.nsh
${If} ${FileExists} "$InstDir\SomeFolder\2.0\*.*"
SetOutPath "$InstDir\SomeFolder\2.0"
${Else}
SetOutPath "$InstDir\SomeFolder\1.0"
${EndIf}
You can also enumerate files and folders at run-time:
FindFirst $0 $1 "$InstDir\SomeFolder\*.*"
loop:
StrCmp $1 "" end ; No more files?
StrCmp $1 "." next ; DOS special name
StrCmp $1 ".." next ; DOS special name
IfFileExists "$InstDir\SomeFolder\$1\*.*" 0 next ; Skip files
DetailPrint "$1 is a folder in $InstDir\SomeFolder"
SetOutPath "$InstDir\SomeFolder\$1"
Goto end ; This stops the search at the first folder it finds
next:
FindNext $0 $1
goto loop
end:
FindClose $0
The Locate macro in FileFunc.nsh is built on top of FindFirst/FindNext and can also be used if you prefer its syntax...

Related

How to get network path of a directory using command line

Say I have a directory called Temp in windows.
When I right click on it and open properties, and go to Sharing tab, I see this:
The field Network Path is Not Shared.
Now I click Share and select to share with and click ok.
The field Network Path now has value:
Is there a way to get the value of Network Path from command line.
Ideally I am looking for a command which takes directory path as input e.g. C:\Temp and output \\S5XXXXXXN\Temp if the directory is shared or Not Shared if the directory is not shared.
Background: I have a NSIS script to create an installer. The end of installation process is supposed to spit out a list of properties. One of the properties required for output is the Network Path of a directory. Inside NSIS what I have available is C:\Temp which I want to convert to \\S5XXXXXXN\Temp (if available) before spitting it out.
I tried to use this https://nsis.sourceforge.io/Get_Universal_Name but its not working.
DetailPrint "My directory is:"
# value of MYDIR is C:\Temp
DetailPrint $MYDIR
Push $MYDIR
Call get_universal_name
Pop $MYDIRNET
DetailPrint "My dir after call is: "
DetailPrint $MYDIRNET
# above also prints C:\Temp even though its shared and has value \\S5XXXXXXN\Temp
Update:
The answer below by #Anders works like a charm.
The list returned has an entry with blank $3 though. Just to be aware of and to add a necessary check if required.
get_universal_name calls WNetGetUniversalName and this function only supports mapped drive letters (net.exe use style). WNetGetConnection seems to have the same limitation. Even if this limitation did not exist it would still not work because a process elevated with UAC does not share the net connections with non-elevated processes.
Going the other way does seem to work, getting a list of shares and their local paths:
!include LogicLib.nsh
Section "Check shares"
System::Call 'NETAPI32::NetShareEnum(p0,i2,*p.r1,i-1,*i0r5,*i0,p0)i.r0'
${If} $0 = 0
Push $1
${DoWhile} $5 <> 0
System::Call '*$1(p.r2,i,p,i,i,i,p.r3,p,&l.r4)'
IntPtrOp $1 $1 + $4
System::Call '*$2(&w${NSIS_MAX_STRLEN}.r2)'
System::Call '*$3(&w${NSIS_MAX_STRLEN}.r3)'
System::Call 'NETAPI32::NetServerGetInfo(p0,i100,*p.r4)' ; Get computer name
System::Call '*$4(i,w.s)'
System::Call 'NETAPI32::NetApiBufferFree(p$4)'
Pop $4
DetailPrint "$3=\\$4\$2"
; ${If} $3 == "$MYDIR" ...TODO... ${EndIf}
IntOp $5 $5 - 1
${Loop}
System::Call 'NETAPI32::NetApiBufferFree(ps)'
${EndIf}
SectionEnd

How to uninstall VSIX extension using NSIS?

Actually, as is noted here How to uninstall .vsix Visual Studio Extensions?, I have to call something like this:
vsixinstaller /u:12345678-1234-5678-1234-123456780000
However, I could not figure out how to get the path to VSIXInstaller inside of NSIS script.
According to this blog you can find the path in the registry or by using the %vs###comntools% environment variable.
Function GetVSIDEPath
Exch $0
Push $1
Push $2
ExpandEnvStrings $1 "%VS$0COMNTOOLS%"
IfFileExists "$1\?" "" tryReg
GetFullPathName $1 "$1\..\..\IDE\"
IfFileExists "$1\?" done
tryReg:
IntOp $0 $0 / 10
ReadRegStr $1 HKLM "SOFTWARE\Microsoft\VisualStudio\$0.0" "InstallDir"
done:
StrCpy $0 $1
Pop $2
Pop $1
Exch $0
FunctionEnd
!include LogicLib.nsh
Section
Push 100 ; Visual Studio 2010
Call GetVSIDEPath
Pop $0
${If} $0 != ""
ExecWait '"$0\vsixInstaller.exe" /u:12345678-1234-5678-1234-123456780000'
${EndIf}
SectionEnd
If you support more than one version of Visual Studio you have to call GetVSIDEPath+ExecWait multiple times...

NSIS, detect if directory exists during installation

I have installer, which supports choosing installation directory. And I want to detect if given folder exists and if it is empty. If it is NOT empty, show warning message box, then remove all its contents and install program into that folder. Only problem is to get into the right code section, where I can get installation folder given by user during installation, I can handle the rest.
Thank you for any advices.
Normally you would just check if the directory exists:
Outfile "$%Temp%\Test.exe"
RequestExecutionLevel user
InstallDir "$Documents\Test"
!include LogicLib.nsh
Page Directory "" "" DirLeave
Page InstFiles
Function DirLeave
${If} ${FileExists} "$InstDir\*"
MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
Abort
yep:
RMDir /r "$InstDir"
${EndIf}
FunctionEnd
Section
SetOutPath $InstDir
File myfile.ext
SectionEnd
This will also display the message if the directory exists but is empty. To work around that you would need some custom detection:
!macro _IsNonEmptyDirectory _a _b _t _f
!insertmacro _LOGICLIB_TEMP
!insertmacro _IncreaseCounter
Push $0
FindFirst $0 $_LOGICLIB_TEMP "${_b}\*"
_IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}:
StrCmp "" $_LOGICLIB_TEMP _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
StrCmp "." $_LOGICLIB_TEMP +2
StrCmp ".." $_LOGICLIB_TEMP 0 _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
FindNext $0 $_LOGICLIB_TEMP
Goto _IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}
_IsNonEmptyDirectory_done${LOGICLIB_COUNTER}:
FindClose $0
Pop $0
!insertmacro _!= "" $_LOGICLIB_TEMP `${_t}` `${_f}`
!macroend
!define IsNonEmptyDirectory `"" IsNonEmptyDirectory`
Function DirLeave
${If} ${IsNonEmptyDirectory} "$InstDir"
MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
Abort
yep:
RMDir /r "$InstDir"
${EndIf}
FunctionEnd

Writting Folder causes error code 80

My NSIS installer is giving a error code of 80 when I attempt to copy/overwrite a folder. I think it may have to do with the fact that the folder I am attempting to copy to the users HD already exists. But in my case I will always want to overwrite it.
What does the error code 80 mean?
Heres my code:
# Write plugins to EXDS_Customisation\EXDS_USER\
ClearErrors
SetOverwrite try
SetOutPath "$INSTDIR\EXDS_User\"
FILE /r "${localInstallDir}\EXDS_Customisation\EXDS_User\${MAINPLUGINSDIR}"
${If} ${Errors}
System::Call "Kernel32::GetLastError() i() .r1"
# Prints: "Error code: 80"
MessageBox MB_ICONINFORMATION|MB_OK "Error code: $1 "
Quit
${EndIf}
If you always want to overwrite, why are you using Try and not SetOverwrite On?
Using System::Call "Kernel32::GetLastError()... is never valid. System::Call has a special ?e option but it is not useful in your case. You cannot get specific error information from NSIS, all you have is just the error flag...

How do I create a shortcut (.lnk) with a relative target?

I have an executable on my disk-on-key in dir\program\prog.exe
I'd like to have a shortcut to the executable on the DoK's root directory, that is, prog.lnk would refer to dir\program\prog.exe.
However, it seems that prog.lnk can't have a relative target. This is a problem when the DoK will have different drive letters assigned to it, depending on which PC it's connected to.
Any suggestions, aside from the obvious one of putting prog.exe in the root dir?
(ultimately, I'd like to do this at install time using nsis)
Thanks,
Rony
If we assume cmd.exe would be on the same absolute path for all windows installations (probable, but not fool-proof) you can make out the .lnk file to start cmd like this
cmd.exe /c start /d. your command here
/d sets the directory to the directory of the .lnk file
There might be other useful options for the start command (e.g. /b)
While it is possible for shortcuts to contain a relative path to the target (.lnk files have a flag called SLDF_HAS_RELPATH) NSIS does not support creating anything other than "normal" shortcuts so you need to write the binary data directly (The .lnk format is pretty stable and has been documented by MS)
!macro FileWriteHexBytes h b
push ${h}
push ${b}
call FileWriteHexBytes
!macroend
Function FileWriteHexBytes
exch $9
exch
exch $0
push $1
push $2
loop:
StrCpy $2 $9 2
StrLen $1 $2
IntCmp $1 2 0 end
FileWriteByte $0 "0x$2"
StrCpy $9 $9 "" 2
goto loop
end:
pop $2
pop $1
pop $0
pop $9
FunctionEnd
Function CreateRelativeLnk
exch $9
exch
exch $0
push $1
FileOpen $0 "$0" w
StrCmp $0 "" clean
!insertmacro FileWriteHexBytes $0 "4C0000000114020000000000C000000000000046"
!insertmacro FileWriteHexBytes $0 48010400 ;flags
!insertmacro FileWriteHexBytes $0 00000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000
StrLen $1 $9 ;must be < 255!
FileWriteByte $0 $1
FileWriteByte $0 0
FileWrite $0 "$9" ;relative target path
!if 0
;The icon is problematic, does not seem like it works with relative paths (but you can use system icons...)
StrCpy $9 "explorer.exe"
StrLen $1 $9
FileWriteByte $0 $1
FileWriteByte $0 0
FileWrite $0 "$9"
!else
!insertmacro FileWriteHexBytes $0 05003e2e657865 ;fake default .exe icon
!endif
clean:
FileClose $0
pop $1
pop $0
pop $9
FunctionEnd
Call it like this:
push "$temp\testlink.lnk"
push "testdir\testapp.exe" ;full path to this is $temp\testdir\testapp.exe
call CreateRelativeLnk
While the generated .lnk seems to work, I'm not sure if I would use this in production code
A much better solution is to create a little NSIS app like Oleg suggests (NSIS applications can contain embedded data at the end of the .exe that it can read from itself at runtime etc..)
Install "Relative"
I'm using a bit of a hack. The approach is shown in this screenshot:
It starts explorer.exe and then passes a relative path like so:
%windir%\explorer.exe path\to\your\files\youFileName.example
I'm using a small tool called "Relative" for this. After you install it, you can right-click a file and then select Create relative shortcut.... It will then pop up a Save as... box. This is not quite as comfortable as simply dragging and dropping but it helps. (It also uses its own special icon for the links it creates. So you no longer have the original icon in the link. See above. You may or may not like this.)
You are right. You can not use shortcuts (.lnk) on a removable media, because like you wrote yourself the removable media can have different drive letters in different situations.
If you need to have something in a root directory of the drive one use a CMD, VBS or JS instead of a shortcut. In some situation one use also a standard EXE which use a config file like INI file to start another program from the other subdirectory. It is a typical practice for CD/DVD to implement starting of a setup program after inserting of the disk. Probably this way will be OK in for your requirements also?
I believe that the NT 4 Resource Kit command SHORTCUT.exe would create relative linked shortcuts. I wish that Microsoft would create a new supported utility or Powershell Cmdlet to facilitate the creation of relative .lnk files or make NTFS links work more like Linux symbolic links. Until then, I use .cmd/.bat and .ps1 files for this purpose.
Program.cmd
#"%~dp0Relative\Path\Program.exe" %*
# = Suppresses the command echo.
%~dp0 = expands to the script's directory.
Relative\Path = could include .. to backup a directory.
%* = passes any parameters received by the script on to Program.exe.
Program.ps1
Unfortunately, though .cmd/.bat files will run from any context (the run dialog, a CMD prompt, in Powershell, double clicking in File Explorer, etc), they cannot call things stored on a UNC path. For exposing things in a UNC path, in Powershell (I do this for tools like Git and Mercurial), I will create a Powershell version of the above script.
&"$PSScriptRoot\Relative\Path\Program.exe" #args
& = puts Powershell into command mode, so that the string in quotes gets ran.
"" = contains a string, expanding any variables.
$PSScriptRoot = expands to the script's directory.
Relative\Path = could include .. to backup a directory.
#args = passes any parameters received by the script on to Program.exe.

Resources