Pomme De Terre
Pomme De Terre

Reputation: 149

Adding needed string to Split-Path

I want to pass file as argument to:

Invoke-Sqlcmd $SqlQuery -InputFile $sqlPath

The file is located in different folder. Parent Folder -> Different Folder -> Desired File

I accessed the Parent Folder using:

$sqlPath = Split-Path -Parent $PSScriptRoot

How can I add the final part to $sqlPath? It's my frist script and I have no idea how to add \DifferentFolder\DesiredFile.sql (string?) to this variable.

Upvotes: 0

Views: 736

Answers (2)

boxdog
boxdog

Reputation: 8442

You can use string interpolation to do this:

$sqlPath = "$(Split-Path $PSScriptRoot -Parent)\DifferentFolder\DesiredFile.sql"

If you place variables in a double-quoted string, PowerShell will replace them for you at run-time. Even more flexible is the ability to place executable code inside a string (as shown in my example), using a sub-expression $(...). However, be careful not to make the string unreadable with too much code. In some cases it is better to split this into multiple steps:

$parentFolder = Split-Path $PSScriptRoot -Parent
$sqlPath = "$parentFolder\DifferentFolder\DesiredFile.sql"

Upvotes: 1

cjb110
cjb110

Reputation: 1491

What your after is how to do 'string concatenation', basically adding two strings to form a longer one, PowerShell has a few methods, but + works.

You also said you want to update $sqlpath, so

   $sqlpath = $sqlpath + "\DifferentFolder\DesiredFile.sql"

Should work.

Upvotes: 1

Related Questions