Code: Select all
@ECHO OFF &SETLocal DISABLEDelayedExpansion
SET LF=^
SET ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
SET "testString=this has a length of 23"
SET strLen=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
(%\n%
FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
SET "s=!%%~b!#"%\n%
SET "len=0"%\n%
FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (%\n%
if "!s:~%%P,1!" NEQ "" (%\n%
SET /a "len+=%%P"%\n%
SET "s=!s:~%%P!"%\n%
)%\n%
)%\n%
)^&FOR %%v IN (!len!) DO ENDLocal ^&SET "%%a=%%v"%\n%
)%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& SET argv=
SET "x="
SET "testString=this has a length of 23"
%strlen% x testString
@ECHO strlen result = %x%
SET macro1=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
(@ECHO --- %%a=!%%a! - %%b=!%%b!)%\n%
)^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=
SET str1=test1
SET str2=test2
%macro1% str1 str2
@ECHO(
SET macro2=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
(@ECHO --- %%a=!%%a! - %%b=!%%b!)%\n%
!!strlen!! x %%a %\n%
)^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=
%macro2% str1 str2
@ECHO(
SET macro3=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
(@ECHO --- %%a=!%%a! - %%b=!%%b!)%\n%
%%strlen%% x %%a %\n%
)^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=
%macro3% str1 str2
endlocal
Code: Select all
strlen result = 23
--- str1=test1 - str2=test2
--- str1=test1 - str2=test2
'FOR' is not recognized as an internal or external command,
operable program or batch file.
--- str1=test1 - str2=test2
'%strlen%' is not recognized as an internal or external command,
operable program or batch file.