1.

Solve : Use an Environment Variable inside another??

Answer»

Win XP Home.

Is it possible to use an environment variable inside ANOTHER environment variable? I'm trying to find the length of an environment variable (just experimenting) without success so far. I can do this by creating a file containing the env var then extracting the file (variable) length but is there another way using batch scripting?

So far my code attempt is:

Code: [Select]@echo off
setlocal enabledelayedexpansion
cls

set var=variable#
set pos=1
set max=1

:start
if "%var:~%pos%,1%"=="#" (
echo Varlen=%max% & exit /b
) ELSE (
set /a max +=1
)
set /a pos +=1
goto start

But this "%var:~%pos%,1%" doesn't seem to extract the value of %pos%, using pos itself. Tried escaping the % but that doesn't seem to work either.

ThanksYou don't need setlocal in this context and I streamlined some of the other code. I took it on faith that # was a delimiter and not part of the variable.

Code: [Select]@echo off
set var=variable#
set pos=0

:start
call set chr=%%var:~%pos%,1%%
if %chr%==# echo Varlen = %pos% & exit /b
set /a pos+=1
goto start

Good luck.

Thanks a bunch. My claim to fame can be that I got at least 4 lines correct

Now all I have to do is understand the Call COMMAND line you posted.

Seems I've got into a bad habit of always using setlocal allowdelayedexpansion in every batch script

Thanks again..Quote from: Sidewinder on January 16, 2009, 06:49:01 PM

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 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.

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 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.

Hmmm - there's a none too subtle hint


Discussion

No Comment Found