Search for a file

From ISXKB

Revision as of 12:37, 29 October 2010 by Markus (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Contents

Introduction

This is an example script that shows how to search for a file with Inno Setup.

With the author's permission, it has been taken from Scan a disk path or an entire disk with Inno Setup on Dateiliste.

The script provides a progress page and a Cancel button for the user during the search. It is a good example for implementing similar functionality for other long-running tasks in a [Code] section.

Image:Scanprogress01.gif

Scanning a directory tree

Example script:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "My Program"
#define MyAppVerName "My Program 1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "MyProg.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{753F2C1F-EA02-488E-A204-A047F1EFA0BC}
AppName={#MyAppName}
AppVerName={#MyAppVerName}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Languages]
Name: english; MessagesFile: compiler:Default.isl

[CustomMessages]
english.ProgressTitle=Searching
english.ProgressCaption=Searching for files
english.ProgressText=Searching for files...

[Code]
var
	ProgressPage:	TOutputProgressWizardPage;
	ProgressValue:	Integer;
	ArrayLen:		LongInt;
	bExitSetup:		Boolean;

procedure ProcessDirectory (RootDir: String; Progress: Boolean);
var
	NewRoot:		String;
	FilePath:		String;
	FindRec:		TFindRec;
begin
	if bExitSetup then
		Exit;
	NewRoot := AddBackSlash (RootDir);
	if FindFirst (NewRoot + '*', FindRec) then
	begin
		try
			repeat
				if (FindRec.Name <> '.') AND (FindRec.Name <> '..') then
				begin
					FilePath := NewRoot + FindRec.Name;
					if FindRec.Attributes AND FILE_ATTRIBUTE_DIRECTORY > 0 then
						ProcessDirectory (FilePath, Progress)
					else
					begin
						// Start action -->
						// .
						// Add your custom code here.
						// FilePath contains the file name
						// including its full path name.
						// Try not to call a function for every file
						// as this could take a very long time.
						// .
						// <-- End action.
						ArrayLen := ArrayLen + 1;
						if (Progress) then
						begin
							if (ArrayLen mod 1000) = (ArrayLen / 1000) then
							begin
								ProgressValue := ProgressValue + 1;
								if ProgressValue = 100 then
									ProgressValue := 0;
								ProgressPage.SetProgress (ProgressValue, 100);
							end;
						end;
					end;
				end;
				if (bExitSetup) then
					Exit;
			until NOT FindNext (FindRec);
		finally
			FindClose(FindRec);
		end;
	end;
end;

function MessageBox (hWnd: Integer; lpText, lpCaption: String; uType: Cardinal): Integer;
 external 'MessageBoxA@user32.dll stdcall';

procedure CancelButtonClick (CurPageID: Integer; var Cancel, Confirm: Boolean);
begin
	Confirm := FALSE;
	if (MessageBox (0, SetupMessage (msgExitSetupMessage),
		SetupMessage (msgExitSetupTitle), 4 + 32) = 6) then
	begin
		bExitSetup := TRUE;
	end;
end;

procedure CurStepChanged (CurStep: TSetupStep);
var
	lI:			LongInt;
	Dir:		String;
begin
	if (CurStep = ssInstall) then
	begin
		// The folder to scan.
		Dir := 'C:\';
		// The progress page.
		ProgressPage := CreateOutputProgressPage (CustomMessage ('ProgressTitle'),
			CustomMessage ('ProgressCaption'));
		ProgressPage.SetText (CustomMessage ('ProgressText'), Dir);
		ProgressPage.SetProgress(0, 0);
		ProgressPage.Show;
		// Make the Cancel button visible during the operation.
		;WizardForm.CancelButton.Visible := TRUE;
		// Scan the folder.
		ProcessDirectory (Dir, TRUE);
		// Hide the progress page.
		try
		finally
			ProgressPage.Hide;
		end;
	end;
end;

See also

External links

Personal tools
Ads: