简体   繁体   English

Powershell:在名称文件夹中查找数字

[英]Powershell: Find numbers in name folders

I have in a directory that contains the following folders: 我的目录包含以下文件夹:

000000000000000000, 0001251557a1485767, 0144dshbc, 014758, 111147857484752169 and 123456789012345678z 000000000000000000、0001251557a1485767、0144dshbc,014758、111147857484752169和123456789012345678z

And I use this code to find folders based on their name: 我使用以下代码根据文件夹名称查找文件夹:

Get-ChildItem | Where-Object {$_.Name -notmatch "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"}

I would like this code to return only the names of folders whose names do not contain exactly 18 numeric characters. 我希望此代码仅返回其名称不完全包含18个数字字符的文件夹的名称。 So, the result should be something like: 因此,结果应为:

0144dshbc, 014758, 0001251557a1485767 and 123456789012345678z 0144dshbc,014758、0001251557a1485767和123456789012345678z

But when I run these command, I will get these folders: 但是,当我运行以下命令时,将获得以下文件夹:

0001251557a1485767, 0144dshbc and 014758 0001251557a1485767、0144dshbc和014758

My Question: How do I also find folder " 123456789012345678z ", which has 18 numbers in the name, but has a letter at the end. 我的问题:我还如何找到文件夹“ 123456789012345678z ”,名称中有18个数字,但末尾有一个字母。

My goal is to find all folders that don't have 18 numeric characters. 我的目标是查找所有不包含18个数字字符的文件夹。 Thanks. 谢谢。

This code will find files that do not have exactly 18 all numeric characters in the name. 此代码将查找名称中不完全包含18个数字字符的文件。 The verbose logging will show you how each value is rated. 详细的日志记录将向您显示每个值的评级方式。 The non-matching values are returned on the pipeline. 不匹配的值在管道上返回。

$VerbosePreference = 'continue'

$list = Get-ChildItem | Select-Object -ExpandProperty Name
foreach ($item in $list)
{

    if($item.Length -eq 18 -and $item -match '^[0-9]+$' )
    {
        Write-verbose 'is both 18 chars and numeric'
        Write-verbose "- $item, length: $($item.Length)"
    }
    else
    {
        Write-verbose 'is not 18 chars and numeric'
        Write-verbose "- $item, length: $($item.Length)"
        Write-Output $item
    }
}

All of the important logic is in the IF() statement. 所有重要的逻辑都在IF()语句中。 Checking for length understandable. 检查长度是可以理解的。 The match operator looks for a string that begins with (indicated by the ^) one or more (indicated by the +) numbers (the [0-9]) and immediately hits the end of the string (the $). 匹配运算符将查找一个以(以^表示)一个或多个(以+表示)数字([0-9])开头的字符串,并立即击中该字符串的末尾($)。

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

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