Squirreljester
Squirreljester

Reputation: 191

How can I get a list of filenames from a folder, and split the names at a specific spot?

I have a list of filenames that I'm trying to get from a folder into an array, splitting the names so that a file name of "Application_install_versionnumber.exe" goes into the array as just "Application". I'm using this array to do a check against another array I'm grabbing from the registry.

$FileNames = Get-ChildItem -Path "Path to files" | Select -expand basename
$Installed = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*Keyword* |Select-Object -ExpandProperty DisplayName
$Installed += Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*Keyword2* |Select-Object -ExpandProperty DisplayName
$Installed | Where-Object { $_ -in $Filenames }

I've honestly tried to understand how REGEX works and I just can't figure it out. I've been on multiple sites trying to learn it, and I just don't get it. I've even tried to pick out what others' examples were doing to set up my own. I literally just need it to split at the first underscore it finds, and ignore the underscore and everything after it.

My purpose is to automate a version check for an automated installation I'm building, but I only want it to check the registry for the installed items that are in the folder it's installing from, not everything every time. It's honestly not a huge list and it's very reasonable to just list what's in the registry every time it completes, and be done with it, but I had this idea, and I can't figure it out... and now it's bugging me that I can't figure out how to do it.

This will eventually be put into an email and be sent out to a group to be a verification that the automated installation completed successfully. I can do that part.

Upvotes: 0

Views: 313

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

You don't need regex at all for your problem. Assuming all your filenames are well-formed in the <applicationname>_install_<version>.exe form, you can use System.String's .Split() method:

$AppNames = Get-ChildItem -Path '<String>' |
    ForEach-Object { $_.BaseName.Split('_')[0] }

Upvotes: 1

Related Questions