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]: SET Command[Next]: SubString Extraction
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
\->Tips and Tricks->Batch Files->Batch File Tips and Tricks->SET Command->String Replacement

String Replacement

Environment variable substitution allows for string (or character) conversion, the syntax is pretty simple, type this from a command line:
echo PATHX: %PATH:c:=X:%

If you tried the above you would have seen that the contents of the "PATH" environment variable was echoed but with all "C:" strings replaced with "X:" (case insensitive match).

Note that there is a bug in this area, it will fail (badly) if the environment variable doesn't exist. For this reason I recommend adding and then stripping a character to the start of end of the environment variable or using the "if defined" syntax to only expand once you know it exists...

The "Get File Safe Dates and Times" tip uses character replacement to replace "/" and ":" characters and here is another example:

set   String=123456789
set  Changed=%String:2=*%
set  Changed=%Changed:3=%
echo Original string was "%String%", was changed to "%Changed%"

echo DATE-SAFE "%date:/=-%" (date env var with "/" replaced by "-")
echo TIME-SAFE "%time::=_%" (time env var with ":" replaced by "_")

Handling "empty" Strings

Due to a Microsoft bug string substitution fails on empty strings. The workaround is to ensure that you never do this by:

  1. Make a possibly empty value non empty by adding a character (in the example below we add "#" to the start).

  2. Do the conversion without worries :-)

  3. Strip off the character we added in the first step.

@echo off
setlocal ENABLEDELAYEDEXPANSION
set /p SomeThing=Enter something ==^> &rem
call :SafelyRemoveAllSpaces "SomeThing"
echo The "SomeThing" environment variable contains "%SomeThing%"...
goto :EOF

@rem ================
:SafelyRemoveAllSpaces
@rem ================
    @rem P1 = NAME OF ENV VAR (MS BUG: need to handle value of "")
    set VAL=#!%~1!
    set VAL=%VAL: =%
    set %~1=%VAL:~1%
    goto :EOF

ToLowerCase.CMD - Case Conversion

Substitution is case sensitive, this is one of the reasons you may wish to convert a string to all lower or all upper case first, this simple batch file shows how to convert to lower case:

@echo off
cls
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set SomeString=TheRainInSpain
    echo SomeString(BEF) = %SomeString%
    call :ToLowerCase "SomeString"
    echo SomeString(AFT) = %SomeString%
goto :EOF



@rem +++++++++++++++++++++++++++++++++++
:ToLowerCase
@rem +++++++++++++++++++++++++++++++++++
    set  TLC_NAME=%~1
    set TLC_VALUE=!%TLC_NAME%!
    if "%TLC_VALUE%" == "" goto :EOF
    set TLC_VALUE=%TLC_VALUE:A=a%
    set TLC_VALUE=%TLC_VALUE:B=b%
    set TLC_VALUE=%TLC_VALUE:C=c%
    set TLC_VALUE=%TLC_VALUE:D=d%
    set TLC_VALUE=%TLC_VALUE:E=e%
    set TLC_VALUE=%TLC_VALUE:F=f%
    set TLC_VALUE=%TLC_VALUE:G=g%
    set TLC_VALUE=%TLC_VALUE:H=h%
    set TLC_VALUE=%TLC_VALUE:I=i%
    set TLC_VALUE=%TLC_VALUE:J=j%
    set TLC_VALUE=%TLC_VALUE:K=k%
    set TLC_VALUE=%TLC_VALUE:L=l%
    set TLC_VALUE=%TLC_VALUE:M=m%
    set TLC_VALUE=%TLC_VALUE:N=n%
    set TLC_VALUE=%TLC_VALUE:O=o%
    set TLC_VALUE=%TLC_VALUE:P=p%
    set TLC_VALUE=%TLC_VALUE:Q=q%
    set TLC_VALUE=%TLC_VALUE:R=r%
    set TLC_VALUE=%TLC_VALUE:S=s%
    set TLC_VALUE=%TLC_VALUE:T=t%
    set TLC_VALUE=%TLC_VALUE:U=u%
    set TLC_VALUE=%TLC_VALUE:V=v%
    set TLC_VALUE=%TLC_VALUE:W=w%
    set TLC_VALUE=%TLC_VALUE:X=x%
    set TLC_VALUE=%TLC_VALUE:Y=y%
    set TLC_VALUE=%TLC_VALUE:Z=z%
    set %TLC_NAME%=%TLC_VALUE%
    goto :EOF


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]: SET Command[Next]: SubString Extraction


MAKEMSI© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Saturday May 28 2022 at 3:11pm
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.