Powershell continues with the deletion even if the variable is incorrect

0

Good morning, everyone.

I have a bug in my Powershell script where I am deleting the contents of a directory. The path of said directory is stored in a variable and is used in the following way:

Write-Host "* Vaciando '$RutaTempFicheros'" -ForegroundColor Yellow
Try { Remove-Item -Path $RutaTempFicheros* -Recurse -Force -ErrorAction Stop }
Catch { ManejarErrores -codigo 13 }

The problem comes when doing tests (emptying the variable, placing a new one without value, etc) and thinking that it would give an error, Powershell continues with the deletion, but in this case it empties the directory in which it is located, which in this case case is the directory where the script is stored.

Any idea why this is so or how to do it to avoid this problem?

Greetings and thanks.

    
asked by tributoo2 10.01.2018 в 12:03
source

1 answer

0

The problem is how the value of the parameter "Path" is defined, since the following is found:

$RutaTempFicheros*

From the above it takes the variable $ PathTempFiles and the symbol * that works like a wildcard. That's why you delete the items from your location. The * indicates that you delete in the path that is currently located at the moment of execution.

Greetings,

Victor Silva

    
answered by 10.01.2018 в 13:37