Page 1 of 1
strLen Subroutine in Windows 2000 doesn't work
Posted: 06 Jan 2010 13:13
by sweener
When I use the strLen Function provided on this site, I always get 0 for a value for my string length... and I AM providing a string. It seems the len variable doesn't ever change from 0 through each bitwise shift step. Any suggestions?
tried something on two different machines. One has Windows Server 2000 and the other has XP.
To narrow the problem down I did this:
Code: Select all
SET /A len=0 ==> output '0' for both systems
SET /A "n=1<<10" ==> output 1024 for both systems
SET /A "n>>=1, len|=n" ==> Output 0 for WS 2000 and 512 for XP
Both variables ( %n% and %len% ) for XP are 512 (makes sense, 0 or'd with anything gives the result of the anything in question) but for WS2000 the %n% is 512 but %len% is 0.
Is there some other operator set that needs be used for Windows Server?
Posted: 16 Jan 2010 08:51
by alan_b
"SET /A ..." is not understood by ancient Command.com
"SET /A ..." is understood by CMD.EXE
Server 2000 and XP have different NTFS systems - see
http://en.wikipedia.org/wiki/NTFS
Is it possible that Server 2000 and XP also have different CMD.EXE,
and both systems understand "SET /A ...", but in different ways ?
Alan
Posted: 16 Jan 2010 11:14
by sweener
That is what appears to be the case. I have only had issues with bitwise operations using SET /A.
Is it possible that Server 2000 and XP also have different CMD.EXE,
and both systems understand "SET /A ...", but in different ways ?
If the two work differently, I'll have to find a work around to OR the value of 'n' to 'len'
Thanks, I'll write back if I find a fix to my problem
Posted: 16 Jan 2010 21:10
by admin
May be Command Extensions are disabled?
You could put this at the beginning of your batch and see if it solves the problem:
Posted: 18 Jan 2010 08:00
by sweener
SETLOCAL ENABLEEXTENSIONS was already in my script. It may just be a variance between DOS 5.0 (Server 2k) and DOS 5.1 (Win XP). Any ideas?
Posted: 18 Jan 2010 13:35
by avery_larry
From a very quick test -- it seems that the or fails if the FIRST number is a 0.
This works:
Code: Select all
set /a len=0
set /a "n=1<<10"
set /a "n>>=1, len=n|len"
Posted: 18 Jan 2010 21:16
by DosItHelp
Well not sure whats going on. Below an alternative implementation you can try.
Code: Select all
@echo off
set "str=This string is 33 characters long"
call:strlen str
pause&goto:eof
:strLen string len -- returns the length of a string via binary search, maximum length 1023
:: -- string [in] - variable name containing the string being measured for length
:: -- len [out] - variable to be used to return the string length
:$created 20081122 :$changed 20081122 :$categories StringOperation
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"&rem keep the A in front it avoids trouble with empty string
set len=0
for /L %%N in (0,1,512) do (
if "!str:~%%N!" NEQ "" set len=%%N
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET "%~2=%len%") else (echo.%len%)
)
EXIT /b
DosItHelp?
Posted: 19 Jan 2010 07:59
by sweener
Why didn't I think of that? I guess because I was too hung up on making the bitwise work. But looking at it, it looks logical, although I'll bet it is not as fast as the bitwise operation but I would only need to check smaller stings (up to 40 - 50 chars at most).
Thanks!