strLen Subroutine in Windows 2000 doesn't work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

strLen Subroutine in Windows 2000 doesn't work

#1 Post by sweener » 06 Jan 2010 13:13

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?
Last edited by sweener on 07 Jan 2010 14:33, edited 1 time in total.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

#2 Post by alan_b » 16 Jan 2010 08:51

"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

sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

#3 Post by sweener » 16 Jan 2010 11:14

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

admin
Site Admin
Posts: 134
Joined: 31 Dec 1969 18:00
Location: US

#4 Post by admin » 16 Jan 2010 21:10

May be Command Extensions are disabled?
You could put this at the beginning of your batch and see if it solves the problem:

Code: Select all

SETLOCAL ENABLEEXTENSIONS

sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

#5 Post by sweener » 18 Jan 2010 08:00

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?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#6 Post by avery_larry » 18 Jan 2010 13:35

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"

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#7 Post by DosItHelp » 18 Jan 2010 21:16

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?

sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

#8 Post by sweener » 19 Jan 2010 07:59

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!

Post Reply