King of NES
King of NES

Reputation: 271

How do I format a row of row of strings to be aligned properly when adding content?

I have a 4200 .txt files which I need to collect the same data from: File Name, Image Count, Document Count. I can correctly collect the data from each file, but run into trouble when trying to write that data to a new .CSV file. To make things easier, I'm using a .txt file to write to, until I can figure this out.

Add-Content "U:\FileProject\logfiles.txt" "`n$fileName `t $imageFile `t`t`t`t`t   $docFile"

The above line correctly starts each line of a data on a new line, tabbed out so that I have 3 headings with all the files data listed underneath. However, due to using /t, each file doesn't always line up correctly, as some have larger names than others. Nothing is justified, so it just places a new line and tabs from the end of each entry.

How do I properly format the Add-Content to correctly left justify each column of data? The file name(up to 70 characters) should start at position 0, ImageCount should start at position 80, and the final heading DocumentCount should be around position 110 on each line. An example for both a .txt file as well as the CSV file would be greatly appreciated!

Upvotes: 1

Views: 726

Answers (1)

Nasir
Nasir

Reputation: 11401

Take a look at the composite formatting using -f operator.

$output = ("{0,-70}{1,-30}{2}{3}" -f $filename,$imagFile,$docFile,[Environment]::NewLine)
Add-Content -Path "U:\FileProject\logfiles.txt" $output

references:

http://www.lazywinadmin.com/2016/08/powershell-composite-formatting.html https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.71).aspx

Upvotes: 3

Related Questions