test-path判断文件是否存在


PowerShell交流中心分类: Powershell基础test-path判断文件是否存在
0
ykxxxbc asked 6年 ago

用test-path可以判断文件是否存在
要判断多个文件可以在中间跟上 -and 
eg: 
(test-path C:\Users\Administrator\Desktop\script\* -include hello.ps1) – and (test-path C:\Users\Administrator\Desktop\script\* -include test.ps1) -and (test-path C:\Users\Administrator\Desktop\script\* -include info.ps1)
但是有没有一种更方便的写法呢? 

1 Answers
3
Best Answer
Mooser Lee 管理员 answered 6年 ago
$fileNames = "a.txt","b.ps1","c.mp3"
$result
$fileNames | foreach {
if(-not (Test-Path -Path "你的目录" -Include $_)){
$result=$false;
break;
}
}
#判断result 结果
$result
c***m replied 4年 ago

Test-Path D:\[]Concur.docx
结果显示的是这个文档是False
把[]去掉就是True
这个怎么处理?

c***m replied 4年 ago

Test-Path -LiteralPath “C:\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx” -PathType Leaf
False
这个文件是存在的啊。咋判断的是False呢