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]: String Replacement[Next]: Special Characters (tabs etc)
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->SubString Extraction

SubString Extraction

Sometimes you want to extract certain characters out of a bigger string or environment variable, this shows how it can be done:

@echo off
@rem *** Copy to "TryMe.cmd" and execute :-) ***
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
cls

set   WholeString=123456789
set        Left20=%WholeString:~0,20%
set    Left4Chars=%WholeString:~0,4%
set   Right4Chars=%WholeString:~-4%
set  The3rdAnd4th=%WholeString:~2,2%
set   AllButFirst=%WholeString:~1%
set    AllButLast=%WholeString:~0,-1%
set  Left20Padded=%WholeString%                    &rem Add 20 spaces
set  Left20Padded=%Left20Padded:~0,20%&             rem Grab left most to produce passed string


echo WholeString  = "%WholeString%"
echo.
echo Left20       = "%Left20%"            (note: wasn't padded to 20 characters)
echo Left20 (pad) = "%Left20Padded%"
echo Left4Chars   = "%Left4Chars%"
echo Right4Chars  = "%Right4Chars%"
echo The3rdAnd4th = "%The3rdAnd4th%"
echo AllButFirst  = "%AllButFirst%"
echo AllButLast   = "%AllButLast%"

Remove Trailing Spaces and Slash

Note that the following example uses the code in the "Special Characters (tabs etc)" section to set up the "TAB" environment variable:

set TheDir=C:\TEMP\   &rem ends in spaces and trailing "\"...

:RemoveTrailingSpacesTabs
    if "%TheDir:~-1%" == " "     set TheDir=%TheDir:~0,-1%& goto RemoveTrailingSpacesTabs
    if "%TheDir:~-1%" == "%TAB%" set TheDir=%TheDir:~0,-1%& goto RemoveTrailingSpacesTabs
if "%TheDir:~-1%" == "\" set TheDir=%TheDir:~0,-1%


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]: String Replacement[Next]: Special Characters (tabs etc)


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.