Sounak@9434 wrote:I'm not surprised with the Aacini's result. His scripts are usually the fastest I've seen. I even had not found any use of his discovery until I read his script. I'm going to update my alignment utility(again) with his script.
@Sounak@9434, please note that
if a character is a digit or an operator the SET /A command process it, so such characters (and also others that may cause errors in the arithmetic expression) must be replaced before use this method. I am still doing tests to check which characters may cause errors in SET /A command, so this trick can NOT be used as a general method to get string lengths (yet).
ShadowThief wrote:Yeah, I'm not even remotely surprised that my script came in last; it has to make a temporary file for every single line. Heck, I'm surprised that it managed to finish in 8 seconds!
After I read this phrase I thought that this method would be faster if the lengths of
all lines in the file could be obtained in just one operation. Then, I realized that the /O switch in FINDSTR command allows to get precisely this result!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "file=poem.txt"
for /F "skip=4 tokens=2" %%a in ('mode con') do set /A "cols=%%a-1" & goto continue
:continue
set "spaces="
for /L %%i in (0,2,%cols%) do set "spaces=!spaces! "
set "str="
set "last=0"
for /F "tokens=1* delims=:" %%a in ('findstr /O "^" poem.txt') do (
set /A "indent=(cols-(%%a-last-2))/2, last=%%a"
if !indent! lss 0 set "indent=0"
if defined str (
for /F %%n in ("!indent!") do set "str=!spaces:~0,%%n!!str!"
echo !str:~0,%cols%!
)
set "str=%%b"
)
rem Last line
for %%a in (poem.txt) do set /A "indent=(cols-(%%~Za-last-2))/2"
if %indent% lss 0 set "indent=0"
set "str=!spaces:~0,%indent%!!str!"
echo !str:~0,%cols%!
I wonder how fast is this method when compared vs. the other ones...
Antonio