如何在系统路径中查找某个文件是否可用? [英] How to find whether a certain file is available in the system path?

查看:48
本文介绍了如何在系统路径中查找某个文件是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Powershell 脚本必须从 VS 命令提示符运行,我想通过检查 msbuild.exe 是否在路径中来验证这一点.我设法使用 where.exe 做到了:

My Powershell script must be run from a VS command prompt, and I want to verity that by checking that msbuild.exe is in path. I managed to do it using where.exe:

where.exe msbuild.exe > $null
if ($LASTEXITCODE -ne 0)
{
    throw "BuildAll must be run from ""Developer Command Prompt for VS2012"" shortcut."
}

然而,这感觉不像是Powershell 方式"——有没有一种 Powershell 原生方式来做到这一点?

However, this doesn't feel like "the Powershell way" - is there a Powershell-native way to do this?

推荐答案

如果 msbuild.exe 必须与脚本位于同一文件夹中,请尝试此操作

Try this if the msbuild.exe must be in the same folder as the script is

   if ( TEST-PATH (JOIN-PATH (Split-Path -parent $MyInvocation.MyCommand.Definition) "msbuild.exe " ))
{
  ...
}
else
{
  ...
}

如果您想检查 build.exe 是否可用作命令($env:path 中的任何可用路径),您可以执行以下操作:

If you want check if build.exe is available as command (any available path from $env:path) you can do:

if ([bool](Get-Command "msbuild.exe" -ea silentlycontinue)) 
{ 
  ...
}

这篇关于如何在系统路径中查找某个文件是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆