1.

Solve : Determining the length of an environment variable?

Answer»

I am TRYING to determine the LENGTH of an environment variable. I tried this:

Code: [Select]SET IN=%1
FOR /L %%I IN (1000,-1,1) IF "%IN:~!I!,1%"=="" SET LEN=!I!
echo "Length="%LEN%
with delayed environment substritution, but it did not work.

Thanks in advance for any help you may provide.Code: [Select]@echo off
>temp.file echo "%~1"
FOR %%a IN (temp.file) do set /a len=%%~za-4
echo "Length=%LEN%"
del temp.file
pauseUsing your technique:

Code: [Select]@echo off
setlocal EnableDelayedExpansion
SET var=%1
FOR /L %%a IN (1,1,1000) do (IF "!var:~%%a,1!"=="" SET LEN=%%a&goto :next)
:next
echo "Length=%LEN%"
pauseOr...

Code: [Select]@echo off
setlocal enabledelayedexpansion

set Var=%*

:: Note: %* is used if the variable may contain spaces.

:loop
set /A cnt+=1
if not "!Var:~%cnt%,1!"=="" goto loop

echo.Var contains %cnt% characters.
Or...

Code: [Select]@echo off
echo wscript.echo len(wscript.arguments(0)) > showlen.vbs
for /f "delims=" %%A in ('cscript //nologo showlen.vbs "%*"') do set vlen=%%A
del showlen.vbs
echo Length: %vlen% characters
One line, no vbs (as LONG as you can write in the same FOLDER)
Code: [Select]ECHO %envar%>x & FOR %%A IN (x) DO SET /A strlength=%%~zA - 2 & del x



Discussion

No Comment Found