Powershell - 使用通配符搜索文件名 [英] Powershell - using wildcards to search for filename

查看:53
本文介绍了Powershell - 使用通配符搜索文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 PowerShell 脚本,该脚本将在文件夹中搜索包含特定文件掩码的文件名.文件夹中的所有文件的格式都类似于 *yyyyMd*.txt.

I am trying to make a PowerShell script that will search a folder for a filename that contains a certain file-mask. All files in the folder will have format like *yyyyMd*.txt.

我已经制作了一个脚本:

I have made a script:

[String]$date = $(get-date -format yyyyMd)
$date1 = $date.ToString
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*$date1*'}

但这似乎不起作用..

有人可以帮忙吗?问题似乎是日期变量不正确,因为当我硬编码如下内容时,它可以工作:

Can anyone help? It seems the problem is that the date variable is not correct because when I hard code something like below, it works:

Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*20141013*'}

推荐答案

您可以通过使用带有 -match 运算符的正则表达式来简化:

You can simplify this by just using regex with the -match operator:

Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_ -match (Get-Date -format yyyyMMdd)

}

如果您使用的是 V3 或更高版本,则可以进一步简化为:

And if you are on V3 or higher, you can further simplify to:

Get-ChildItem C:\Users\pelam\Desktop\DOM | Where Name -match (Get-Date -format yyyyMMdd)

这篇关于Powershell - 使用通配符搜索文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆