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

  • Security Permissions for simple file transfer

    Hey All I'm transferring a file using RMI as part of an enhancement. I want to restrict where the file can be transferred to and thus will use a security manager (On the destination object). However the object its being transferred to shares the same

  • Speed of an SQL query?

    How can we see how long our SQL query took to execute?

  • Export data without commas

    Hi! I've got Oracle 10.2.0.3 and SQLDeveloper 1.5.1.54.40 NLS_LANG = RUSSIAN_CIS.CL8MSWIN1251; What I did: CREATE TABLE B (     ID NUMBER,      CNT NUMBER Insert into B (ID,CNT) values (1,1); Insert into B (ID,CNT) values (1,3); After that I've made

  • Problems recording Adobe Acrobat 9 with Captivate 4

    I am trying to put together an elearning lesson on Adobe Acrobat 9, using Captivate 4. I can record the screens just fine, but Captivate does not create ANY highlight boxes or click boxes. Just to make sure I wasn't losing my mind, or had my recordin

  • Query with additional condition

    select * from (select  b.account, f.company from account a, company f                             where f.company=b.company                              and f.company_type='BIZ') where rownum<=10 So, these are two tables I am drawing data from. I als