Align text in output file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Align text in output file

#1 Post by Matt Williamson » 26 May 2016 06:54

I have the following code that Foxidrive helped me with awhile ago and I want to have the output file right/left aligned. I have found 2 examples so far that seem to fit the bill but I can't figure out how to apply them to my code. Here is my code.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "testing=0"

if "%testing%" EQU "1" (
   set "xmlpath=F:\Scripts\batch\EOD testing"
) ELSE (
   set "xmlpath=\\server1.prod.mtb.com\developer\inetpub\Ftproot\XMLs"
)
if exist "%temp%\filterEOD?.txt" del "%temp%\filterEOD?.txt"
  If /i "%userdomain%" equ "DEV" (
     set "dom=PROD"
      set /p useris=Enter your production user name:
     set /p pw=Enter your production password:
      set Map1=Net use "%xmlpath%" !pw! /USER:!dom!\!useris! /PERSISTENT:NO
      !Map1!&pushd "%xmlpath%"
   ) ELSE (
      pushd "%xmlpath%"
   )
dir /o:-d /a-d "%xmlpath%\*.xml" |find "/" >"%temp%\filterEOD.txt"

set "found=0"
set "files=0"
(
for /f "usebackq tokens=1,2,3,4,*" %%a in ("%temp%\filterEOD.txt") do (
      set "f3=%%e"
      echo %%e %%a %%b %%c
      if !found! GTR 0 set /a files+=1
      IF /i "!f3:~0,3!" EQU "EOD" IF !found! GTR 0 (
            set /a files-=1
            echo(
            echo located !files! files between EOD files in total
            echo(
            goto :out
      )
      IF /i "!f3:~0,3!" EQU "EOD" set /a found+=1 && Echo( && ECHO(
   )
  if !found! LSS 2 (
     echo( & echo(
     echo Only !found! EOD file found
     echo !files! files were found from the first EOD file
 )
)>"%temp%\filterEOD2.txt"
:out
start "Filter EOD Files" "%temp%\filterEOD2.txt"
popd
del "%temp%\filterEOD.txt"
endlocal
exit /b


Here is the output currently:

Code: Select all

SB9116930001_ACCTV21_20160526_PCS_110_108880059V.xml 05/25/2016 06:01 PM
SB1836430001_ACCTV21_20160526_PCS_1_1088800594.xml 05/25/2016 06:01 PM
SB1836430001_BALMIS_20160526_PCS_1_1088800595.xml 05/25/2016 06:01 PM
EODMESS_20160525_1088800290.xml 05/25/2016 05:45 PM
 

ID1799480001_BALMIS_20160525_NAC_12_108880025S.xml 05/25/2016 05:45 PM
ID1799490001_BALMIS_20160525_NAC_12_108880025G.xml 05/25/2016 05:45 PM
ID1837390001_BALMIS_20160525_NAC_8_1088800254.xml 05/25/2016 05:45 PM
TA1911120001_BALMIS_20160525_TRC_2_108880013Z.xml 05/25/2016 05:44 PM
TA1904570001_BALMIS_20160525_TRC_2_108880013I.xml 05/25/2016 05:44 PM
ID1929030001_BALMIS_20160525_TRC_2_1088800131.xml 05/25/2016 05:44 PM
OD1933020001_BALMIS_20160525_TRC_1_108880011K.xml 05/25/2016 05:44 PM
SB1508550001_BALMIS_20160525_TRC_5_1088800113.xml 05/25/2016 05:44 PM
SB9106670001_BALMIS_20160525_TRC_13_108880008M.xml 05/25/2016 05:44 PM
SB1278560001_BALMIS_20160525_TRC_12_1088800085.xml 05/25/2016 05:44 PM
SB1277510001_BALMIS_20160525_TRC_10_108880006P.xml 05/25/2016 05:44 PM
SB9128630001_BALMIS_20160525_TRC_14_1088800068.xml 05/25/2016 05:44 PM
TA1918240001_BALMIS_20160525_TRC_1_108880005S.xml 05/25/2016 05:44 PM
EX8044800001_BALMIS_20160525_TRC_12_108880005B.xml 05/25/2016 05:43 PM
SB9109620001_BALMIS_20160525_TRC_10_108880003V.xml 05/25/2016 05:43 PM
SB9109630001_BALMIS_20160525_TRC_10_108880003E.xml 05/25/2016 05:43 PM
CI5426830001_BALMIS_20160525_DEA_2_1088799973.xml 05/25/2016 05:42 PM
CI5433480001_BALMIS_20160525_DEA_1_108879996R.xml 05/25/2016 05:42 PM
EX1895670001_BALMIS_20160525_DEA_1_108879996F.xml 05/25/2016 05:42 PM
SB1481220001_BALMIS_20160525_DEA_1_1088799964.xml 05/25/2016 05:42 PM
SB1481220001_ACCTV21_20160525_DEA_1_1088799963.xml 05/25/2016 05:42 PM
EX1920880001_BALMIS_20160525_DEA_1_108879995R.xml 05/25/2016 05:42 PM
BS1829130001_BALMIS_20160525_EXP_1_108879995E.xml 05/25/2016 05:42 PM
SB1849030001_ACCTV21_20160525_EXT_1_1088799952.xml 05/25/2016 05:42 PM
EODMESS_20160525_1088799922.xml 05/25/2016 05:42 PM

located 24 files between EOD files in total


I want to left justify the first column and right justify the date and time columns.

Here are the 2 code examples that I'm working with: Example1 and Example2

I just can't seem to apply either to my code.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Align text in output file

#2 Post by Squashman » 26 May 2016 07:33

I use this.
http://www.dostips.com/DtCodeCmdLib.php#Function.Format

But I know Dave came up with a better and faster way to do this. I know it was a pretty recent post but I can't find it right now.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Align text in output file

#3 Post by Matt Williamson » 26 May 2016 08:28

The first example was Dave's code on SS64. I'm playing with it now to figure out how it works but I'm not making much headway.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Align text in output file

#4 Post by Squashman » 26 May 2016 08:56

Squashman wrote:But I know Dave came up with a better and faster way to do this. I know it was a pretty recent post but I can't find it right now.

Matt Williamson wrote:The first example was Dave's code on SS64. I'm playing with it now to figure out how it works but I'm not making much headway.


You and I must have a different concept of time. The code on SS64 was from 2012. When I say recent, I mean it was recent on DosTips within the past few months.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Align text in output file

#5 Post by thefeduke » 27 May 2016 00:07

Matt Williamson wrote:The first example was Dave's code on SS64. I'm playing with it now to figure out how it works but I'm not making much headway.
I have a couple of things to offer, based on your example2 and one reworked from a script that I just wrote this week. I adapted example2 to work on the directory file that you already create, "%temp%\filterEOD.txt", instead of your script output because it falls apart processing the summary. TextFormatRaw.bat :

Code: Select all

@ECHO Off &SetLOCAL enabledelayedexpansion

For /F "tokens=1-4* delims= " %%a In (%~1) Do (
  >"%temp%\%~n0.txt" echo %%e
  For /F "usebackq" %%I In ('%temp%\%~n0.txt') Do Set size=%%~zI
  If !maxSize! lss !size! Set /A maxSize=!size!
)

set "blanks=                                                           "
for /f "tokens=1-4*" %%a in (%~1) do (
    set "v1=%%e%blanks%"&set "v2=%blanks%%%a %%b %%c"
    call echo(!v1:~0,%maxsize%!!v2:~-19!
)

Exit /B
[color=#0040FF][/color]
To eliminate the detail records from "%temp%\filterEOD2.txt" either remove

Code: Select all

echo %%e %%a %%b %%c
or replace it with:

Code: Select all

      >>%temp%\FilterEod4.txt (echo(%%e [[Ali]] %%a %%b %%c)
in preparation for the second approach. Now add the following code between your ":out" label and the "popd"

Code: Select all

(Call TextFormatraw "%temp%\filterEOD.txt"
)>%temp%\FilterEod1.txt
Copy /Y "%temp%\filterEOD1.txt"+"%temp%\filterEOD3.txt" "%temp%\filterEOD3.txt"
start "Filter EOD Files" "%temp%\filterEOD3.txt"
The width of the area for the filename can be adjusted from 60 within TextFormatRaw.bat. Working from FilterEOD allows for spaces in the filename.

That should keep you for now. It is not very intrusive to your existing code. My other approach adjusts the right margin to the length of the longest filename but I'll delay that post until a less tired time.
Edit: Above code now self-adjusts margin, thanks to Jer in the next post.
John A.
Last edited by thefeduke on 28 May 2016 00:25, edited 2 times in total.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Align text in output file

#6 Post by Jer » 27 May 2016 10:33

This is my alternative solution to aligning the text left and right.
I am not an expert in batch coding, but just try to get the job done.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
Set "tmpfile=temp.txt"
Set /A maxSize=0

For /F "tokens=1* delims= " %%a In (source.txt) Do (
  >"%tmpfile%" echo %%a
  For /F "usebackq" %%I In ('%tmpfile%') Do Set size=%%~zI
  If !maxSize! lss !size! Set /A maxSize=!size!
)

If EXIST "%tmpfile%" DEL "%tmpfile%"

Set "padRight="
For /L %%n In (1,1,%maxSize%) Do Set "padRight= !padRight!"

If EXIST output.txt DEL output.txt

For /F "tokens=1-4 delims= " %%a In (source.txt) Do (
   Set "str=%%a %padRight%"
   Set "str=!str:~0,%maxSize%!"
   >>"output.txt" Echo !str! %%b %%c %%d
)
type output.txt
endlocal & exit /b

code edited 5/28/2016 - "!str2!" deleted-not used
Last edited by Jer on 28 May 2016 08:57, edited 1 time in total.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Align text in output file

#7 Post by thefeduke » 28 May 2016 01:07

I have updated the code in my last post, because I was so impressed with the method used by Jer to determine the lenght of the longest filename. I had used a technique that counted offsets from FINDSTR and another stepping backwards byte-by-byte, but this is far more appealing.

Thank you, Jer.

John A.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Align text in output file

#8 Post by Compo » 28 May 2016 08:30

Although this isn't a batch solution you may be able to utilise the formatting features of powershell through your .cmd file.
example.ps1 - (single line)

Code: Select all

ls .\Folder *.xml|FT -P @{L='File Name'; E={$_.Name}; A='Left'},@{L='Last Written'; E={$_.LastWriteTime}; A='Right'} -A
This example should give you the layout you're after from a listing of xml files in an adjacent folder.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Align text in output file

#9 Post by Jer » 28 May 2016 14:12

Getting line length by writing the line to file and checking the length returns the length +2.
Can someone knowledgeable about this post a comment.
Can you always count on the line length being 2 less than the file size of a one-line file?

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Align text in output file

#10 Post by thefeduke » 28 May 2016 14:32

Jer,
I did a hexdump of a file containing one character: the letter a. This shows a length of three in a DIR listing. The dump shows 61 0D 0A. I guess that is: <a> <CR> <LF>. The experts can tell you how much you can count on it. There are some ways of creating data without those.

John A.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Align text in output file

#11 Post by foxidrive » 29 May 2016 09:23

I had these squirelled away that deal with string length:

Code: Select all

@echo off
set name="test & 1234.txt"
(echo %name%)>tmp.txt
for %%a in (tmp.txt) do set /a num=%%~za-4
del tmp.txt
echo %num%

Code: Select all

@echo off
set "string=aaa bbb ccc"
call :a abc string
echo %abc%
pause
goto :EOF
:a
(   
    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
)

Code: Select all

@echo off

:: Space char. substitution below (I used a period) was necessary as when %* or %VAR% is expanded by
:: the for-in-do below, it concatenated any number of sequential spaces to 1 space destroying an
:: accurate count
set "VAR=%*"
set "var=%var: =.%"
for /f "delims=[] tokens=1" %%l in (
 'cmd /u /c echo/%VAR%^|find /v /n ""') do set /a CNT=%%l-3

if %CNT% EQU -3 set CNT=0
echo %CNT%
pause

Code: Select all

@echo off
set pre=.abc
for /f "tokens=1* delims=:" %%a in (
   '^(for %%i in ^("%pre%" .^) do @echo %%i^) ^| findstr /o .^| findstr /v /b 0') do set /a var=%%~a-5

set v
pause & goto :EOF

Code: Select all

@echo off
:: penpen 20140324
:: checks the length of %num% for string length if it is less than 9 char long

set "len=0123456789%num%"
set "len=%len:~-10,1%"


:: similarly, check if %ops% is less than 19 characters long

set "check=x%op1%"
set "check=%check:~-19,1%"
echo %check%

:: check will only be "x" if the length is less than 19.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Align text in output file

#12 Post by Jer » 29 May 2016 19:08

So for aligning a specifically structured list left & right,
doing the subtraction from the file size in the For loop leaves us with this:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
Set "tempsource=tmp.txt"
Set "linefile=line.tmp"
Set "output=output.txt"
Set /A maxSize=0

> "%tempsource%" (
echo SB9116930001_ACCTV21_20160526_PCS_110_108880059V.xml 05/25/2016 06:01 PM
echo SB1836430001_ACCTV21_20160526_PCS_1_1088800594.xml 05/25/2016 06:01 PM
echo SB1836430001_BALMIS_20160526_PCS_1_1088800595.xml 05/25/2016 06:01 PM
echo EODMESS_20160525_1088800290.xml 05/25/2016 05:45 PM
echo ID1799480001_BALMIS_20160525_NAC_12_108880025S.xml 05/25/2016 05:45 PM
echo ID1799480001_THIS_IS_THE_LONGEST_FILENAME_IN_THE_LIST.xml 05/25/2016 05:45 PM
echo ID1799490001_BALMIS_20160525_NAC_12_108880025G.xml 05/25/2016 05:45 PM
echo SB1849030001_SHORTEST.xml 05/25/2016 05:42 PM
echo EODMESS_20160525_1088799922.xml 05/25/2016 05:42 PM
)

For /F "tokens=1* delims= " %%a In (%tempsource%) Do (
  >"%linefile%" echo %%a
  For /F "usebackq" %%I In ('%linefile%') Do Set /a size=%%~zI-2
  If !maxSize! lss !size! Set /A maxSize=!size!
)

Set "padRight="
For /L %%n In (1,1,%maxSize%) Do Set "padRight= !padRight!"

If EXIST "%output%" DEL "%output%"
For /F "tokens=1-4 delims= " %%a In (%tempsource%) Do (
   Set "str=%%a %padRight%"
   Set "str=!str:~0,%maxSize%!"
   >>"%output%" Echo !str! %%b %%c %%d
)
If EXIST "%linefile%" DEL "%linefile%"
If EXIST "%tempsource%" DEL "%tempsource%"

type %output%
endlocal & exit /b


I was going to include the output but apparently Quote and Code and List functions
use a proportional font and don't show the true formatting in the DOSTIPS input form.
Jerry

edit: Here is the formatted output. I did not post originally because the code block does not
show the formatting until posted. Now I know :wink:

Code: Select all

SB9116930001_ACCTV21_20160526_PCS_110_108880059V.xml      05/25/2016 06:01 PM
SB1836430001_ACCTV21_20160526_PCS_1_1088800594.xml        05/25/2016 06:01 PM
SB1836430001_BALMIS_20160526_PCS_1_1088800595.xml         05/25/2016 06:01 PM
EODMESS_20160525_1088800290.xml                           05/25/2016 05:45 PM
ID1799480001_BALMIS_20160525_NAC_12_108880025S.xml        05/25/2016 05:45 PM
ID1799480001_THIS_IS_THE_LONGEST_FILENAME_IN_THE_LIST.xml 05/25/2016 05:45 PM
ID1799490001_BALMIS_20160525_NAC_12_108880025G.xml        05/25/2016 05:45 PM
SB1849030001_SHORTEST.xml                                 05/25/2016 05:42 PM
EODMESS_20160525_1088799922.xml                           05/25/2016 05:42 PM
Last edited by Jer on 01 Jun 2016 10:14, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Align text in output file

#13 Post by foxidrive » 30 May 2016 00:02

You can put the output in another code block, Jerry. It preserves the formatting also.
I'm not sure why it didn't work for you.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Align text in output file

#14 Post by Matt Williamson » 31 May 2016 12:09

Thank you to everyone who responded. Much appreciated.

Post Reply