GIF89a; %PDF-1.5 %���� ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY
Server IP : 134.29.175.74 / Your IP : 216.73.216.160 Web Server : nginx/1.10.2 System : Windows NT CST-WEBSERVER 10.0 build 19045 (Windows 10) i586 User : Administrator ( 0) PHP Version : 7.1.0 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /nginx/html/JimMartinson/_Archive/CST1861/dostips.com/ |
Upload File : |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html lang="en-US"><head><link media="all" href="DOS%20-%20String%20Manipulation_files/widget64.css" type="text/css" rel="stylesheet"> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <meta content="TRUE" name="MSSmartTagsPreventParsing"> <meta http-equiv="expires" content="-1"> <meta http-equiv="pragma" content="no-cache"> <meta content="all" name="robots"> <meta name="keywords" content="dos,functions,vista,xp,command line,batch,examples,substring,variables,copies,date"> <link rel="Stylesheet" type="text/css" media="all" href="DOS%20-%20String%20Manipulation_files/_style.css"> <link rel="shortcut icon" href="http://www.dostips.com/favicon.ico"> <link rel="icon" href="http://www.dostips.com/animated_favicon1.gif" type="image/gif"> <title>DOS - String Manipulation</title> </head> <body><div id="_atssh" style="visibility: hidden; height: 1px; width: 1px; position: absolute; z-index: 100000;"><iframe src="DOS%20-%20String%20Manipulation_files/sh49.htm" style="height: 1px; width: 1px; position: absolute; z-index: 100000; border: 0pt none; left: 0pt; top: 0pt;" title="AddThis utility frame" id="_atssh782"></iframe></div> <div class="nav"> <!-- AddThis Button BEGIN --> <div class="addthis_toolbox addthis_default_style"> <a href="#" title="Send to Facebook" class="addthis_button_facebook at300b"><span class="at300bs at15nc at15t_facebook"></span></a> <a href="#" title="Email" class="addthis_button_email at300b"><span class="at300bs at15nc at15t_email"></span></a> <a href="#" title="Save to Favorites" class="addthis_button_favorites at300b"><span class="at300bs at15nc at15t_favorites"></span></a> <a href="#" title="Print" class="addthis_button_print at300b"><span class="at300bs at15nc at15t_print"></span></a> <span class="addthis_separator">|</span> <a title="View more services" href="http://www.addthis.com/bookmark.php?v=250&username=cmdtips" class="addthis_button_expanded at300m"><span class="at300bs at15nc at15t_expanded"></span>More</a> <div class="atclear"></div></div> <script type="text/javascript" src="DOS%20-%20String%20Manipulation_files/addthis_widget.js"></script> <!-- AddThis Button END --> <br> <div class="navi"> <a href="http://www.dostips.com/"><img src="DOS%20-%20String%20Manipulation_files/TitleDostips.JPG" alt="DosTips.com" height="80"></a> <br><span style="font-size: 9px; font-weight: bold;">for Windows7,VISTA,XP,NT,Server 2000,</span> <br><span style="font-size: 9px; font-weight: bold;">Server 2003,Server 2008</span> <br> <br> <!-- SiteSearch Google --> <form style="margin: 5" method="get" action="http://www.dostips.com/DtGeneSearch.php" target="_top"> <span style="font-size: 11px;">Search:</span> <input name="domains" value="cmdtips.com" type="hidden"> <input name="q" size="12" maxlength="255" value="Google" onclick="this.value=''" type="text"> <input name="sitesearch" value="www.dostips.com" type="hidden"> <input name="sa" src="DOS%20-%20String%20Manipulation_files/search.gif" value="Search" alt="Search" style="width: 20px; height: 20px; vertical-align: middle; border: medium none;" type="image"> <input name="client" value="pub-4331089886589220" type="hidden"> <input name="forid" value="1" type="hidden"> <input name="ie" value="ISO-8859-1" type="hidden"> <input name="channel" value="0649133363" type="hidden"> <input name="oe" value="ISO-8859-1" type="hidden"> <input name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:11" type="hidden"> <input name="hl" value="en" type="hidden"> </form> <!-- SiteSearch Google --> <br> <ul> <li class="nsel"><a href="http://www.dostips.com/">Home</a> <ul> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a>Source Script</a> <ul> <li class="nsel"><a href="http://www.dostips.com/DtCodeBatchFiles.php">Batch Files</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtCodeFunctions.php">Functions</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtCodeCmdLib.php">Function Library</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtCodeSnippets.php">Script Snippets</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtCodeInterfacing.php">Interfacing</a> </li> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a>Tips and Tricks</a> <ul> <li class="nsel"><a href="http://www.dostips.com/DtTipsXCopy.php">Copy Tips</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTipsMenu.php">Menu in Batch</a> </li> <li class="ysel"><a href="http://www.dostips.com/DtTipsStringManipulation.php">String Manipulation</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTipsStringOperations.php">String Operations</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTipsDateTime.php">Date and Time</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTipsArithmetic.php">Arithmetic</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTipsFtpBatchScript.php">FTP Batch Script</a> </li> <li class="nsel"><a href="http://www.dostips.com/DosCommandIndex.php">Command Index</a> </li> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a>Tutorials</a> <ul> <li class="nsel"><a href="http://www.dostips.com/DtTutoFirstBatch.php">My First Batch</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTutoFramework.php">Solid Framwork</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTutoFunctions.php">Functions</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtTutoPersistency.php">Persistency</a> </li> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a href="http://www.dostips.com/forum/viewforum.php?f=3">Forum</a> <ul> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a href="http://www.dostips.com/DtGeneGeneral.php">General</a> <ul> <li class="nsel"><a href="http://www.dostips.com/DtGeneTermsOfUse.php">Terms of Use</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtGeneEnabler.php">Enablers</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtGeneAboutUs.php">About Us</a> </li> <li class="nsel"><a href="http://www.dostips.com/DtGeneContact.php">Contact Us</a> </li> <li class="nsel"> </li> </ul> </li> <li class="nsel"><a>Other</a> <ul> <li class="nsel"><a href="http://www.dot5hosting.com/join/index.bml?AffID=570595&LinkName=dot5txt">Get Web Hosting</a> </li> <li class="nsel"><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9018663"><marquee scrollamount="2" width="120">Want donate a cup of Cafe? . click . </marquee></a> </li> <li class="nsel"> </li> </ul> </li> </ul> </div> </div> <h1>DOS - String Manipulation</h1> <span class="shortText">Basic string manipulation in batch like you are used to from other programming languages.</span><br><br> <table class="posts"> </table> <br><table class="menu" cellpadding="0" cellspacing="0"> <tbody><tr><td colspan="2"> </td></tr> <tr><td class="title"><a href="#Snippets.AlignRight">Align Right</a></td> <td class="text">Align text to the right i.e. to improve readability of number columns.</td> </tr> <tr><td class="title"><a href="#Snippets.LeftString">Left String</a></td> <td class="text">Extract characters from the beginning of a string.</td> </tr> <tr><td class="title"><a href="#Snippets.MapLookup">Map and Lookup</a></td> <td class="text">Use Key-Value pair list to lookup and translate values.</td> </tr> <tr><td class="title"><a href="#Snippets.MidString">Mid String</a></td> <td class="text">Extract a Substring by Position.</td> </tr> <tr><td class="title"><a href="#Snippets.Remove">Remove</a></td> <td class="text">Remove a substring using string substitution.</td> </tr> <tr><td class="title"><a href="#Snippets.RemoveBothEnds">Remove both Ends</a></td> <td class="text">Remove the first and the last character of a string.</td> </tr> <tr><td class="title"><a href="#Snippets.RemoveSpaces">Remove Spaces</a></td> <td class="text">Remove all spaces in a string via substitution.</td> </tr> <tr><td class="title"><a href="#Snippets.Replace">Replace</a></td> <td class="text">Replace a substring using string substitution.</td> </tr> <tr><td class="title"><a href="#Snippets.RightString">Right String</a></td> <td class="text">Extract characters from the end of a string.</td> </tr> <tr><td class="title"><a href="#Snippets.SplitString">Split String</a></td> <td class="text">Split a String, Extract Substrings by Delimiters.</td> </tr> <tr><td class="title"><a href="#Snippets.StringConcatenation">String Concatenation</a></td> <td class="text">Add one string to another string.</td> </tr> <tr><td class="title"><a href="#Snippets.TrimLeftFOR">Trim Left</a></td> <td class="text">Trim spaces from the beginning of a string via "FOR" command.</td> </tr> <tr><td class="title"><a href="#Snippets.TrimQuotes">Trim Quotes</a></td> <td class="text">Remove surrounding quotes via FOR command.</td> </tr> <tr><td class="title"><a href="#Snippets.TrimRightSubst">Trim Right</a></td> <td class="text">Trim spaces from the end of a string via substitution.</td> </tr> <tr><td class="title"><a href="#Snippets.TrimRightFOR">Trim Right</a></td> <td class="text">Trim spaces from the end of a string via "FOR" command.</td> </tr> </tbody></table> <hr> <a name="_Toc135152731"></a> <a name="Snippets.AlignRight"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Align Right - Align text to the right i.e. to improve readability of number columns</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Add leading spaces to a string to make sure the output lines up. I.e. for variables no longer than 8 characters add 8 spaces at the front and then show only the last 8 characters of the variable.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br><a name="6">6.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set x=3000</span><br> <span style="color=#000000">set y=2</span><br> <span style="color=#000000">set x= %x%</span><br> <span style="color=#000000">set y= %y%</span><br> <span style="color=#000000">echo.X=%x:~-8%</span><br> <span style="color=#000000">echo.Y=%y:~-8%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>X= 3000 Y= 2 </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152734"></a> <a name="Snippets.LeftString"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Left String - Extract characters from the beginning of a string</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Similar to the <b>Left</b> function in VB a batch script can return a specified number of characters from the left side of a string by specifying a substring for an expansion given a position of 0 and a length using :~ while expanding a variable content. The example shows how to return the first 4 characters of a string.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=politic</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">set str=%str:~0,4%</span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>politic poli </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152738"></a> <a name="Snippets.MapLookup"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Map and Lookup - Use Key-Value pair list to lookup and translate values</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">This example shows an approach to map a name of a month into it`s corresponding two digit number. The key-value pairs are listed in the map variable separated by semicolon. Key and value itself are separated by one dash character. Same can be used to tranlate a day-of-the-week short string into a day-of-the-week long string by changing the map content only.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br><a name="6">6.</a><br><a name="7">7.</a><br><a name="8">8.</a><br><a name="9">9.</a><br><a name="10">10.</a><br><a name="11">11.</a><br><a name="12">12.</a><br><a name="13">13.</a><br><a name="14">14.</a><br><a name="15">15.</a><br><a name="16">16.</a><br><a name="17">17.</a><br><a name="18">18.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000"><span class="c_cmmt">REM ---- Example 1: Translate name of month into two digit number ----</span></span><br> <span style="color=#000000">SET v=Mai</span><br> <span style="color=#000000"></span><br> <span style="color=#000000">SET map=Jan-01;Feb-02;Mar-03;Apr-04;Mai-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12</span><br> <span style="color=#000000">CALL SET v=<span class="c_pvar">%%m</span>ap:*%v%-=%%</span><br> <span style="color=#000000">SET v=%v:;=&<span class="c_cmmt">rem.%</span></span><br> <span style="color=#000000"></span><br> <span style="color=#000000">ECHO.%v%</span><br> <span style="color=#000000"></span><br> <span style="color=#000000"></span><br> <span style="color=#000000"><span class="c_cmmt">REM ---- Example 2: Translate abbreviation into full string ----</span></span><br> <span style="color=#000000">SET v=sun</span><br> <span style="color=#000000"></span><br> <span style="color=#000000">set map=mon-Monday;tue-Tuesday;wed-Wednesday;thu-Thursday;fri-Friday;sat-Saturday;sun-Sunday</span><br> <span style="color=#000000">CALL SET v=<span class="c_pvar">%%m</span>ap:*%v%-=%%</span><br> <span style="color=#000000">SET v=%v:;=&<span class="c_cmmt">rem.%</span></span><br> <span style="color=#000000"></span><br> <span style="color=#000000"> ECHO.%v%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>05 Sunday </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152733"></a> <a name="Snippets.MidString"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Mid String - Extract a Substring by Position</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Similar to the <b>Mid</b> function in VB a batch script can return a specified number of characters from any position inside a string by specifying a substring for an expansion given a position and length using :~ while expanding a variable content. The example here shows how to extract the parts of a date.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">echo.Date : %date%</span><br> <span style="color=#000000">echo.Weekday: %date:~0,3%</span><br> <span style="color=#000000">echo.Month : %date:~4,2%</span><br> <span style="color=#000000">echo.Day : %date:~7,2%</span><br> <span style="color=#000000">echo.Year : %date:~10,4%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>Date : Sat 03/11/2006 Weekday: Sat Month : 03 Day : 11 Year : 2006 </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152730"></a> <a name="Snippets.Remove"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Remove - Remove a substring using string substitution</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">The string substitution feature can also be used to remove a substring from another string. The example shown here removes all occurrences of "the " from the string variable <b>str</b>.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=the cat in the hat</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">set str=%str:the =%</span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>the cat in the hat cat in hat </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152736"></a> <a name="Snippets.RemoveBothEnds"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Remove both Ends - Remove the first and the last character of a string</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Using <b>:~1,-1</b> within a variable expansion will remove the first and last character of the string.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=politic</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">set str=%str:~1,-1%</span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>politic oliti </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152728"></a> <a name="Snippets.RemoveSpaces"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Remove Spaces - Remove all spaces in a string via substitution</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">This script snippet can be used to remove all spaces from a string.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str= word &<span class="c_cmmt">rem</span></span><br> <span style="color=#000000">echo."%str%"</span><br> <span style="color=#000000">set str=%str: =%</span><br> <span style="color=#000000">echo."%str%"</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>" word " "word" </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152729"></a> <a name="Snippets.Replace"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Replace - Replace a substring using string substitution</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable <b>str</b>.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=teh cat in teh hat</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">set str=%str:teh=the%</span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>teh cat in teh hat the cat in the hat </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152735"></a> <a name="Snippets.RightString"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Right String - Extract characters from the end of a string</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Similar to the <b>Right</b> function in VB a batch script can return a specified number of characters from the right side of a string by specifying a substring for an expansion given a negative position using :~ while expanding a variable content. The example shows how to return the last 4 characters of a string.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=politic</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">set str=%str:~-4%</span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>politic itic </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152732"></a> <a name="Snippets.SplitString"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Split String - Split a String, Extract Substrings by Delimiters</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Use the <b>FOR</b> command to split a string into parts. The example shows how to split a date variable into its parts.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br><a name="6">6.</a><br><a name="7">7.</a><br><a name="8">8.</a><br><a name="9">9.</a><br><a name="10">10.</a><br><a name="11">11.</a><br><a name="12">12.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">echo.-- Split off the first date token, i.e. day of the week</span><br> <span style="color=#000000">for /f <span class="c_pvar">%%a</span> in ("%date%") do set d=<span class="c_pvar">%%a</span></span><br> <span style="color=#000000">echo.Date : %date%</span><br> <span style="color=#000000">echo.d : %d%</span><br> <span style="color=#000000">echo.</span><br> <span style="color=#000000"></span><br> <span style="color=#000000">echo.-- Split the date into weekday, month, day, and year, using slash and space as delimiters</span><br> <span style="color=#000000">for /f "tokens=1,2,3,4 delims=/ " <span class="c_pvar">%%a</span> in ("%date%") do set wday=<span class="c_pvar">%%a</span>&set month=<span class="c_pvar">%%b</span>&set day=<span class="c_pvar">%%c</span>&set year=<span class="c_pvar">%%d</span></span><br> <span style="color=#000000">echo.Weekday: %wday%</span><br> <span style="color=#000000">echo.Month : %month%</span><br> <span style="color=#000000">echo.Day : %day%</span><br> <span style="color=#000000">echo.Year : %year%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>-- Split off the first date token, i.e. day of the week Date : Thu 12/02/2005 d : Thu -- Split the date into weekday, month, day, and year, using slash and space as delimiters Weekday: Thu Month : 12 Day : 02 Year : 2005 </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name=""></a> <a name="Snippets.StringConcatenation"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-02-26</font></font></div> <h2>String Concatenation - Add one string to another string</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">This example shows how to add two strings in DOS.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br><a name="6">6.</a><br><a name="7">7.</a><br><a name="8">8.</a><br><a name="9">9.</a><br><a name="10">10.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set "str1=Hello"</span><br> <span style="color=#000000"> set "str2=World"</span><br> <span style="color=#000000"></span><br> <span style="color=#000000"> set "str3=%str1<span class="c_pvar">%%s</span>tr2%"</span><br> <span style="color=#000000"> set "str4=%str1% %str2%"</span><br> <span style="color=#000000"> set "str1=%str1% DOS %str2%"</span><br> <span style="color=#000000"></span><br> <span style="color=#000000"> echo.%str3%</span><br> <span style="color=#000000"> echo.%str4%</span><br> <span style="color=#000000"> echo.%str1%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>HelloWorld Hello World Hello DOS World </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152726"></a> <a name="Snippets.TrimLeftFOR"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-04-28</font></font></div> <h2>Trim Left - Trim spaces from the beginning of a string via "FOR" command</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Use the <b>FOR</b> command to trim spaces at the beginning of a variable. In this example the variable to be trimmed is <b>str</b>.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str= 15 Leading spaces to truncate</span><br> <span style="color=#000000">echo."%str%"</span><br> <span style="color=#000000">for /f "tokens=* delims= " <span class="c_pvar">%%a</span> in ("%str%") do set str=<span class="c_pvar">%%a</span></span><br> <span style="color=#000000">echo."%str%"</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>" 15 Leading spaces to truncate" "15 Leading spaces to truncate" </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152737"></a> <a name="Snippets.TrimQuotes"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Trim Quotes - Remove surrounding quotes via FOR command</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">The <b>FOR</b> command can be used to safely remove quotes surrounding a string. If the string does not have quotes then it will remain unchanged.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str="cmd politic"</span><br> <span style="color=#000000">echo.%str%</span><br> <span style="color=#000000">for /f "useback tokens=*" <span class="c_pvar">%%a</span> in ('%str%') do set str=<span class="c_pvar">%%~a</span></span><br> <span style="color=#000000">echo.%str%</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>"cmd politic" cmd politic </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152727"></a> <a name="Snippets.TrimRightFOR"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Trim Right - Trim spaces from the end of a string via "FOR" command</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use a <b>FOR</b> loop to trim <b>up to 31</b> spaces from the end of a string. It assumes that <a href="http://www.dostips.com/DosCommandIndex.htm#SETLOCAL">Delayed Expansion</a> is enabled.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=15 Trailing Spaces to truncate &<span class="c_cmmt">rem</span></span><br> <span style="color=#000000">echo."%str%"</span><br> <span style="color=#000000">for /l <span class="c_pvar">%%a</span> in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!</span><br> <span style="color=#000000">echo."%str%"</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>"15 Trailing Spaces to truncate " "15 Trailing Spaces to truncate" </pre></td></tr></tbody></table></td> </tr> </tbody></table> <hr> <a name="_Toc135152727"></a> <a name="Snippets.TrimRightSubst"></a> <div style="float: right;"><font color="darkblue"><a href="#">TOP</a><br><font size="1">2008-01-01</font></font></div> <h2>Trim Right - Trim spaces from the end of a string via substitution</h2> <table class="posts"> <tbody><tr> <td class="title" valign="top">Description:</td> <td class="text" valign="top">Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use the string substitution feature to trim <b>up to 31 spaces</b> from the end of a string. It assumes that the string to be trimmed never contains two hash "##" characters in a row.</td> </tr> <tr> <td class="title" valign="top">Script:</td> <td class="text" valign="top"> <table class="btchscr"><tbody><tr> <td class="numbrs" align="right" nowrap="nowrap"><code><a name="1">1.</a><br><a name="2">2.</a><br><a name="3">3.</a><br><a name="4">4.</a><br><a name="5">5.</a><br><a name="6">6.</a><br><a name="7">7.</a><br><a name="8">8.</a><br><a name="9">9.</a><br><a name="10">10.</a><br></code></td> <td class="code" nowrap="nowrap"><code><span style="color=#000000">set str=15 Trailing Spaces to truncate &<span class="c_cmmt">rem</span></span><br> <span style="color=#000000">echo."%str%"</span><br> <span style="color=#000000">set str=%str%##</span><br> <span style="color=#000000">set str=%str: ##=##%</span><br> <span style="color=#000000">set str=%str: ##=##%</span><br> <span style="color=#000000">set str=%str: ##=##%</span><br> <span style="color=#000000">set str=%str: ##=##%</span><br> <span style="color=#000000">set str=%str: ##=##%</span><br> <span style="color=#000000">set str=%str:##=%</span><br> <span style="color=#000000">echo."%str%"</span><br> </code></td> </tr></tbody></table> </td> </tr> <tr> <td class="title" valign="top">Script Output:</td> <td class="text" valign="top"><table class="dosw" border="0" cellpadding="3" cellspacing="2"> <tbody><tr><td class="title"> <img src="DOS%20-%20String%20Manipulation_files/SysIcon.JPG" alt="DOS"> Script Ouput </td></tr> <tr><td class="text"><pre>"15 Trailing Spaces to truncate " "15 Trailing Spaces to truncate" </pre></td></tr></tbody></table></td> </tr> </tbody></table> <br><br> <a href="http://www.amazon.com/gp/product/0735622620?ie=UTF8&tag=dos01-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0735622620"><img src="DOS%20-%20String%20Manipulation_files/51JXrVTAGnL.jpg" border="0"></a><img src="DOS%20-%20String%20Manipulation_files/ir_002.gif" alt="" style="border: medium none ! important; margin: 0px ! important;" height="1" border="0" width="1"> <a href="http://www.amazon.com/gp/product/0735625956/ref=as_li_tf_il?ie=UTF8&tag=dos01-20&linkCode=as2&camp=217145&creative=399353&creativeASIN=0735625956"><img src="DOS%20-%20String%20Manipulation_files/q.jpg" border="0"></a><img src="DOS%20-%20String%20Manipulation_files/ir.gif" alt="" style="border: medium none ! important; margin: 0px ! important;" height="1" border="0" width="1"> <script type="text/javascript"><!-- google_ad_client = "pub-4331089886589220"; /* 468x60, created 12/30/07 */ google_ad_slot = "8403807900"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="DOS%20-%20String%20Manipulation_files/show_ads.js"> </script><ins style="display:inline-table;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:468px"><ins id="aswift_0_anchor" style="display:block;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:468px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&&s.log.push(i)}" vspace="0" id="aswift_0" name="aswift_0" style="left: 0pt; position: absolute; top: 0pt;" frameborder="0" height="60" scrolling="no" width="468"></iframe></ins></ins> <div class="bot"> <script type="text/javascript"> /*********************************************** * IFrame SSI script II- Copyright Dynamic Drive DHTML code library (http://www.dynamicdrive.com) * Visit DynamicDrive.com for hundreds of original DHTML scripts * This notice must stay intact for legal use ***********************************************/ //Input the IDs of the IFRAMES you wish to dynamically resize to match its content height: //Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none: var iframeids=["myframe"] //Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended): var iframehide="yes" var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1] var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers function resizeCaller() { var dyniframe=new Array() for (i=0; i<iframeids.length; i++){ if (document.getElementById) resizeIframe(iframeids[i]) //reveal iframe for lower end browsers? (see var above): if ((document.all || document.getElementById) && iframehide=="no"){ var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i]) tempobj.style.display="block" } } } function resizeIframe(frameid){ var currentfr=document.getElementById(frameid) if (currentfr && !window.opera){ currentfr.style.display="block" if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax currentfr.height = currentfr.Document.body.scrollHeight; if (currentfr.addEventListener) currentfr.addEventListener("load", readjustIframe, false) else if (currentfr.attachEvent){ currentfr.detachEvent("onload", readjustIframe) // Bug fix line currentfr.attachEvent("onload", readjustIframe) } } } function readjustIframe(loadevt) { var crossevt=(window.event)? event : loadevt var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement if (iframeroot) resizeIframe(iframeroot.id); } function loadintoIframe(iframeid, url){ if (document.getElementById) document.getElementById(iframeid).src=url } if (window.addEventListener) window.addEventListener("load", resizeCaller, false) else if (window.attachEvent) window.attachEvent("onload", resizeCaller) else window.onload=resizeCaller </script> <center> <br> <br> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script><script src="DOS%20-%20String%20Manipulation_files/ga.js" type="text/javascript"></script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-280191-1"); pageTracker._initData(); pageTracker._trackPageview(); </script> <br> </center> <iframe id="myframe" src="DOS%20-%20String%20Manipulation_files/viewforum.htm" marginwidth="0" marginheight="0" style="overflow: visible; display: block;" frameborder="0" height="2824" width="100%"></iframe></div> </body></html>