简体   繁体   中英

Powershell: Find numbers in name folders

I have in a directory that contains the following folders:

000000000000000000, 0001251557a1485767, 0144dshbc, 014758, 111147857484752169 and 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. So, the result should be something like:

0144dshbc, 014758, 0001251557a1485767 and 123456789012345678z

But when I run these command, I will get these folders:

0001251557a1485767, 0144dshbc and 014758

My Question: How do I also find folder " 123456789012345678z ", which has 18 numbers in the name, but has a letter at the end.

My goal is to find all folders that don't have 18 numeric characters. Thanks.

This code will find files that do not have exactly 18 all numeric characters in the name. 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. 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 $).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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