Powershell populate array from list of files -
i need populate powershell array filenames of files stored in directory based on names. need different array each set of files (see example folder structure) used part of process.
i automatic , create arrays on fly, have arrays containing filenames starting processor123
, processorabc
, processorxyz
. names of these arrays can stored in array named $arrynames
. needs dynamic in case new processor introduced, not want rely on users enter 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
processor123 processorabc processorxyz
$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
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, you'll want each string:
- extract first part of file name
- put entire string array matching first part
as mentioned in comments, can use hashtable hold arrays, file prefix being key:
# set hashtable $hashtable = @{} # pipe list of file paths foreach-object get-content c:\filenames.txt |foreach-object { # extract filename [system.io.path]::getfilename(), split dots , grab part before first dot $prefix = [system.io.path]::getfilename($_) -split "\." |select-object -first 1 # use extracted prefix place string in correct hashtable entry $hashtable[$prefix] += $_ }
now, can reference first 5 paths with: $hashtable["processor123"]
Comments
Post a Comment