简体   繁体   English

Powershell从文件列表填充数组

[英]Powershell populate array from list of files

I need to populate a powershell array with the filenames of files stored in a directory based on their names. 我需要使用基于文件名存储在目录中的文件名来填充powershell数组。 I need a different array for each set of files (see Example folder structure) to be used as part of another process. 对于每组文件(请参阅示例文件夹结构),我都需要一个不同的数组,以用作另一个过程的一部分。

I would like this to be automatic and create the arrays on the fly, so I would have arrays containing all the filenames starting with processor123 , another for processorabc and processorxyz . 我希望这是自动的并且可以动态创建数组,所以我将拥有包含所有文件名的数组,这些文件名以processor123开头,另一个为processorabcprocessorxyz The names of these arrays can be stored in another array named $arrynames . 这些数组的名称可以存储在另一个名为$arrynames数组中。 It needs to be dynamic in case a new processor is introduced, I do not want to rely on users to enter the file names. 如果引入了新处理器,它必须是动态的,我不想依靠用户来输入文件名。

Example folder structure; 示例文件夹结构;

c:\directory\processor123\processor123.log.20150604
c:\directory\processor123\processor123.log.20150603
c:\directory\processor123\processor123.log.20150602
c:\directory\processor123\processor123.log.20150601
c:\directory\processor123\processor123.log.20150531
c:\directory\processorabc.log.20150604
c:\directory\processorabc.log.20150603
c:\directory\processorabc.log.20150602
c:\directory\processorabc.log.20150601
c:\directory\processorabc.log.20150531
c:\directory\processorxyz.log.20150604
c:\directory\processorxyz.log.20150603
c:\directory\processorxyz.log.20150602
c:\directory\processorxyz.log.20150601
c:\directory\processorxyz.log.20150531

$ArryNames $ ArryNames

processor123
processorabc
processorxyz

$processor123 $ processor123

c:\directory\processor123\processor123.log.20150604
c:\directory\processor123\processor123.log.20150603
c:\directory\processor123\processor123.log.20150602
c:\directory\processor123\processor123.log.20150601
c:\directory\processor123\processor123.log.20150531

$processorabc $ processorabc

c:\directory\processorabc.log.20150604
c:\directory\processorabc.log.20150603
c:\directory\processorabc.log.20150602
c:\directory\processorabc.log.20150601
c:\directory\processorabc.log.20150531

So, what you'll want to do for each string: 因此,您想要对每个字符串执行以下操作:

  1. Extract first part of file name 提取文件名的第一部分
  2. Put entire string into array matching the first part 将整个字符串放入与第一部分匹配的数组中

As already mentioned in comments, you can use a hashtable to hold the arrays, with the file prefix being the key: 正如注释中已经提到的,您可以使用哈希表来保存数组,文件前缀是关键:

# Set up a hashtable
$HashTable = @{}

# Pipe your list of file paths to ForEach-Object
Get-Content C:\filenames.txt |ForEach-Object {
    # Extract the filename with [System.IO.Path]::GetFileName(), split it by dots and grab only the part before the first dot
    $Prefix = [System.IO.Path]::GetFileName($_) -split "\." |Select-Object -First 1
    # Use the extracted prefix to place the string in the correct hashtable entry
    $HashTable[$Prefix] += $_
}

Now, you can reference the first 5 paths with: $HashTable["processor123"] 现在,您可以使用以下命令引用前5个路径: $HashTable["processor123"]

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

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