Setup a batch macro by calling a different file
Posted: 07 Mar 2017 02:31
Hello again everyone.
This time I'm working with a batch macro.
I'm working on a alignment macro. Here is the script.
But according to my tests so many amounts of for loop used in the script has slowed down the script a lot.
I have a different approach with an additional batch file that uses string-length macro.
Here is the script.
This code is pretty much twice as fast but it needs to set up the "@strlen" file each time which I want to avoid.
Adding an endlocal is not possible because the macro needs delayed expansion as disabled.
So people, any Idea about how to permanently setup the '@strlen' macro so that while calling the script from a separate file it would not need to set up the value of '@strlen'?
This time I'm working with a batch macro.
I'm working on a alignment macro. Here is the script.
Code: Select all
@echo off
setlocal disabledelayedexpansion
set ^"LF=^
%= This creates a variable containing a single linefeed (0x0A) character =%
^"
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set @align=for %%. in (1 2) do if %%.==2 (%\n%
setlocal%\n%
for %%1 in (!argv!) do (%\n%
if not defined _align (if "%%1"=="/s" (set "_line=") ELSE (set "_align=%%1")) ELSE (%\n%
if not defined _text (set "_text=%%~1") ELSE (%\n%
if not defined _var if "%%1" neq "" set "_var=%%1"%\n%
)%\n%
)%\n%
)%\n%
if not defined _line for /f "skip=4 tokens=2 delims= " %%a in ('mode con') do if not defined _line set "_line=%%a"%\n%
set "_s=A!_text!"%\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%
set /a "_space=!_line!-!_len!"%\n%
if "!_align!"=="/m" set /a _space/=2%\n%
if "!_align!"=="/l" set _space=0%\n%
if "!_align!"=="/r" set /a _space-=1%\n%
set "_bsp="%\n%
for /l %%A in (1,1,!_space!) do set "_bsp=!_bsp! "%\n%
if not defined _var for %%V in ("!_bsp!!_text!") do (endlocal^&endlocal^&endlocal^&echo %%~V) ELSE for %%Z in ("!_var!") do for %%V in ("!_bsp!!_text!") do (endlocal^&endlocal^&endlocal^&set %%~Z=%%~V)%\n%
) else setlocal enabledelayedexpansion^&setlocal^&set argv=
%@align% /m "Hello world"
%@align% /r "Goodbye Cruel World" _out
echo(%_out%
But according to my tests so many amounts of for loop used in the script has slowed down the script a lot.
I have a different approach with an additional batch file that uses string-length macro.
Here is the script.
Code: Select all
@echo off
::Align.bat
::Show help check
if "%~1"=="" type help.txt&exit /b
if "%~1"=="/?" type help.txt&exit /b
::Line reset check
if "%~1"=="/s" set "_line="&shift
::Getting Data
set "_align=%1"
set "_text=%~2"
if not "%~3"=="" set "_var=%~3"
::Getting window length if not defined
if not defined _line for /f "skip=4 tokens=2 delims= " %%a in ('mode con') do if not defined _line set "_line=%%a"
::Setting up variables if not done already
if not defined @strlen call :macro
::Getting string length
%@strlen% _text _len
::Mathematical part
set /a "_space=%_line%-%_len%"
if "%_align%"=="/m" set /a _space/=2
if "%_align%"=="/l" set _space=0
if "%_align%"=="/r" set /a _space=%_space%-1
::Setting up spaces
setlocal enabledelayedexpansion
for /l %%a in (1,1,%_space%) do set "_bsp=!_bsp! "
::Printing aligned text or setting it to a variable for further use
if not defined _var (echo(%_bsp%%_text%) ELSE (endlocal&&endlocal&&set %_var%=%_bsp%%_text%)
exit /b
:macro
::Creating string length macro
::Created at dostips.com
set ^"LF=^
%= This creates a variable containing a single linefeed (0x0A) character =%
^"
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set @strLen=for %%. in (1 2) do if %%.==2 (%\n%
for /f "tokens=1,2 delims=, " %%1 in ("!argv!") do ( endlocal%\n%
set "s=A!%%~1!"%\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^&if "%%~2" neq "" (set "%%~2=%%V") else echo %%V%\n%
)%\n%
) else setlocal enableDelayedExpansion^&setlocal^&set argv=,
This code is pretty much twice as fast but it needs to set up the "@strlen" file each time which I want to avoid.
Adding an endlocal is not possible because the macro needs delayed expansion as disabled.
So people, any Idea about how to permanently setup the '@strlen' macro so that while calling the script from a separate file it would not need to set up the value of '@strlen'?