Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: StringContains() - look for some text within another text string[Next]: Sweep Directories.cmd -
\->Batch Files->SubString Operations - extract Left, Right, middle

SubString Operations [extract Left, Right, middle].cmd

There are two main alternative approaches to extracting information out of some text string:

  1. You can break a string into parts with the "SET" command (see it for more details about the sub-string extraction syntax). There are some examples:
    set   TEXT=[ABCD]
    set  First=%TEXT:~0,1%
    set Middle=%TEXT:~1,-1%
    set   Last=%TEXT:~-1%
    echo     TEXT STRING: "%Text%"
    echo FIRST CHARACTER: "%FIRST%",  MIDDLE: "%MIDDLE%",  LAST: "%LAST%"

    Note that the first byte is at position "0" (not "1").

  2. An alternative approach is where there are characters that "delimit" the string you want then you can use the "FOR" command, for example:
    ::--- TIME Looks Like "17:55:10.02" (parts delimited by ":" and ".") ---
    for /F "tokens=1,2,3,4 delims=:." %%A in ("%TIME%") do set HH=%%A& set MM=%%B& set SS=%%C& set SSFRACTION=%%D
    echo TIME: "%TIME%" ==^> HOUR: %HH%,  Minute: %MM%,  Seconds: %SS% (.%SSFRACTION%)

    Sometimes you can convert text you know is there into a delimiter with string replacement. The follow script renames files that contain the text "HWT40 - ", it extracts the text after that and renames the file:

    @echo off
    for %%F in ("[W*.log") do call :RENAMEIT "%%F"
    goto :EOF
    
    ::######################
    :RENAMEIT
    ::######################
        set ShortNameNameBefore=%~1
        set  ShortNameNameAfter=%ShortNameNameBefore%
        if "%ShortNameNameBefore%" == "" goto :EOF
    
        set Text=%ShortNameNameBefore:HWT40 - =?%
        for /F "tokens=2 delims=?" %%A in ("%Text%") do set ShortNameNameAfter=%%A
    
        set CMD=ren "%ShortNameNameBefore%" "%ShortNameNameAfter%"
        echo.
        echo EXEC: %CMD%
                   %CMD%
        goto :EOF

[anchor]

The Code for: "SubString Operations [extract Left, Right, middle].cmd"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].SubString Operations [extract Left, Right, middle].cmd.pvcs   1.0   29 Jun 2014 12:51:22   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

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%"

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


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]: StringContains() - look for some text within another text string[Next]: Sweep Directories.cmd -


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks'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.