I am trying to create a variable for each fixed width portion of the records in my text file.
I keep getting errors when I try using $var.Substring() against my array.
I’ve successfully looped arrays before but have never tried to perform a substring on them. Is substring the wrong thing for my goal?

My text file looks like this:

W2023121125330020             01/03/202400000000244.440000000111948             
W2023121207352002             01/03/202400000002884.810000000289452             
W2023121207352014             01/03/202400000005197.920000000289452             

My code is this:

$csvfile = "c:\temp\SAMPLE.txt"
$csvdata = Import-CSV -Path $csvfile
$index=0
foreach ($index in 0..($csvdata.Length -1)) {
$Season, $Year, $PN, $DP, $Amount, $Check = $csvdata[$index].SubString(0, 1), $csvdata[$index].SubString(2, 4), $csvdata[$index].SubString(6, 29), $csvdata[$index].SubString(30, 10), $csvdata[$index].SubString(40, 14),$csvdata[$index].SubString(55, 25)
Write-Host $Season
Write-Host $Year
Write-Host $PN
Write-Host $DP
Write-Host $Amount
Write-Host $Check
}

Expected result:
W
2023
171125330020
01/03/2024
00000000244.44
0000000111948

Repeated for every line in the txt file.

Actual result:

Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named
'SubString'.

Thank you.

2 Spice ups

Many years ago I did exactly this work using VBScript (if that tells you how many years ago that might have been). That said, such code is of no value here.

My first thought is that since the original file is not delimited in any way, you will want to not import it as csv content, but keep it as string content. Once you parse the parts, then you can convert it to csv if you need to.

$csvdata = Get-Content $csvfile

Now you can manipulate the contents as strings, including using the .SubString() method

5 Spice ups

like so? looks like some of your substring values were off.
also looks like your foreach is funky…
either do FOR or FOREACH you seem to have come up with some… mixture lol

$csvdata = Get-Content "D:\test.csv"


$data = 
foreach($row in $csvdata){

    [PSCustomObject]@{
        Season = $row.SubString(0, 1)
        Year   = $row.SubString(1, 4)
        PN     = $row.SubString(6, 12)
        DP     = $row.SubString(30, 10)
        Amount = $row.SubString(40, 14)
        Check  = $row.SubString(55, 12)
    }
   
}

$data

EDIT: added output

Season : W
Year   : 2023
PN     : 21125330020 
DP     : 01/03/2024
Amount : 00000000244.44
Check  : 000000111948

Season : W
Year   : 2023
PN     : 21207352002 
DP     : 01/03/2024
Amount : 00000002884.81
Check  : 000000289452

Season : W
Year   : 2023
PN     : 21207352014 
DP     : 01/03/2024
Amount : 00000005197.92
Check  : 000000289452
5 Spice ups

Thanks. I’ll give that a look.

That works very nicely. Thank you.

Is it possible to reference individual fields when outside the foreach loop? I see I can reference $data.Season, but that gives me the Season for every record. I may want the Season, DP, and Amount for only the 4th record.

$data[3] would give you just the fourth record (first row is 0).

4 Spice ups

Wonderful! Thank you!

1 Spice up