PowerShell 查看是否存在与 env:systemroot 匹配且名称较长的正则表达式的文件夹

托马斯·博格·彼得森

我测试是否有一个名为 \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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Excel中读取条形码以查看是否存在匹配项

如何使用Bash中的正则表达式测试文件夹是否存在?

检查列表以查看Powershell中是否存在文件?

是否有库方法来查看是否存在识别 PATH 环境变量中的路径的文件

Powershell 脚本,它将使用正则表达式搜索文件夹并删除早于 XX 的文件

“文件夹名MM_DD__YYYY”是否有正则表达式

当被测试的字符串中存在其他字符时,Powershell 正则表达式如何防止匹配

查找HTML中的链接以查看是否与数组匹配

使用regexmatch将单元格的一部分与列中的值进行匹配,以查看是否存在匹配项

在PowerShell中进行多行正则表达式匹配

正则表达式以匹配Powershell中的URL

Powershell-正则表达式多个匹配项

PowerShell正则表达式匹配Y或Z

PowerShell 多行匹配正则表达式

使用正则表达式检查特定目录是否具有以特定单词开头的文件夹

如何使用Regex检查URL数组以查看是否返回到原始文件夹

是否可以将Dockerfile中的ENV变量与正则表达式或包含之类的类似内容进行比较?

Mitel的Powershell正则表达式日志文件

使用Powershell脚本的renamig文件的正则表达式

Powershell使用正则表达式替换文件

用于文件路径的Powershell正则表达式

正则表达式是否将除2个文件夹之外的所有内容都包含在根文件夹下?

Powershell检查文件是否存在

Powershell检查文件夹结构是否存在

多行正则表达式-Powershell

PowerShell正则表达式过滤

Powershell 正则表达式查询

PowerShell正则表达式多行

Powershell 正则表达式组