Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell获取目录中具有数字名称的所有目录_Powershell_Directory - Fatal编程技术网

使用powershell获取目录中具有数字名称的所有目录

使用powershell获取目录中具有数字名称的所有目录,powershell,directory,Powershell,Directory,我将首先说我是powershell的新手。。。 我可以获取指定目录中的所有目录,但我只需要获取名称为数字的目录。例如: 1 - include 2 - include 3 - include 10 - include LastFailed - exclude 我还需要按顺序订购,以便以后处理 以下是我到目前为止的情况: $Dirs = Get-ChildItem -Path ..\..\..\builds -Attributes D foreach($D in $Dirs) { W

我将首先说我是powershell的新手。。。 我可以获取指定目录中的所有目录,但我只需要获取名称为数字的目录。例如:

1 - include
2 - include
3 - include
10 - include
LastFailed - exclude
我还需要按顺序订购,以便以后处理

以下是我到目前为止的情况:

$Dirs = Get-ChildItem -Path ..\..\..\builds -Attributes D 

foreach($D in $Dirs)
{
    Write-Host $D.Name
}

要仅获取具有数字名称的文件夹列表,可以执行以下操作:

$Path = '..\..\..\builds'
$Dirs = Get-ChildItem -Path $Path -Attributes D | 
        Where-Object { $_.Name -match '^\d+$' } | 
        Sort-Object
$Dirs
上面的代码使用Where Object子句只过滤出只有数字名称的文件夹。 它通过使用
-match
运算符和正则表达式
^\d+$
来实现这一点,其中:

`^`  --> start at the beginning of the line
`\d` --> look for numeric values (0-9)
`+`  --> there must be 1 or more numeric values present
`$`  --> the end of the string
它也可以使用
[0-9]
代码以
Sort Object
结尾,因为OP希望文件夹列表被排序。 因为我们不知道在这里排序哪个属性,Powershell默认为名称

上述内容将产生如下列表:

Mode                LastWriteTime         Length Name                                                                                                                            
----                -------------         ------ ----                                                                                                                            
d-----        18-8-2018     16:13                1                                                                                                                               
d-----        18-8-2018     16:13                10                                                                                                                              
d-----        18-8-2018     16:13                2                                                                                                                               
d-----        18-8-2018     16:13                5
如您所见,列表是按属性名称排序的,但是。。这些数字被视为字符串,因此“10”跟在“1”后面

这里我们最想做的是让Sort对象将名称视为实数,而不是字符串。 为了解决这个问题,我们添加了
Select-Object
cmdlet,以便使用我们需要的属性创建我们自己的对象

我们可以获得通过管道传输的DirInfo对象的标准属性,并且可以创建新的计算属性

在这里,我添加了名为
SortIndex
的计算属性,它只是从名称转换而来的数值,然后我们将使用它进行数值排序:

$Dirs = Get-ChildItem -Path $Path -Attributes D | 
        Where-Object { $_.Name -match '^\d+$' } | 
        Select-Object -Property FullName, Name, LastWriteTime, @{Name = 'SortIndex'; Expression = {[int]$_.Name} } |
        Sort-Object -Property SortIndex
$Dirs
现在,它生成一个对象数组,当打印到console时,该数组如下所示:

FullName   Name LastWriteTime      SortIndex
--------   ---- -------------      ---------
D:\Temp\1  1    18-8-2018 16:13:22         1
D:\Temp\2  2    18-8-2018 16:13:25         2
D:\Temp\5  5    18-8-2018 16:13:28         5
D:\Temp\10 10   18-8-2018 16:13:31        10

希望这有帮助。

您可以利用
Where Object
来过滤您的收藏:

$dirs = Get-ChildItem -Path ..\..\..\builds -Directory |
    Where-Object -Property Name -Match '\d'
此操作使用正则表达式匹配数字类


如果您的意思是只需要以数字开头的目录名,则可以使用锚定:

最后,您可以使用
sort Object
对它们进行排序:

$dirs = Get-ChildItem -Path ..\..\..\builds -Directory |
    Where-Object -Property Name -Match '^\d' |
    Sort-Object -Property Name

您可以通过明确起始数字来进一步增强此排序:

-Property {[int]($_.Name -split '[^\d]')[0]}

在此上下文中,我们将对第一个非数字字符进行拆分,并捕获拆分的第一个片段(该片段应为表示为字符串的数字,因此我们将其转换为整数)。

请添加一些解释。如果未来的读者不知道正则表达式,答案很难理解。@vonPryz你是对的。时间不早了,但我以前唯一的密码答案太直截了当了。我现在编辑了答案来解释。效果很好。谢谢
-Property {[int]($_.Name -split '[^\d]')[0]}