\Tips and TricksFile and DirectoryRemove 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.
<$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>^>
<$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>^>
<$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>