August 17, 2012

How to check required Space on disc using InstallScript?

At times, we need Custom Functions (over & above the InstallShield's default behaviour) to check the availability of disc space before proceeding with the installation. Here is the code sample for the same.

///////////////////////////////////////////////////////////////////////////////
//  FUNCTION:   CheckRequiredSpace
//  Purpose:    Performs space requirements check for selected targetdir.
///////////////////////////////////////////////////////////////////////////////
function CheckRequiredSpace()
    STRING svRequired;
    BOOL   bCheck;
    NUMBER nResult, nvTotalRequiredSpace, nvTotalAvailableSpace;
begin
    //Get total required disk space
    nResult = FeatureGetTotalCost(MEDIA, TARGETDIR, nvTotalRequiredSpace);
    if (nResult < 0) then
          return -1;
    endif;
    //Add 10000 KB as safety buffer to required space
    nvTotalRequiredSpace = nvTotalRequiredSpace + 10000;
    //Get total available disk space
    nvTotalAvailableSpace = GetDiskSpaceEx(TARGETDIR, KBYTES);
    if (nResult < 0) then
          return -1;
    endif;
    //Convert nvTotalRequiredSpace to string
    NumToStr(svRequired, nvTotalRequiredSpace);
    //Check if there is enough space available
    if (nvTotalRequiredSpace > nvTotalAvailableSpace) then
            MessageBox(@CheckRequiredDiskSpace_Error + svRequired + " KB", WARNING);
            return 0;
    endif;
    return 1;
end;

This function applies to Pure InstallScript & InstallScript MSI Projects.

2 comments:

  1. thank you so much, you saved my time...

    ReplyDelete
  2. Glad to hear that it was helpful, Dhanaraj.

    Regards
    Bhuvana

    ReplyDelete