RenameFile 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 RenameFile function.

*

* First, RenameFile is called to rename FILENAME1 to FILENAME2.

* It is then called again to move the FILENAME2 file to the

* TARGET directory.

*

* This can also be done in one call to RenameFile.  The third

* call to RenameFile demonstrates this by renaming and moving

* the FILENAME2 file from the TARGET directory to the SOURCE

* directory, with the FILENAME1 file name.

*

* Note: Before running this script, set the preprocessor

*       constants so that they specify valid file names and

*       paths on the target system.

*

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

 

#define FILENAME1  "ISExampl.txt"

#define FILENAME2  "ISExampl.bak"

#define SOURCE_DIR "C:\\ISExampl\\Source"

#define TARGET_DIR "C:\\ISExampl\\Target"

#define TITLE      "RenameFile Example"

 

 

#include "ifx.h"

 

function OnBegin()

begin

 

   // Set up system variables for rename operation.

   SRCDIR    = SOURCE_DIR;

   TARGETDIR = SOURCE_DIR;

 

   // Rename FILENAME1 to FILENAME2.

   if (RenameFile (FILENAME1, FILENAME2) < 0) then

      MessageBox("First call to RenameFile failed.", SEVERE);

      abort;

   else

      szMsg = "%s successfully renamed to %s.";

      SprintfBox(INFORMATION, szTitle, szMsg, FILENAME1, FILENAME2);

   endif;

 

   // Set up system variables to move a file from one directory to another.

   SRCDIR    = SOURCE_DIR;

   TARGETDIR = TARGET_DIR;

 

   // Move the file from the SOURCE to the TARGET directory.

   if (RenameFile(FILENAME2, FILENAME2) < 0) then

      MessageBox("Second call to RenameFile failed.", SEVERE);

      abort;

   else

      szMsg = "%s successfully moved to %s.";

      SprintfBox(INFORMATION, TITLE, szMsg, FILENAME2, TARGETDIR);

   endif;

 

   // Set up system variables to move the file back to its original location.

   SRCDIR    = TARGET_DIR;

   TARGETDIR = SOURCE_DIR;

 

   // Rename the file and move it from the TARGET directory

   // to the SOURCE directory.

   if (RenameFile(FILENAME2, FILENAME1) < 0) then

      MessageBox("Third call to RenameFile failed.", SEVERE);

      abort;

   else

      szMsg = "%s successfully renamed %s and moved to the directory %s.";

      SprintfBox(INFORMATION, TITLE, szMsg, FILENAME2, FILENAME1, TARGETDIR);

   endif;

 

end;