What Was I Thinking?

Follies & Foils of .NET Development
posts - 87 , comments - 255 , trackbacks - 0

Useful DOS Batch functions: Substring() and Length()

Recently I needed to determine the length of a string and perform a Substring operation on a variable in a DOS Batch (.bat) file.  (Yes, people still use DOS batch files!)

After some Googling and some playing around I came up with the following functions. 

 

:Substring
::Substring(retVal,string,startIndex,length)
:: extracts the substring from string starting at startIndex for the specified length 
 SET string=%2%
 SET startIndex=%3%
 SET length=%4%
 
 if "%4" == "0" goto :noLength
 CALL SET _substring=%%string:~%startIndex%,%length%%%
 goto :substringResult
 :noLength
 CALL SET _substring=%%string:~%startIndex%%%
 :substringResult
 set "%~1=%_substring%"
GOTO :EOF
 
:StrLength
::StrLength(retVal,string)
::returns the length of the string specified in %2 and stores it in %1
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
::echo the string is %length% characters long!
set "%~1=%length%"
GOTO :EOF

 

To call them:

 

:: get the lenth of the sConfigFileRoot
call:StrLength length %sCFR%

:: extract the suffix
call:Substring suffix,%fileroot%,%length%,0

Print | posted on Saturday, January 30, 2010 2:26 PM | Filed Under [ General Geekiness ]

Feedback

Gravatar

# re: Useful DOS Batch functions: Substring() and Length()

Why not move to Windows PowerShell instead?
1/30/2010 6:10 PM | Bill Sorensen
Gravatar

# re: Useful DOS Batch functions: Substring() and Length()

Speed mostly. These batch files are run VERY frequently and onDemand so they need to be FAAST! My experience is for simplier tasks, Batch files are faster than Powershell.
1/31/2010 9:11 AM | wtfChris
Gravatar

# re: Useful DOS Batch functions: Substring() and Length()

I got a post build event that I can run from the command line just fine but when I run it as a post build event I get exited with code 1. The command is : robocopy C:\ws\MA\erw\Source\NextGen\Services\AcctMgmtService C:\ws\MA\erw\Source\NextGen\Services\AcctMgmtService\LxNx.OLS.PM.PurgingService. A friend of mine from Content Security told me to ask you for help, so do you have any ideas on how to fix this?
5/19/2011 1:38 PM | MikeS
Gravatar

# re: Useful DOS Batch functions: Substring() and Length()

The stringlength function works fine as long as %2 does not contain spaces. If so you would need to quote the string and use %~2. The method I like uses %* and rather than passing the length variable return the length in errorlevel and nothing need be quoted unless you wish to count quotes in the string:

:Strlen Returns length of string in errorlevel
@echo off&setlocal enabledelayedexpansion&set "#=%*"
if not defined # exit /b 0
for /l %%a in (1,1,0xff) do (
set "#=!#:~1!"&if not defined # exit/b %%a)
7/17/2011 10:02 AM | Mark Carlson
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: