private/Get-AutFieldFromFileInfo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Tool script used to parse a FileInfo to extract fields from naming convention
 
.DESCRIPTION
See documentation for more information : https://github.com/Fleid/asa.unittest
 
.PARAMETER thisFileInfo
FileInfo to be processed
 
.PARAMETER fieldSeparator
Separator used in the naming convention
 
.PARAMETER numberOfFields
Number of fields to be extracted
 
.EXAMPLE
(Get-ChildItem -Path $arrangePath -File) | Get-AutFieldFromFileInfo
#>


Function Get-AutFieldFromFileInfo{
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true)]
        [System.IO.FileInfo]$thisFileInfo,
        [string]$separatorOfFields,
        [int]$numberOfFields
    )

    BEGIN {}

    PROCESS {
        $Fields = New-Object PSObject -Property @{
            FullName = $thisFileInfo.Name
            FilePath = $thisFileInfo.Fullname
            Basename = $thisFileInfo.Basename
        }

        $basenameParts = $thisFileInfo.Basename.Split($separatorOfFields)

        for (($i=0);($i -lt $numberOfFields);$i++){
            $Fields | Add-Member Noteproperty "basename$fieldSeparator$i" $basenameParts[$i]
        }

        $Fields
    }
}