I want to count the elements in the pipeline and also process them

The following script works, but only because I save the pipeline contents in a variable.  I would like to not have to save it - see below. The commented section is my most recent failure. (And BTW I know the script has nothing to do with ZIP: that's
the next step.
function ZipUpModified {
    param ([string] $from, [string]$to, [datetime]$cutOff)
    try
        Write-Host ("Copying {0} modified on or after {1} to {2} in execution at {3}" -f $from, $cutOff.toString(), $to, $(Get-Date))
        if (-not (Test-Path -Path $from)) {Throw "Source folder $from does not exist"}
        if (Test-Path -Path $to) {Throw "Target folder $to already exists"}
# I would like to substitute the next 5 lines for the following 4; but I cannot seem to get the pipeline to reach the Copy_Item
#       $fileCount=0
#        Get-ChildItem $from -Recurse  |
#            Where-Object {(New-TimeSpan -Start  $_.LastWriteTime -End $cutoff).TotalHours -le 0} |
#            ForEach-Object {Copy-Item -Destination {Join-Path $to $_.FullName.Substring($from.length)}; $fileCount++; }
#        Write-Host ("Copied {0} files/folders successfully at {1}" -f $fileCount, $(Get-Date))
        $filelist=Get-ChildItem $from -Recurse  |
            Where-Object {(New-TimeSpan -Start  $_.LastWriteTime -End $cutoff).TotalHours -le 0}
        $filelist | Copy-Item -Destination {Join-Path $to $_.FullName.Substring($from.length)}
        Write-Host ("Copied {0} files/folders successfully at {1}" -f $filelist.count, $(Get-Date))
    catch
        Write-Host $($_.Exception.ToString() -replace ".*: ")
        Write-Host "so we are stopping..."
    finally
        $IgnoreThis = Read-Host "hit OK or Enter"
### Main
$cutOff= (Read-Host 'Enter date of most recent backup (mm/dd/yyyy):') | Get-Date
ZipUpModified "C:\Users\Jonathan\Desktop\Test_BU\SRC" "C:\Users\Jonathan\Desktop\Test_BU\TGT" $cutOff
## two more calls to ZipUpModified

Guys - Thanks for the quick response
Bill - I think you are recommending the approach I had in the submitted code.
David - the whole point is that I want to use the pipeline, not the Path argument, and I had no luck with your suggestion, which I took as:
        Get-ChildItem $from -Recurse  |
            Where-Object {(New-TimeSpan -Start  $_.LastWriteTime -End $cutoff).TotalHours -le 0} |
            ForEach-Object {Copy-Item -Path $_ -Destination {Join-Path $to $_.FullName.Substring($from.length)}; $fileCount++}
However, all this did make me go read more documentation, and there is a -PassThru argument to Copy-Item which creates pipeline output after the copy.  Specifically:
        $fileCount=0
        Get-ChildItem $from -Recurse  |
            Where-Object {(New-TimeSpan -Start  $_.LastWriteTime -End $cutoff).TotalHours -le 0} |
            Copy-Item -Destination {Join-Path $to $_.FullName.Substring($from.length)} -PassThru |
            ForEach-Object {$fileCount++}
        Write-Host ("Copied {0} files/folders successfully at {1}" -f $fileCount, $(Get-Date))
Thanks again
JonW
JonW

Similar Messages

Maybe you are looking for