简体   繁体   English

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

[英]Add list of files in a directory to array list

I am trying to save the list of files in a directory to array list, can you please share the code snippet for Windows PowerShell.我正在尝试将目录中的文件列表保存到数组列表中,您能否分享 Windows PowerShell 的代码片段。

I have 2 files under c:\temp我在 c:\temp 下有 2 个文件

file1: stack.zip文件1:堆栈.zip
file2: overflow.zip文件2:溢出。zip

Need to store file1 & file2 in a array called需要将 file1 和 file2 存储在一个名为的数组中

$arrlst = ['stack.zip','overflow.zip']
Set-Location -Path "c:\temp"
fileslst = Get-children
$arrlst = [filelist]

Running the below will get you what you are after.运行下面的代码会让你得到你想要的。

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

You need to do Select -ExpandProperty Name to ensure that the only result from the Get-ChildItem is the filename including extension ( Name ).您需要执行Select -ExpandProperty Name以确保Get-ChildItem的唯一结果是包含扩展名 ( Name ) 的文件名。

$scriptpath='c:\temp'
$fileNames = Get-ChildItem -File -Path $scriptPath -Recurse -Include *.zip | Select -ExpandProperty Name

foreach ($vinodh in $fileNames)
{
write-host $vinodh

}

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

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