0

My goal is to create a PS script that does the following:

a) scans a source directory and produces a list of files that have a matching pattern that I pass in found inside the files b) take the list of those files and Copy-Item to move and then archive it.

I have this process working for when I "-Filter" on a filename, but can't seem to get my script to work when using "Select-String -pattern". When it gets to the "$FileNames = @($Files | %{$_.Path.Substring($Source.Length)})" part of the code, it says file does not exist as its passing in the @{Path} code?

View of error

If ({$PatternIdentifier -ne "" -and $FileIdentifier -eq "" -and $FileExtension -ne ""})
{
$Files = get-childitem $Source -Filter $FileExtension | Select-String -pattern $PatternIdentifier -SimpleMatch |Select Path
}

    $FileNames = @($Files | %{$_.Path.Substring($Source.Length)})

    if($Files.Count -ne 0)
        {
          if ((test-path $ArchiveDestination) -eq 0)
                {
                     New-Item -ItemType Directory -Force -Path $ArchiveDestination
                }


            foreach ($File in $Files)
                {
                    Copy-Item $File -Destination $DestinationFolder
                    Copy-Item $File -Destination $ArchiveDestination
                    $count++
                }
                        if($error.length -lt 0)
                            {
                                Write-Host ("Copied {0} files!!" -f $count)
                                $answer = $FilesTotalCount -$count
                            }
        }
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
  • as `js2010` pointed out, you need to get the _value_ in the property. [*grin*] your `Select Path` is creating a new object with one property - `Path` - that contains the value that you actually want. the usual way to get that info is as `js2010` shows - use the `-ExpandProperty` parameter. you can also get the value of one property from the previous call by wrapping the call in `()` and adding `.Path` to get the value in the property. – Lee_Dailey Dec 24 '19 at 03:44

1 Answers1

0

Get the value from the property:

|Select -expand path
js2010
  • 23,033
  • 6
  • 64
  • 66