What Was I Thinking?

Follies & Foils of .NET Development

  Home  |   Contact  |   Syndication    |   Login
  74 Posts | 0 Stories | 192 Comments | 0 Trackbacks

News

Archives

Post Categories

Check These Out

Gurus

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Saturday, January 30, 2010 2:26 PM

Feedback

# re: Useful DOS Batch functions: Substring() and Length() 1/30/2010 6:10 PM Bill Sorensen
Why not move to Windows PowerShell instead?

# re: Useful DOS Batch functions: Substring() and Length() 1/31/2010 9:11 AM wtfChris
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.

# re: Useful DOS Batch functions: Substring() and Length() 5/19/2011 1:38 PM MikeS
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?

# re: Useful DOS Batch functions: Substring() and Length() 7/17/2011 10:02 AM Mark Carlson
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)


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: