Skip to content
⌘ NSIS Forum Archive

Cancel during installation - File /r loop?

6 posts

MrBighead#

Cancel during installation - File /r loop?

Hello everyone. I've been creating a personal installer using NSIS, and I can generally get a good idea of how to do things by reading the various tutorials and forums around the web. I would like to cancel an installation when the installer is in the middle of installing files. I soon realized NSIS is not really equipped to do this, but it is possible via the method found in this tutorial:
https://nsis.sourceforge.io/InstFile...ring_InstFiles

I managed to get everything implemented, but there is one problem with the approach above. It does not account for when installing files with a "File /r" loop, so it doesn't seem possible to actually pause the installer temporarily while the dialog is showing because there is no way to interrupt the loop. Example snippet below.

Section "MainSection" MAINSEC
SetOutPath "$INSTDIR"
File /r "${INPUTPATH}\*"
${PauseIfUserIsMakingAbortDecision}
WriteUninstaller "$INSTDIR\uninstall.exe"
SectionEnd​
Is there some other approach I can take here? Is there another way to grab all files from a directory then loop through its contents with some other kind of loop where instructions can be defined during each iteration? Any help would be greatly appreciated.

Off topic: If this subject is inappropriate for this forum, I can edit the post and remove it. I would much rather have my question above answered, but also would like to know where else this question can be posted.

So I have another account on these forums that I can log into which uses my primary email address, but I can't post or do anything with it. I've contacted the site using "CONTACT US" about this issue but I've never received a reply and the issue remains. Is there a reason my account is stuck in some kind of stasis? It is only about two years old, and I don't recall even make a post on it so I don't think it was banned or anything. It's not unusual for me to make an account on forums and lurk around to browse for solutions to potential issues. Any ideas on why I can log into the site with it but can't actually do anything?
Anders#
NSIS was never designed to abort mid-install and everything else is a hack.

File /r is not going to work, you would have to use !system to generate a file list with a batch file and then use !include and normal File commands...
MrBighead#
Thanks for the tips. I've never created anything other than simple installers, so I've never used !system, !include, or anything other than File /r so I'll see what I can do.
Anders#
Originally Posted by MrBighead View Post
So I have another account on these forums that I can log into which uses my primary email address, but I can't post or do anything with it.
Post or PM me the username. The forum was recently upgraded, maybe that broke it.

MrBighead#
So I managed to figure out something that works. I created an external powershell script to create a ".nsh" file that sets the appropriate paths, includes each file, and includes the pause function after each line. It took me a little while to understand what to do and to get it working, but it will pause immediately after the current file when the user hits Cancel. Thank you for the suggestion! To anyone else who happens upon this post, and wants to be able to cancel and pause the installation while doing it, here is the steps...

- Follow the tutorial from the first post. The powershell script relies on "PauseIfUserIsMakingAbortDecision" function.
- Implement as much as you need, at least up until the point to pause when cancel is pressed.
- Download my powershell script here:
MediaFire is a simple to use free service that lets you put all your photos, documents, music, and video in a single place so you can access them anywhere and share them everywhere.

- Place the above script in the directory you wish to create an installer for and run it (right click, run with powershell).
- This will create "FileList.nsh" in the current directory.
- Delete or move the script from the current directory (you won't want to include it in your installer).
- Move the "FileList.nsh" alongside your main NSIS ".nsi" script (you won't want to include this either).
- In your main installer section, use "!include FileList.nsh"

And that's it, it will just work if you set everything up correctly from the tutorial. The generated file includes all directories and files, keeps the hierarchy, and places the PauseIfUserIsMakingAbortDecision after every line so it pauses as soon as possible. Thanks Anders for the solution.
MrBighead#
Since this is the only place I posted this script, and it has almost 200 downloads, I figured it was worth posting an update.
MediaFire is a simple to use free service that lets you put all your photos, documents, music, and video in a single place so you can access them anywhere and share them everywhere.

Over the past few months, I have been stealthily updating the script above without changing the Mediafire link by replacing it directly. The script is now up to v1.2.0 and has had quite a bit of work put into it. I implemented a GUI, and it can now serve as a general purpose file list generator for NSIS as well as generating an uninstall list. This is a good alternative to a (file /r ) loop as it allows greater control over what happens between adding/removing each file. The latest update improved the speed significantly, as instead of writing all files to the NSH file one by one, it will now write them all once at the end.

The main point of this script is still the ability to add the call to the function ( ${PauseIfUserIsMakingAbortDecision} ) after each line, but it's possible to change the inserted command by editing the textbox. This will only make sense if following the tutorial mentioned in the first post, which is a really helpful guide on how to generate an installer that pauses after a user presses the "Cancel" button and waits for input. This script basically relies on the information in that tutorial, unless you are just using it to generate file lists.

Click image for larger version

Name:	Untitled.png
Views:	233
Size:	21.0 KB
ID:	4625006

Here is a description of each option:

Program Install Path: This is the root path of the files you wish to include in your installer.
Installer List Path: This is the path to output FileList.nsh which is the generated file list with the pause command after every line.
Insert custom command: The custom command added after every file in the list, which defaults to the function name from the tutorial.
Generate uninstaller: Also generates an UninList.nsh which is a file list to be used in an uninstaller. The textbox below is the intended uninstaller name ( $UninFileName ).
Include commands: This adds a command to the installer list to create the uninstaller ( WriteUninstaller "$INSTDIR/$UninFileName" ) and adds a command to the uninstaller list to delete the uninstaller after uninstalling (Delete "$INSTDIR\$UninFileName" ). Uncheck this if you wish to handle these commands in the host script (.nsi) rather than these (.nsh) scripts.
Do not display message box: This hides the GUI message box that displays progress and prevents text being written to it. This should be checked when dealing with very large installers, as printing text to the GUI is slow and will overall increase the amount of time to generate the lists. For hundreds of files, it's not really needed. For thousands+, I suggest checking it.

Here is some instructions on how to use this:

- If you wish to pause the installer when cancelling, follow the tutorial here:
https://nsis.sourceforge.io/InstFile...ring_InstFiles
- Right click the script and click Run with PowerShell
- Set the Program Install Path to the root location of the files you wish to install
- Set any options you may want to modify
- Press Start and it will generate the lists of files with the optionally included command
- Include the NSH files in your script. In the installer section, use ( !include FileList.nsh ) and in the uninstaller section, use ( !include UninList.nsh ). They should be in the same path as your NSI script. If not, the path will need to be added to the include command.

I think that is everything I can think of. If I think of anything new to add to it, find any bugs, or think of ways to optimize it further, I will continue to just update the link so it never changes. This forum does not allow editing posts, so I'll try to keep the link static. If for some reason it ends up changing, I'll post a followup with a new link. And if you read this far, I hope it's been useful for you!