0

I have a txt file whose current output looks like this:

filetest.WIN.txt Wed Feb 10 12:00:37 2021
filetest.VID.avi Wed Feb 10 12:00:51 2021

I essentially need each individual line to be its own array, with each item separated by a space to make up the items in the array. So I can take action on the same array item of each line.

I've tried this, but it puts the entire file into 1 large array:

$arrayFromFile = Get-Content -Path $Path

$newArray =$arrayFromFile.Split(" ")

$newArray

But that output is this:

filetest.WIN.txt
Wed
Feb
10
12:00:37
2021
filetest.VID.avi
Wed
Feb
10
12:00:51
2021

I'm also open to other ways of doing this if it's more efficient.

1
  • By what you are doing, have is an array. What exactly are you trying to accomplish? Commented Feb 12, 2021 at 5:22

2 Answers 2

2

Continuing from my comment...

Your code lines can be refactored to just this.

***Note:

I am using PowerShell variable squeezing to assign to the variable and output at the same time. That is not a requirement, just a choice.***

($NewArray = (Get-Content -Path 'D:\Temp\FileData.txt').Split(''))
# Results
<#
filetest.WIN.txt
Wed
Feb
10
12:00:37
2021
filetest.VID.avi
Wed
Feb
10
12:00:51
2021
#>

As I stated this is an array, and you can determine this, just by doing this

$NewArray.GetType()
# Results
<#
IsPublic IsSerial Name                                     BaseType 
-------- -------- ----                                     -------- 
True     True     Object[]                                 System.Array
#>

Or doing this.

$NewArray[0]
# Results
<#
filetest.WIN.txt
#>

$NewArray[9]
# Results
<#
10
#>

Or is this your use case...

# The default display
(
$NewDataObject  = Import-Csv -Path 'D:\Temp\FileData.txt' -Delimiter ' ' -Header FileName, 
                                                                                 DayName, 
                                                                                 Month,
                                                                                 DayNumber, 
                                                                                 Time, 
                                                                                 Year
)
# Results
<#
FileName  : filetest.WIN.txt
DayName   : Wed
Month     : Feb
DayNumber : 10
Time      : 12:00:37
Year      : 2021

FileName  : filetest.VID.avi
DayName   : Wed
Month     : Feb
DayNumber : 10
Time      : 12:00:51
Year      : 2021
#>
# Or forced to display as a table.
(
$NewDataObject  = Import-Csv -Path 'D:\Temp\FileData.txt' -Delimiter ' ' -Header FileName, 
                                                                                 DayName, 
                                                                                 Month,
                                                                                 DayNumber, 
                                                                                 Time, 
                                                                                 Year | 
Format-Table -AutoSize
)
# Results
<#
FileName         DayName Month DayNumber Time     Year
--------         ------- ----- --------- ----     ----
filetest.WIN.txt Wed     Feb   10        12:00:37 2021
filetest.VID.avi Wed     Feb   10        12:00:51 2021
#>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, this is a nice creative solution
Thanks, I a firm believer in using the least amount of English readable code possible. I want to be able to provide the most junior folks, results that are as complete and as easily understandable/self-documenting as possible. Also. you have been offering up several yourself. ;-} Keep up that good work.
0

So essentially you want something that turns the rows into objects where you can access like properties? Something like this?

property1        property2 property3 property4 property5 property6
---------        --------- --------- --------- --------- ---------
filetest.WIN.txt Wed       Feb       10        12:00:37  2021
filetest.VID.avi Wed       Feb       10        12:00:51  2021

If so, here is the code

$line1 = "filetest.WIN.txt Wed Feb 10 12:00:37 2021" -split " "
$line2 = "filetest.VID.avi Wed Feb 10 12:00:51 2021" -split " "

function ArrayToObject([string[]]$array) {
    $count = $array.Count
    if ($count) {
        $object = [ordered]@{ }
        foreach ($x in 1..$count) {
            $object["property$x"] = $array[$x - 1]
        }
        [PSCustomObject]$object
    }
}

$customObjectArray = @(
    ArrayToObject $line1
    ArrayToObject $line2
)

$customObjectArray | Format-Table

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.