9

I'm trying to concatenate two variables within Powershell to form a complete file path and file name to save a text file.

I have two variables

  • $Log_path and
  • $Log_name

I'm trying to join the two variables together within this piece of code:

$objWorkbook = $objExcel.Workbooks.Open $Log_path"\ $Log_name"

I'm unable to find the correct combination of functions??

A "\" is also required to be added between the Log_path and Log_name variables to correctly format the file path.

6

You can use Join-Path and it will put in the directory slash for you.

$objWorkbook = $objExcel.Workbooks.Open (Join-Path $Log_path $Log_name)

It handles the logic if $Log_Path parent already has or doesn't have the slash.

>join-path c:\temp test.txt
c:\temp\test.txt

>join-path c:\temp\ text.txt
c:\temp\test.txt
12
$path = "C:\folder"
$name = "file.exe"
$fullname = $path + "\" + $name
$fullname

(or)

$fullname = "$path\$name"

but not

$fullname = '$path\$name'

Output

C:\folder\file.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.