| 1. |
Solve : Use an Environment Variable inside another?? |
|
Answer» Win XP Home. I took it on faith that # was a delimiter and not part of the variable. ... you kind of have to do that, don't you? So you can't measure the length of a string that contains the delimiter character. Set a counter to 0. Starting at the first character, create a substring which is composed of the CHARACTERS from the counter position to either the end of the string or 8192 characters along, whichever is the lower. (8192 is the max string length in XP or later cmd. Win2K & NT4 is 2047). Add 1 to the counter. Repeat until the substring is the empty string ("") which MEANS you have run out of characters, i.e. you have reached the end of the string. The counter value is the string length. You have to use call to set the substring because you need to do the variable substitution in a child process. Although this method does not use an added delimiter character which must be absent from the supplied string, it will still break if the supplied string contains control characters especially &, but then so will the previous code. That's a fact of batch life. Code: [Select]@echo off set string=Seems I've got into a bad habit set stringlen=0 :loop call set sstring=%%string:~%stringlen%,8192%% if "%sstring%"=="" goto done set /a stringlen+=1 goto loop :done echo string length=%stringlen% Thanks for that Dias - nice explanation - more for me to puzzle over wow. getting the length of a string is a troublesome task in batch.Quote from: BC_Programmer on January 17, 2009, 02:45:11 AM wow. getting the length of a string is a troublesome task in batch. When you start hitting such problems in batch, often it's a sign that you need to be using another language.Quote from: Dias de verano on January 17, 2009, 04:39:15 AM Quote from: BC_Programmer on January 17, 2009, 02:45:11 AMwow. getting the length of a string is a troublesome task in batch. Like VBScript. Len() Function to the rescue! Quote from: BC_Programmer on January 17, 2009, 04:59:16 AM Like VBScript. Len() Function to the rescue! exactly Code: [Select]@echo off set string=Mary had a little lamb echo Wscript.echo (Len(WScript.Arguments(0)))>stringlength.vbs for /f %%L in ('cscript //nologo stringlength.vbs "%string%"') do set /a slen=%%L del stringlength.vbs echo String length is %slen% Quote from: Dias de verano on January 17, 2009, 04:39:15 AM Quote from: BC_Programmer on January 17, 2009, 02:45:11 AMwow. getting the length of a string is a troublesome task in batch. Hmmm - there's a none too subtle hint |
|