Check if an item exists using a relative path

I’m adding Windows support to my project and I’m using powershell for the Windows parts in my makefile. What I want to do is remove a file called out\ferium-windows-msvc.zip (relative to the cwd) if it exists. I’ve been fighting with powershell trying to do such a simple task for the past day. In *nix I used rm -f out/ferium-linux-gnu.zip. The -f tells rm to ignore the error if the file does not exist. Remove-Item doesn’t have such a switch afaik (its -Force does something else) and for some reason checking if the file exists with Test-Path doesn’t work presumably because the path to the file is relative to the cwd. I’ve tried the following:

if (Test-Path -Path ".\out\ferium-windows-msvc.zip") {
    Remove-Item -Path ".\out\ferium-windows-msvc.zip"
}

But I get this error: -Path was unexpected at this time.
I removed -Path as it’s optional and it still says: ".\out\ferium-windows-msvc.zip") was unexpected at this time.

Why is doing such a simple task so tedious and unintuitive? Is powershell just completely different from most other shell languages?

In PowerShell all built-in cmdlets support the so called common parameters.

One of them is -ErrorAction. With this you can control how PowerShell reacts if an error occurs.

Why shouldn’t that work?

If the file is in a subdirectory of the current working directory with the name “out” your code is correct and should work. Does your script run in the correct directory?

Even if it’s optional you should not ommit it and write your code as verbose as possible. That makes it easier to read. :wink:

You cannot learn a complex technology by guessing. You may start with learning the very basics of PowerShell first. :wink:

Yes, it definitely is. Because it works with powerful objects and properties instead of stupid and boring strings and text it is way more advanced than other shells.

2 Likes

Thank you so much for being helping me! I apologise for being harsh in my original post.

I really should have done this, I’ve never used powershell (or Windows much for that matter) and I realise now the Power-shell is quite powerful. I’ve seen more complex code using classes, namespaces, and such. I must admit I am a little overwhelmed.

I do intend on being more verbose. I did this as a debugging step because the interpreter (?) said that it was unexpected.

Nope! The make subprocess doesn’t seem to inherit the cwd. Get-Location doesn’t return anything when run from the makefile. I don’t want to use Set-Location because this is a public repository and people will clone to different locations. So how can I make make inherit the cwd? I installed it using Chocolatey and it’s a GNU program so I’m not surprised it doesn’t cooperate well with powershell.

PowerShell has some automatic variables you can use. One of them is $PWD

what represents the current working directory. Another one is $PSScriptRoot

you can use to determine the directory the script has been started from.

Than you can use

to switch to a desired directory if needed and

to come back to where you came from. And that even works in multiple levels. :wink:

2 Likes

This is CMD error, not a PowerShell error. Are you trying to run PowerShell code from a .BAT or .CMD file?

I’m running a make target using make (from Chocolatey) so I think the parent process’ shell is used?

I’m having some trouble using this variable. I’m running Remove-Item -ErrorAction:Ignore -Path "$($PSScriptRoot)\out\ferium-windows-msvc.zip" but make says it is actually running Remove-Item -ErrorAction:Ignore -Path "\out\ferium-windows-msvc.zip", I guess that means make is interfering with the ‘script’? Did I make a syntax error or is the problem something else?

Sounds like it’s using the CMD interpreter, not PowerShell. You could call powershell.exe with the -command parameter, but I’d be inclined to just use:

IF EXIST ".\out\ferium-windows-msvc.zip" (DEL ".\out\ferium-windows-msvc.zip")

Ah what a shame, I was rather excited to try out powershell. I will use CMD for now then. Might try and rewrite the makefile in powershell some other time