![](/img/trans.png)
[英]Recursively find same name files between two folders using powershell
[英]Powershell: Find numbers in name folders
我的目录包含以下文件夹:
000000000000000000、0001251557a1485767、0144dshbc,014758、111147857484752169和123456789012345678z
我使用以下代码根据文件夹名称查找文件夹:
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]"}
我希望此代码仅返回其名称不完全包含18个数字字符的文件夹的名称。 因此,结果应为:
0144dshbc,014758、0001251557a1485767和123456789012345678z
但是,当我运行以下命令时,将获得以下文件夹:
0001251557a1485767、0144dshbc和014758
我的问题:我还如何找到文件夹“ 123456789012345678z ”,名称中有18个数字,但末尾有一个字母。
我的目标是查找所有不包含18个数字字符的文件夹。 谢谢。
此代码将查找名称中不完全包含18个数字字符的文件。 详细的日志记录将向您显示每个值的评级方式。 不匹配的值在管道上返回。
$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
}
}
所有重要的逻辑都在IF()语句中。 检查长度是可以理解的。 匹配运算符将查找一个以(以^表示)一个或多个(以+表示)数字([0-9])开头的字符串,并立即击中该字符串的末尾($)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.