如何搜索另一个文件,如果不存在并使用它?
问题描述:
我需要创建一个可以找到文件的脚本,如果它不存在(或拼写错误),请使用其他文件。如何搜索另一个文件,如果不存在并使用它?
从我所看到的 - 似乎只有写主机的答案,我不希望它做。在做了一些研究之后,看起来像一个if
声明是前进的方向,但我遇到了问题。已经得到(但可能是疯狂的错误):
$test = if (Test-Path 'C:\test1') else (Test-Path 'C:\test2')
答
是这样的? You should really use Google for getting to grips with basic syntax
$filePath1 = 'C:\test1'
$filePath1 = 'C:\test2'
if (Test-Path $filePath1){
$test = $filePath1
}Elseif(Test-Path $filePath2){
$test = $filePath2
}Else{
Write-Host "Neither file exits."
}
'$测试= {(试验路径 'C:\ TEST1') - 或(测试的路径 'C:\ TEST2')}'(可变'$ test'将'$真'如果任一路径存在) –