Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off
- :# Macro escaped for definition with delayed expansion enabled.
- Setlocal EnableDelayedExpansion
- ===================================================================:# [StrLen]
- :# %[StrLen]% Returnvar Var
- :# Source: https://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=417
- :# Returns two results:
- :# Returnvar=ActualLength
- :# ReturnVar[0i]=ActualLenght-1
- :# use Zero indexed suffix '[0i]' when iterating over a string from first character to last IE:
- :# For /L %%i in (0 1 !Returnvar[0i]!)Do For /F "Delims=" %%c in ('Set /a %%i + 1')Do Echo(Character %%c = !Variable:~%%i,1!
- (Set \n=^^^
- %= \n macro newline variable - Do not Modify =%)
- Set [StrLen]=for /L %%n in (1 1 2) do if %%n==2 (%\n%
- For /F "tokens=1,* delims=, " %%G in ("^!argv^!")Do (%\n%
- Set "[StrLen]tmpStr=^!%%~H^!"%\n%
- Set "[StrLen]Len=0"%\n%
- Set "[StrLen]Result=%%~G"%\n%
- If not "^![StrLen]tmpStr^!" == "" For %%N IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1)Do (%\n%
- If not "^![StrLen]tmpStr:~%%N,1^!" == "" (%\n%
- Set /A "[StrLen]Len += %%N"%\n%
- Set "[StrLen]tmpStr=^![StrLen]tmpStr:~%%N^!"%\n%
- ))%\n%
- )%\n%
- Set /A "^![StrLen]Result^!=^![StrLen]Len^! + 1,^![StrLen]Result^![0i]= ^![StrLen]Len^!"%\n%
- If "^![StrLen]tmpStr^!" == "" Set /A "^![StrLen]Result^!=0,^![StrLen]Result^![0i]=0"%\n%
- )Else set argv=,
- ===============================================================:# End [StrLen]
- :# Usage examples.
- :# Example one: test using parameters from the command line using parameters
- If not "%~2" == "" (
- %[strLen]% "%~1" "%~2"
- If /I "%~3" == "/O" Echo(%~1=!%~1!
- For /F "tokens=1,2 Delims=;" %%G in ("!%~1!;!%~1[0i]!")Do Endlocal & (
- Set /A "%~1=%%~G,%~1[0i]=%%~H"
- )
- Exit /B 0
- )Else if not "%~1" == "" (
- Echo(%~n0 ReturnVar VariableName
- Exit /b 0
- )
- :# Example two: Example use with computername variable.
- %[Strlen]% CN Computername
- Echo(Computername "%Computername%" contains !CN! characters:
- For /L %%i in (0 1 !CN[0i]!)Do For /F "Delims=" %%c in ('Set /a %%i + 1')Do Echo(Character %%c = !ComputerName:~%%i,1!
Add Comment
Please, Sign In to add comment