MAKEMSI quickly and reliably creates MSI files in a non-programmatic way
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
[Bottom][Contents][Prev]: Registry Appending[Next]: Share Creation
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
\->Tips and Tricks->File and Directory->Remove Trailing Slashes from Directory Names

Remove Trailing Slashes from Directory Names

Unfortunately Windows Installer directory names always end with a slash (thank Microsoft for that...). Your product may require that you create a registry value or ini file entry without a trailing slash.

This demonstrates custom actions in 3 languages removing the trailing backslash off the "INSTALLDIR" property and creating a new property called "DIR_NO_SLASH" with the result.

Example #1 - VBSCRIPT

<$VbsCa Binary="!SimpleVbs">
     InstallDir = session.property("INSTALLDIR")
     InstallDir = left(InstallDir, len(InstallDir)-1)

     'MsgBox "DEBUG: The INSTALLDIR without a trailing backslash ===> " & InstallDir
     session.property("DIR_NO_SLASH") = InstallDir      ;;You will see this action logged in any verbose log
<$/VbsCa>
<$VbsCaSetup Binary="!SimpleVbs" Seq="CostFinalize-" SeqTable="InstallExecuteSequence" CONDITION=^<$CONDITION_INSTALL_ONLY>^>

Example #2 - JSCRIPT

<$JsCa Binary="!SimpleJS">
     var InstallDir = Session.Property("INSTALLDIR");
     InstallDir = InstallDir.slice(0, -1);              ;;Remove last character (\) from the path
     Session.Property("DIR_NO_SLASH") = InstallDir      ;;You will see this action logged in any verbose log
<$/JsCa>
<$JsCaSetup Binary="!SimpleJS" Seq="CostFinalize-" SeqTable="InstallExecuteSequence" CONDITION=^<$CONDITION_INSTALL_ONLY>^>

Example #3 - C++ DLL

<$DllCa-C Binary="SimpleSlashRemoval.dll">
    //============================================================================
    <$DllCaEntry "INSTALLDIR">
    //============================================================================
    {
        ;--- Get the value of the path (with terminating backslash) ---------
        UINT  Rc;
        TCHAR Path[300] = {0};
        DWORD PathLng   = sizeof(Path) / sizeof(TCHAR);
        Rc = MsiGetProperty(hInstall, "INSTALLDIR",  Path, &PathLng);
        if  (Rc != ERROR_SUCCESS)
        {
            CaDebugv(PROGRESS_LOG, "MsiGetProperty(\"INSTALLDIR\") failed with RC = %u", Rc);
            return(1603);
        }
        CaDebugv(PROGRESS_LOG, "The property contained \"%s\".", Path);

        //--- Now remove any terminating slash -------------------------------
        if  (Path[PathLng-1] == '\\')
            *(&Path[PathLng-1]) = '\0';
        CaDebugv(PROGRESS_LOG, "The value without a terminating slash is \"%s\".", Path);

        ;--- Now create "DIR_NO_SLASH" property with modified path ----------
        Rc = MsiSetProperty(hInstall, "DIR_NO_SLASH", Path);
        if  (Rc != ERROR_SUCCESS)
        {
            CaDebugv(PROGRESS_LOG, "MsiSetProperty(\"DIR_NO_SLASH\") failed with RC = %u", Rc);
            return(1603);
        }

        //--- Return successful to Windows Installer -------------------------
        return(0);
    }
    <$/DllCaEntry>
<$/DllCa-C>
<$DllCa Binary="SimpleSlashRemoval.dll" Seq="CostFinalize-" Type="Immediate" \
         Entry=^<$DllCaEntry? "INSTALLDIR">^                                 \
         Condition="<$DLLCA_CONDITION_INSTALL_ONLY>"                         \
>

Example - Use the resultant Property...

<$Component "TestValues" Create="Y" Directory_="INSTALLDIR" LM="Y">
    <$Registry HKEY="LOCAL_MACHINE" KEY="software\testmsi\WithSlash"  VALUE="[INSTALLDIR]"   MsiFormatted="VALUE">
    <$Registry HKEY="LOCAL_MACHINE" KEY="software\testmsi\NoSlash"    VALUE="[DIR_NO_SLASH]" MsiFormatted="VALUE">
<$/Component>


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Registry Appending[Next]: Share Creation


MAKEMSI© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday February 28 2016 at 3:45pm
Visit MAKEMSI's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.