I had also tried using the macros technique to get string length (mentioned on this forum); but, I had even more problems with that. All I want, is the fastest possible way to get the string lenghth of %print% variable IN the run-log.cmd script.
If I put the stringlength counter in a separate test script (or even macros stringlength technique, they work perfectly. I'm hoping someone much more experienced than me would know how I can make stringlength counting work.
run-log.cmd
Code: Select all
@ECHO OFF
setlocal enableDelayedExpansion
set "log=stunnel.log"
set "logx=stunnelx.log"
(
for /f "tokens=1-3* delims= " %%a in ('findstr /V /I /C:"127.0.0" /C:"192.168" /C:"byte" /C:"WSAECONNRESET" /C:"Peer suddenly disconnected" "%log%"') do (
set "dateValue=%%a"
set "dateValue=!dateValue:~5,2!/!dateValue:~8,2!/!dateValue:~2,2!"
for /F "tokens=1* delims=:" %%e in ("%%b") do (
set /A "timeValue=%%e"
if !timeValue! == 0 (
set "timeValue=12:%%fam"
) else if !timeValue! Lss 12 (
set "timeValue=%%e:%%fam"
) else if !timeValue! == 12 (
set "timeValue=12:%%fpm"
) else (
set /A "timeValue-=12"
set "timeValue=0!timeValue!"
set "timeValue=!timeValue:~-2!:%%fpm"
)
)
set "info=%%d"
set "ipPort="
for %%e in (!info!) do set "ipPort=%%e"
for /F "tokens=1,2 delims=:" %%f in ("!ipPort!") do (
if not defined IP[%%f] for /f "tokens=2 delims= " %%h in ('nslookup %%f 2^> nul ^| findstr /I /C:"Name" ') do set "IP[%%f]= %%h"
if not defined IP[%%f] set "IP[%%f]= "
set "host=!IP[%%f]:~1!"
if defined host (
set "info=!info: %%f:%%g=!: !host! [%%f]"
) else (
set "info=!info: %%f:%%g=!: [%%f]"
)
)
set "location=- United States of America"
set "print=%dateValue% %timeValue% %info% %location%"
::: If I add the below lines, the batch file produces, "The syntax of the command is incorrect." error.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
call :strlen result print
set /a charlength=%result%
if !charlength! GTR 190 goto :endcounter
set /a padding = 190 - %charlength%
set counter=0
:counter
if !counter!==!padding! goto :endcounter
set /a counter=%counter%+1
set "location= %location%"
goto counter
:endcounter
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo(!dateValue! !timeValue! !info! !location!
)
) > "%logx%"
endlocal
goto :eof
:strlen
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
stunnel.log
Code: Select all
2013.07.23 10:16:00 LOG5[10152:15136]: Service [stunnel-sslh] connected remote server from 24.12.152.129:58773
2013.07.23 10:16:00 LOG3[10152:15136]: SSL_read: Connection reset by peer (WSAECONNRESET) (10054)
2013.07.23 10:16:00 LOG5[10152:832]: Connection reset: 272 byte(s) sent to SSL, 96 byte(s) sent to socket
2013.07.23 10:17:53 LOG5[10152:4000]: Service [stunnel-sslh] accepted connection from 71.194.51.232:5535
2013.07.23 10:17:53 LOG5[10152:16008]: connect_blocking: connected 24.12.152.129:7777
2013.07.23 10:17:53 LOG5[10152:4000]: Service [stunnel-sslh] connected remote server from 24.12.152.129:58799
2013.07.23 10:17:53 LOG5[10152:13212]: Service [stunnel-sslh] accepted connection from 71.194.51.232:5508
2013.07.23 10:17:53 LOG5[10152:3348]: Service [stunnel-sslh] accepted connection from 71.194.51.232:5509
2013.07.23 10:17:53 LOG5[10152:2884]: Service [stunnel-sslh] accepted connection from 71.194.51.232:5519
stringlen-test.cmd (works perfectly outside of the above script)
Code: Select all
@ECHO OFF
setlocal EnableDelayedExpansion
set "timeValue=11:44:32pm "
set "dateValue=07/23/13 "
set "info=Service [stunnel-sslh] accepted connection from: r74-194-126-35.jsbrcmta02.jnboar.lr.dh.suddenlink.net [74.194.126.35]"
set "location=- United States of America "
set "print=%dateValue% %timeValue% %info% %location%"
call :strlen result print
set /a charlength=%result%
if !charlength! GTR 190 goto :endcounter
set /a padding = 190 - %charlength%
set counter=0
:counter
if !counter!==!padding! goto :endcounter
set /a counter=%counter%+1
set "location= %location%"
goto counter
:endcounter
echo "%dateValue% %timeValue% %info% %location%"
goto :end
:strlen
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
:end
"High-performance" stringlength counter using macros (only seems to work outside of the my batch file):
Code: Select all
@echo off
call :loadMacros
set "myVar=abcdefg"
%$strlen% result myVar
echo %result%
exit /b
:loadMacros
set LF=^
::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
:::: StrLen pResult pString
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
set "str=A!%%~2!"%\n%
set "len=0"%\n%
for /l %%A in (12,-1,0) do (%\n%
set /a "len|=1<<%%A"%\n%
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
)%\n%
for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,
exit /