我测试是否有一个名为 \windows\software 的文件夹,并且我正在使用这个 powershell 代码,它可以工作。
$PathBackup = Test-Path Env:\systemroot\software -IsValid
If ($PathBackup -eq $true) {Write-host "There is backup's"}
Else {write-Verbose "There is no backup's of folder \software*"}
但我真正想要的是测试是否有任何其他文件夹匹配 \software******* 文件夹而不是 \software\ 文件夹本身。
我怎样才能用正则表达式做到这一点?
我试过这个,但没有运气:
$PathBackup = Test-Path Env:\systemroot\software -IsValid | Where { $_ -match '\w{8,}'}
可能最直接的方法是使用Get-ChildItem
通配符通配符。Where?
匹配任何字符并且*
是零个或多个任何字符。
这将匹配 \softwarebackup 但不匹配 \software 或 \backupsoftware
if (Get-ChildItem "$Env:SystemRoot\software?*") {
Write-Verbose "There are backups"
} else {
Write-Verbose "There are no backups of folder \software*"
}
这将匹配 \softwarebackup、\software 和 \backupsoftware
if (Get-ChildItem "$Env:SystemRoot\*software*") {
Write-Verbose "There are backups"
} else {
Write-Verbose "There are no backups of folder \software*"
}
这将匹配 \softwarebackup 和 \backupsoftware 但不匹配 \software
#Using Globbing
if (@((Get-ChildItem "$Env:SystemRoot\software?*"),(Get-ChildItem "$Env:SystemRoot\?*software")) | Where-Object {$_}) {
Write-Verbose "There are backups"
} else {
Write-Verbose "There are no backups of folder \software*"
}
#using regex
if (Get-ChildItem "$Env:SystemRoot" | Where-Object {$_.Name -match '.+software$|^software.+'}) {
Write-Verbose "There are backups"
} else {
Write-Verbose "There are no backups of folder \software*"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句