powershell - 如何在Powershell中测试无效路径?

标签 powershell path

我有一个脚本尝试使用相对路径运行一些可执行文件。
因此,我使用test-path来验证可执行文件是否在预期的位置。
如果没有,我尝试其他位置。

if(test-path "$current../../../myexe.exe"){
   # found it!
}

但是在这种情况下,如果$ current是C:/folder/,则test-path "C:/folder/../../../myexe.exe"失败,并显示

The path ... referred to an item that was outside the base 'C:'



有没有一种干净可靠的方法来测试路径,以便它返回true或false,并且不会抛出一些意外错误?

最佳答案

Test-Path ([io.path]::Combine($current,(Resolve-Path ../../../myexe.exe)))

有关更多信息,请参见this thread:

关于powershell - 如何在Powershell中测试无效路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900121/

相关文章:

java - Android - 获取从文件资源管理器中选择的.txt文件的真实路径

具有 'dynamic' ConfirmImpact 属性设置的 Powershell Cmdlet

powershell - Powershell-输入标准输入后,捕获标准输出/标准错误不再起作用

sql - 如何在 Powershell 中使用 SQLCMD 将结果导出到 Excel

javascript - Amcharts 图像 "304 Not Modified"错误

python os.path.realpath 无法正常工作

powershell - 使用空用户名创建PSCredential

git - 在 git shell 中运行批处理文件

相对于文件的 Javascript 路径

matlab - 如何检查文件夹是否在搜索路径上