【发布时间】:2017-12-23 13:32:36
【问题描述】:
我的 windows 文件夹中有 2 个文件夹,分别是 software 和 softwaretest。 所以我有主文件夹“软件”if语句,然后跳转到elseif - 这里我有备份文件夹,所以跳转到else...
我的问题是我从 elseif 获取写入主机,并且我有一个备份文件夹,我正在调用 softwaretest,所以不明白为什么它给我输出而不是 else。
希望有人可以指导/帮助我:-)
If ($SoftwarePathBackup = Get-ChildItem -Path "$Env:SystemRoot" | Where-Object { (!$_.Name -like 'software') }) {
Write-Host ( 'There are no folder named \software\ on this machine - You cant clean/clear/empty the folder!' ) -ForegroundColor Red;
} elseif ($SoftwarePathBackup = Get-ChildItem -Path "$Env:SystemRoot" | Where-Object { ($_.Name -match '.+software$|^software.+') } | Sort-Object) {
Write-Host ( 'There are none folder-backups of \software\ on this machine - You need to make a folder-backup of \software\ before you can clean/clear/empty the folder!' ) -ForegroundColor Red;
} else {
Remove-Item
}
【问题讨论】:
-
你使用
$_.Name -match '.+software$|^software.+'而不是$_.Name -like 'softwaretest'的原因是什么? -
嗨 Paxz.. 我和常规 exp./RegEX。 :-) 最后一个表达式将为软件返回 true,对吗?它不适用于备份软件和软件备份,对吗? - elseif 语句需要查看名称中包含软件的文件夹,但不是独立单词软件,如果没有名称包括软件.. 然后运行 elseif - 然后“软件”之前和之后的单词/字母可以任何单词/字母,softwaretest 只是一个例子。
标签: regex powershell if-statement wildcard negation