1

I am trying to cut specific content from a string and store it in a variable to use later.

The string is :

\\path\shares\Product\Product_Name\Custom\Version\Version_1\Packages\2018-05-31_07-33-12\PRODUCT_NAME_1_SETUP.exe

How can I cut the part in caps(PRODUCT_NAME_1_SETUP) and store it in variable in powershell.

I'd really appreciate help.

The path is actually stored in a variable called path_name, and I'm trying the following:

$build_name=[io.path]::GetFileNameWithoutExtension('$($path_name)')

But it is not working. I'm getting O/P "$path_name" only. :(

Even $build_name=(Get-Item '$($path_name)').Basename is failing too.

John_J
  • 53
  • 2
  • 9
  • [io.path]::GetFileNameWithoutExtension($p) – Tomek Jun 04 '18 at 11:14
  • What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Jun 04 '18 at 11:14
  • Having given you the boilerplate, I will also suggest that you look at the [`Split-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-6) cmdlet and [`-split` operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_split?view=powershell-6) if you want a "pure PowerShell" solution, or follow the suggestion from @Tomek if direct .NET calls are acceptable. – Jeff Zeitlin Jun 04 '18 at 11:17
  • @JeffZeitlin - My first instinct was also Split-Path, but it does not support trimming file extension. Splitting by '.' is not good also as . is valid character in file name and extension does not have to be exactly 3 chars so trimming last 4 characters would not work either. Y'd need to split by '.' , remove last string, put it back together. – Tomek Jun 04 '18 at 11:27
  • 2
    @John_J `$build_name=[io.path]::GetFileNameWithoutExtension($path_name)` if You do single quotes (') You tell powershell not to evaluate variable inside. just ditch the single quotes. – Tomek Jun 04 '18 at 13:59
  • Yeah, I agree with @Tomek. Your only problem here is your usage of single quotes which are never evaluated for embedded variables or subexpressions. See [`Get-Help about_Quoting_Rules`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6). – Bacon Bits Jun 04 '18 at 15:00
  • Thank you very much for helping! This is working perfectly. :) – John_J Jun 05 '18 at 07:19

2 Answers2

2

If You need to select only file name from a Path - best way is to use [io.path]::GetFileNameWithoutExtension() It was already answered here: Removing path and extension from filename in powershell

Tomek
  • 641
  • 1
  • 6
  • 16
0

Two variants using a RegEx

$String ="\\path\shares\Product\Product_Name\Custom\Version\Version_1\Packages\2018-05-31_07-33-12\PRODUCT_NAME_1_SETUP.exe"
$string -match "([^\\]+)\.exe$"|out-Null
$matches[1]

sls -input $string -patt "([^\\]+)\.exe$"|%{$_.Matches.groups[1].Value}

Explaining the RegEx:

([^\\]+)\.exe$
 1st Capturing Group ([^\\]+)
 Match a single character not present in the list below [^\\]+
 + Quantifier — Matches between one and unlimited times, 
   as many times as possible, giving back as needed (greedy)
 \\ matches the character \ literally (case sensitive)
\. matches the character . literally (case sensitive)
exe matches the characters exe literally (case sensitive)
$ asserts position at the end of the string, 
  or before the line terminator right at the end of the string (if any)