JM1
JM1

Reputation: 1715

Powershell - How to split a string based on characters?

I have a list of pdf filenames that need to be parsed and ultimately sent to a sql table, with the parse out pieces each in their own column. How would I split based on a dash '-' and ultimately get it into a table.

What cmdlets would you start with to split on a character? I need to split based on the dash '-'.

Thanks for the help.

Example File Names:

Desired Results:

enter image description here

Upvotes: 0

Views: 605

Answers (3)

Lee_Dailey
Lee_Dailey

Reputation: 7479

yet another way is to use regex & named capture groups. [grin]

what it does ...

  • creates a set of file name strings to work with
    when ready to use real data, remove the entire #region/#endregion block and use either (Get-ChildItem).Name or another method that gives you plain strings.
  • iterates thru the collection of file name strings
  • uses $Null = to suppress the False/True output of the -match call
  • does a regex match with named capture groups
  • uses the $Match automatic variable to plug the captured values into the desired properties of a [PSCustomObject]
  • sends that PSCO out to the $Results collection
  • displays that on screen
  • sends it to a CSV for later use

the code ...

#region >>> fake reading in a list of file names
#    in real life, use (Get-ChildItem).Name
$InStuff = @'
tester-2458-full_contact_snapshot-20200115_1188.pdf
tester-2458-limited_contact_snapshot-20200119_9330.pdf
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a list of file names

$Results = foreach ($IS_Item in $InStuff)
    {
    $Null = $IS_Item -match '^(?<User>.+)-(?<AppId>.+)-(?<FileType>.+)-(?<Date>.+)\.pdf$'
    [PSCustomObject]@{
        User = $Matches.User
        AppId = $Matches.AppId
        FileType = $Matches.FileType
        Date = $Matches.Date
        FileName = $IS_Item
        }
    }

# display on screen    
$Results

# send to CSV file
$Results |
    Export-Csv -LiteralPath "$env:TEMP\JM1_-_FileReport.csv" -NoTypeInformation

output to screen ...

User     : tester
AppId    : 2458
FileType : full_contact_snapshot
Date     : 20200115_1188
FileName : tester-2458-full_contact_snapshot-20200115_1188.pdf

User     : tester
AppId    : 2458
FileType : limited_contact_snapshot
Date     : 20200119_9330
FileName : tester-2458-limited_contact_snapshot-20200119_9330.pdf

content of the C:\Temp\JM1_-_FileReport.csv file ...

"User","AppId","FileType","Date","FileName"
"tester","2458","full_contact_snapshot","20200115_1188","tester-2458-full_contact_snapshot-20200115_1188.pdf"
"tester","2458","limited_contact_snapshot","20200119_9330","tester-2458-limited_contact_snapshot-20200119_9330.pdf"

Upvotes: 1

Nasir
Nasir

Reputation: 11401

There is also a -split operator.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_split

basic example:

if you have file names in $FilePaths array.

foreach($filepath in $FilePaths)
{
  $parts = $filepath -split '-'; 
  [pscustomobject]@{"User" = $parts[0]; "AppID" = $parts[1]; "FileType" = $parts[2]; "FilePath"=$filepath }
}

Upvotes: 1

LordPupazz
LordPupazz

Reputation: 669

Use $variable.split('-') which will return a string array with a length equal to however many elements are produced by the split operation.

Upvotes: 1

Related Questions