Powershell:将目录中的文件列表添加到数组列表中

问题描述 投票:1回答:1

我正在尝试将目录中的文件列表保存到数组列表中,请您共享Windows PowerShell的代码段

我在c:\ temp下有2个文件

file1 : stack.zip
file2 : overflow.zip

需要将文件1和2存储在名为的数组中

$arrlst=['stack.zip','overflow.zip']
*set-Location -path "c:\temp"
fileslst=Get-children
$arrlst=[filelist]*
powershell get-childitem
1个回答
1
投票

运行下面的内容将为您提供所需要的。

[System.Collections.ArrayList]$arrlst = @(
    $(Get-ChildItem -File -Path 'C:\temp' | Select -ExpandProperty Name)
)

您需要执行Select -ExpandProperty Name以确保Get-ChildItem的唯一结果是包含扩展名(Name)的文件名。

© www.soinside.com 2019 - 2024. All rights reserved.