Powershell Copy-Item based on type and date.

Bob007

Prince Among Men
Joined
Dec 22, 2003
Messages
585
Code:
$A = Get-Date
$B = $A.Date
$Date = $B.AddDays(-1)
Get-childitem -path C:\Scripts\ -Recurse -File include *.xml | Where-Object {$_.LasWriteTime -gt $Date} | ForEach-Object {Copy-Item $_.fullName -Destination D:\TestFolderXML\}

Above is an extract from a PS script am working on that will copy xml and wav files created in a single day to a target folder ready for conversion.

The above extract should work independently of everything else. Its ment to get todays date ($A) reset the clock to 00:00:00 ($B) then take a day off (Date). After that it lists all the XML files in a folder and its sub folders (I must point out it works up to here) then for each of them checks it against the date for yesterday and if its lastwritetime is greater (-gt) it copies the files over to the destination.

Except it don't. Am not sure why.

Sugestions ? :)
 

Bob007

Prince Among Men
Joined
Dec 22, 2003
Messages
585
In An effort to tidy up my problem, I rewrote this script out as a single script. Still don't work. might make more sense.
Code:
foreach ($XML in Get-childitem -Path C:\Scripts\ -Recurse -File -include *.xml)
    {
        if ($XML_.LasWriteTime -gt (Get-Date).AddDays(-1))
        {
        Copy-Item $XML -Destination D:\TestFolderXML\
        }
    }
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
I don't know powershell, but there's a typo in the if statement, should it not be $XML rather than $XML_
 

Bob007

Prince Among Men
Joined
Dec 22, 2003
Messages
585
Cheers jingleBells. Made me look at it again. I was over complicating it.

Code:
Get-ChildItem -path C:\Scripts\ -Recurse -File -include *.xml | where-object {$_.lastwritetime -gt (Get-Date).Adddays(-1)} | Copy-Item -destination D:\TestFolderXML\

Works a treat. :)
 

Users who are viewing this thread

Top Bottom