FileDeleteLine Example

InstallShield 2016 » InstallScript Language Reference

Note • To call this function in a Basic MSI setup, you must first create a custom action for the entry-point function, execute the custom action in a sequence or as the result of a dialog's control event, and then build the release.

/*--------------------------------------------------------------*\

*

* InstallShield Example Script

*

* Demonstrates the FileDeleteLine function.

*

* This script searches a file for the first line that includes

* the string "PATH".  If a line with that string is found, it

* is deleted.  Finally, a new line is added to the file at the

* position of the deleted line.  If a line with the word

* "PATH" was not found, the new line is inserted at the top of

* the file.

*

* Note: Before running this script, create a batch file

*       named ISExampl.bat in the root of drive C.  For

*       best effect, that file should include PATH command.

*

\*--------------------------------------------------------------*/

 

#define SDIR         "C:\\"

#define EXAMPLE_BAT  "ISExampl.bat"

#define TITLE        "FileDeleteLine Example"

 

// Include Ifx.h for built-in InstallScript function prototypes.

#include "Ifx.h"

 

export prototype ExFn_FileDeleteLine(HWND);

 

function ExFn_FileDeleteLine(hMSI)

    STRING szSearchStr, svReturnLine, szNewString, szMsg;

    NUMBER nvResult, nvLineNum;

begin

 

    // Set up the search string parameter for FileGrep.

    szSearchStr = "PATH";

 

    // Find the search string in the specified file.

    nvResult = FileGrep (SDIR ^ EXAMPLE_BAT, szSearchStr,

                        svReturnLine, nvLineNum, RESTART);

 

    switch(nvResult)

        case FILE_NOT_FOUND:

            // Report error; then terminate.

            MessageBox (EXAMPLE_BAT + " not found.", WARNING);

            abort;

        case FILE_LINE_LENGTH:

            // Report error; then terminate.

            MessageBox (EXAMPLE_BAT + "lines too long.", WARNING);

            abort;

        case OTHER_FAILURE:

            // Report error; then terminate.

            MessageBox (EXAMPLE_BAT + "Unknown failure on call to FileGrep.",

                       WARNING);

            abort;

        case END_OF_FILE:

            // Report that the search string was not found.

            szMsg = "\"%s\" not found in %s.";

            SprintfBox (INFORMATION, TITLE, szMsg, szSearchStr, EXAMPLE_BAT);

 

            // Set the line number parameter for FileInsertLine.

            nvLineNum = 0;

        case 0:

            // Delete the line with the search string.

            if (FileDeleteLine (EXAMPLE_BAT, nvLineNum, nvLineNum) < 0) then

                MessageBox ("Failed on call to FileDeleteLine.", SEVERE);

                abort;

            else

                // Report the deletion.

                szMsg = "\"%s\" found in line %d of %s:\n\n%s\n\n\Line deleted. ";

                SprintfBox (INFORMATION, TITLE, szMsg, szSearchStr, nvLineNum,

                           EXAMPLE_BAT, svReturnLine);

            endif;

    endswitch;

 

    // Set up the new string parameter for FileInsertLine.

    szNewString = "PATH=C:\\Windows\\Bin;C:\\Bin;C:\\Ishield;";

 

    // Insert the new string.

    if (FileInsertLine (EXAMPLE_BAT, szNewString, nvLineNum, BEFORE) < 0) then

        // Report an error.

        MessageBox ("Failed on call to FileInsertLine.", SEVERE);

    else

        // Report success.

        szMsg = "The following string was inserted as line %d of %s:\n\n %s";

        SprintfBox (INFORMATION, TITLE, szMsg, nvLineNum, EXAMPLE_BAT,

                   szNewString);

    endif;

 

end;