简体   繁体   English

Powershell 获取 childitem 文件名掩码

[英]Powershell get childitem filename masks

I'm working on a script that goes through folders with multiple .pdf-files in it.我正在编写一个脚本,它遍历包含多个 .pdf 文件的文件夹。 Only when the name of the file fits one of two masks, Powershell should continue executing certain steps.只有当文件名符合两个掩码之一时,Powershell 才应继续执行某些步骤。 Else it should continue to search for the next file fitting the mask.否则它应该继续搜索下一个适合掩码的文件。

I've used:我用过:

Get-ChildItem $PATH -Filter "*TEXT-TXT-*" -Recurse -Force |Foreach-Object {

        write-host $_.Name

But this filters only hardcoded.但是这个过滤器只是硬编码。 How to use a mask and preferable use two mask as filter?如何使用口罩,最好使用两个口罩作为过滤器? The masks should be:口罩应该是:

  • "##-##-#### XXXX-XXX-XXXX-XX.PDF" “##-##-#### XXXX-XXX-XXXX-XX.PDF”
  • "##-##-#### XXXX-XXX-XXX-XXXX-XX.PDF" “##-##-#### XXXX-XXX-XXX-XXXX-XX.PDF”

Where # are any numbers and X are any non-number characters.其中 # 是任何数字,X 是任何非数字字符。

Hope this is possible.希望这是可能的。

for this you can use regular expressions, add several conditions to them through "or" for example as follows为此,您可以使用正则表达式,通过“或”向它们添加几个条件,例如如下

Get-ChildItem $PATH -Filter "*TEXT-TXT-*" -Recurse -Force|Where-Object {($_.name -match '\d{2}-\d{2}-\d{4} \w{4}-\w{3}-\w{3}-\w{4}-\w{2}.PDF') -or($_.name -match '\d{2}-\d{2}-\d{4} \w{4}-\w{3}-\w{4}-\w{2}.PDF')}

in this expression it is enough to substitute the name of the file transmitted by the piping在这个表达式中,替换管道传输的文件名就足够了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM