I am trying to create a variable for each fixed width portion of the records in my text file.
\nI keep getting errors when I try using $var.Substring() against my array.
\nI’ve successfully looped arrays before but have never tried to perform a substring on them. Is substring the wrong thing for my goal?<\/p>\n
My text file looks like this:<\/p>\n
W2023121125330020 01/03/202400000000244.440000000111948 \nW2023121207352002 01/03/202400000002884.810000000289452 \nW2023121207352014 01/03/202400000005197.920000000289452 \n<\/code><\/pre>\n
Advertisement
My code is this:<\/p>\n
$csvfile = \"c:\\temp\\SAMPLE.txt\"\n$csvdata = Import-CSV -Path $csvfile\n$index=0\nforeach ($index in 0..($csvdata.Length -1)) {\n$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)\nWrite-Host $Season\nWrite-Host $Year\nWrite-Host $PN\nWrite-Host $DP\nWrite-Host $Amount\nWrite-Host $Check\n}\n\n<\/code><\/pre>\nExpected result:
\nW
\n2023
\n171125330020
\n01/03/2024
\n00000000244.44
\n0000000111948<\/p>\n
Repeated for every line in the txt file.<\/p>\n
Actual result:<\/p>\n
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named\n'SubString'.\n<\/code><\/pre>\nThank you.<\/p>","upvoteCount":2,"answerCount":7,"datePublished":"2024-07-10T19:26:19.541Z","author":{"@type":"Person","name":"jason6148","url":"https://community.spiceworks.com/u/jason6148"},"suggestedAnswer":[{"@type":"Answer","text":"
I am trying to create a variable for each fixed width portion of the records in my text file.
\nI keep getting errors when I try using $var.Substring() against my array.
\nI’ve successfully looped arrays before but have never tried to perform a substring on them. Is substring the wrong thing for my goal?<\/p>\n
My text file looks like this:<\/p>\n
W2023121125330020 01/03/202400000000244.440000000111948 \nW2023121207352002 01/03/202400000002884.810000000289452 \nW2023121207352014 01/03/202400000005197.920000000289452 \n<\/code><\/pre>\nMy code is this:<\/p>\n
$csvfile = \"c:\\temp\\SAMPLE.txt\"\n$csvdata = Import-CSV -Path $csvfile\n$index=0\nforeach ($index in 0..($csvdata.Length -1)) {\n$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)\nWrite-Host $Season\nWrite-Host $Year\nWrite-Host $PN\nWrite-Host $DP\nWrite-Host $Amount\nWrite-Host $Check\n}\n\n<\/code><\/pre>\nExpected result:
\nW
\n2023
\n171125330020
\n01/03/2024
\n00000000244.44
\n0000000111948<\/p>\n
Repeated for every line in the txt file.<\/p>\n
Actual result:<\/p>\n
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named\n'SubString'.\n<\/code><\/pre>\nThank you.<\/p>","upvoteCount":2,"datePublished":"2024-07-10T19:26:19.637Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/1","author":{"@type":"Person","name":"jason6148","url":"https://community.spiceworks.com/u/jason6148"}},{"@type":"Answer","text":"
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.<\/p>\n
My first thought is that since the original file is not delimited in any way, you will want to not<\/em> import it as csv content, but keep it as string content. Once you parse the parts, then<\/em> you can convert it to csv if you need to.<\/p>\n\n$csvdata = Get-Content $csvfile<\/p>\n<\/blockquote>\n
Now you can manipulate the contents as strings, including using the .SubString() method<\/p>","upvoteCount":5,"datePublished":"2024-07-10T20:07:29.821Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/2","author":{"@type":"Person","name":"AdmiralKirk","url":"https://community.spiceworks.com/u/AdmiralKirk"}},{"@type":"Answer","text":"
like so? looks like some of your substring values were off.
\nalso looks like your foreach is funky…
\neither do FOR or FOREACH you seem to have come up with some… mixture lol<\/p>\n
$csvdata = Get-Content \"D:\\test.csv\"\n\n\n$data = \nforeach($row in $csvdata){\n\n [PSCustomObject]@{\n Season = $row.SubString(0, 1)\n Year = $row.SubString(1, 4)\n PN = $row.SubString(6, 12)\n DP = $row.SubString(30, 10)\n Amount = $row.SubString(40, 14)\n Check = $row.SubString(55, 12)\n }\n \n}\n\n$data\n<\/code><\/pre>\nEDIT: added output<\/p>\n
Season : W\nYear : 2023\nPN : 21125330020 \nDP : 01/03/2024\nAmount : 00000000244.44\nCheck : 000000111948\n\nSeason : W\nYear : 2023\nPN : 21207352002 \nDP : 01/03/2024\nAmount : 00000002884.81\nCheck : 000000289452\n\nSeason : W\nYear : 2023\nPN : 21207352014 \nDP : 01/03/2024\nAmount : 00000005197.92\nCheck : 000000289452\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2024-07-10T20:57:34.423Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thanks. I’ll give that a look.<\/p>","upvoteCount":0,"datePublished":"2024-07-10T21:49:32.391Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/4","author":{"@type":"Person","name":"jason6148","url":"https://community.spiceworks.com/u/jason6148"}},{"@type":"Answer","text":"
That works very nicely. Thank you.<\/p>\n
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.<\/p>","upvoteCount":0,"datePublished":"2024-07-10T22:07:16.755Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/5","author":{"@type":"Person","name":"jason6148","url":"https://community.spiceworks.com/u/jason6148"}},{"@type":"Answer","text":"
$data[3]<\/code> would give you just the fourth record (first row is 0).<\/p>","upvoteCount":4,"datePublished":"2024-07-10T22:41:24.571Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/6","author":{"@type":"Person","name":"semicolon","url":"https://community.spiceworks.com/u/semicolon"}},{"@type":"Answer","text":"Wonderful! Thank you!<\/p>","upvoteCount":1,"datePublished":"2024-07-10T23:22:03.737Z","url":"https://community.spiceworks.com/t/using-substring-with-an-array-in-powershell/1094938/7","author":{"@type":"Person","name":"jason6148","url":"https://community.spiceworks.com/u/jason6148"}}]}}