Search code examples
batch-filensis

Get response after deleting folder by batch using NSIS


I am executing batch file by using NSIS script.

which deletes some folders from given folder location.

after execution of batch user will get Message Box.

Problem :

batch file get started but before completion of it user is getting Message Box. Which impacts further usecases.

How ca we hold the process for getting message box or how we can get responce from batch file that excecution is completed and then show message box.

Batch File :

@echo off

if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
rd "FolderLocation1" /s /q

rd "FolderLocation2" /s /q

NSIS Script :

 ExecWait '$INSTDIR\BATCHFILE.bat'
  MessageBox MB_OK $(Message For Message Box)
  Quit

Solution

  • NSIS waits for the batch file, the problem is your call to Powershell with a verb which does not wait. Adding -wait seems to fail when combined with the "RunAs" verb.

    Instead of elevating for this single command you should elevate your installer instead.

    RequestExecutionLevel admin ; Require admin rights on Vista+ (when UAC is turned on)
    
    !include LogicLib.nsh
    
    Function .onInit
    UserInfo::GetAccountType
    Pop $0
    ${If} $0 != "admin" ; Require admin rights on NT4+
        MessageBox mb_iconstop "Administrator rights required!"
        SetErrorLevel 740 ; ERROR_ELEVATION_REQUIRED
        Quit
    ${EndIf}
    FunctionEnd
    
    Page InstFiles
    
    Section
    RMDir /R "FolderLocation1"
    RMDir /R "FolderLocation2"
    SectionEnd