Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate filename with current directory path

Tags:

powershell

I'm trying to execute a PowerShell script that opens an Excel file and does a SaveAs with it. the problem is that it works when I put the full path for the file, but I want it to be directed to the working folder, that way if I take the script and files to other location I don't have to modify the script for each environment.

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open("E:\TEMP\TEMPLATE4WEEKS.xlsx")#Opening like this works

$wb = $excel.Workbooks.Open(ThisWorkbook.Path & "\TEMPLATE4WEEKS.xlsx") #this is not working 
$wb.SaveAs(ThisWorkbook.Path & "\TEMPLATE4WEEKSprotected.xlsx",[Type]::Missing,"password") #this is not working
$excel.Quit()

I was trying to concatenate current folder so it will work if I say move the entire folder to another path.

Right now the folder that contains the PS1 script is the same that contains the TEMPLATE4WEEKS.xlsx file. I want all to operate inside the same folder.

Changed as per Ansgar's response:

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx"
$wb = $excel.Workbooks.Open($file)
$wb.SaveAs($file,[Type]::Missing,"password")
$excel.Quit()

I'm getting this error now:

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:4 char:19
+ $file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx"
+                   ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Join-Path],     ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

Sorry, we couldn't find . Is it possible it was moved, renamed or deleted?
At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:5 char:1
+ $wb = $excel.Workbooks.Open($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:6 char:1
+         $wb.SaveAs($file,[Type]::Missing,"password")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
like image 311
Baldie47 Avatar asked Jan 25 '23 22:01

Baldie47


1 Answers

& in PowerShell is not a concatenation operator. Plus, you don't want to build paths via string concatenation in the first place. You also cannot use the object ThisWorkbook directly in PowerShell (that only works in VBA, and it's the location of the current macro, so it won't work here at all because you're not running a macro).

Change this:

$wb = $excel.Workbooks.Open(ThisWorkbook.Path & "\TEMPLATE4WEEKSxlsx")

into this:

$wb = $excel.Workbooks.Open("${PSScriptRoot}\TEMPLATE4WEEKS.xlsx")

Note: The automatic variable $PSScriptRoot was introduced with PowerShell v3 and doesn't exist in earlier versions. If you're running an older version you need to define it yourself, e.g. like this:

$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
like image 153
Ansgar Wiechers Avatar answered Feb 05 '23 04:02

Ansgar Wiechers