Aligning echo command output in batch script

I wrote a simple batch file to analyze the log file and print the necessary data on the screen. Everything works for me, except that my output does not align properly, as I would like. Here is my batch script:

@echo off
setlocal
echo Name               Reg No.
echo -------------------------------------
for /f "tokens=1-3 delims=' " %%A in ('findstr "Registered" registration.log') do (
     echo %%B               %%C
)
endlocal

Output:

Name               Reg No.
-------------------------------------
Russel McDonald               28068906
Pete               23985688
David Antony               87681747
Jaques               71979798
Jayson Burgers               21343854
Allison Jameson               87446435

But I want the result to be:

Name               Reg No.
-------------------------------------
Russel McDonald    28068906
Pete               23985688
David Antony       87681747
Jaques             71979798
Jayson Burgers     21343854
Allison Jameson    87446435

How can I do that?

+4
source share
1 answer

This batch code aligns the registration number on each output line.

@echo off
setlocal EnableDelayedExpansion
echo Name               Reg No.
echo -------------------------------------
for /f "tokens=2,3 delims=' " %%A in ('%SystemRoot%\System32\findstr.exe "Registered" registration.log') do (
    set "RegistrationName=%%A                   "
    echo !RegistrationName:~0,19!%%B
)
endlocal

I believe the file registration.logis a tab delimited CSV file, and therefore the character after delims='is the character of the horizontal tab.

, , 19 , 19 , .

findstr:

@echo off
setlocal EnableDelayedExpansion
echo Name               Reg No.
echo -------------------------------------
for /f "tokens=1-3 delims=' " %%A in (registration.log) do (
    if /I "%%A" == "Registered" (
        set "RegistrationName=%%B                   "
        echo !RegistrationName:~0,19!%%C
    )
)
endlocal

, , , , .

  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
+4

Source: https://habr.com/ru/post/1618559/


All Articles