File copy based on date - noob

by jim.w.armstrong at 2012-10-11 12:24:53

Win7 Pro SP1, Powershell 3.0.

Would someone point out why this might not work? I have three files named u_ex120222.log, u_ex120223.log, u_ex120224.log, that I copied into c:\test1 yesterday. The files modified dates correspond with their names.
I copied and pasted this code into notepad and saved it as movefilestest2.ps1. No err msg’s are thrown but the files are not copied into the c:\test2 dir.

PS C:\myscripts> powershell.exe "c:\myscripts\movefilestest2a.ps1"
PS C:\myscripts> dir c:\test2
PS C:\myscripts>


foreach ($i in Get-ChildItem C:\test1)
{
if ($i.CreationTime -lt ($(Get-Date).Adddays(-1)))
{
Copy-Item $i.FullName C:\Test2
}
}

I got the code from this link. Modified the dir names and changed addmonths to adddays.

http://technet.microsoft.com/en-us/libr … 76988.aspx
by bobsdesk at 2012-10-11 12:37:30
I think you want LastWriteTime not CreationTime
by JeffH at 2012-10-11 12:41:17
Are you trying to find files created since before yesterday or since yesterday? In any event, you need to start thinking about objects. You are sort of there, but still taking a text parsing approach.

dir c:\work | where {$.creationtime -lt (Get-Date).AddDays(-1)}

This gets all files in C:\work that have a creation time earlier than yesterday. If these are the files, then pipe this to copy-item

dir c:\work | where {$
.creationtime -lt (Get-Date).AddDays(-1)} | copy -dest g:\temp -whatif

-Whatif gives you a nice sanity check and confirmation.
by JeffH at 2012-10-11 12:42:08
My same technique applies regardless of which property you choose.
by jim.w.armstrong at 2012-10-11 16:01:45
[quote="JeffH"]Are you trying to find files created since before yesterday or since yesterday? In any event, you need to start thinking about objects. You are sort of there, but still taking a text parsing approach.

dir c:\work | where {$.creationtime -lt (Get-Date).AddDays(-1)}

This gets all files in C:\work that have a creation time earlier than yesterday. If these are the files, then pipe this to copy-item

dir c:\work | where {$
.creationtime -lt (Get-Date).AddDays(-1)} | copy -dest g:\temp -whatif

-Whatif gives you a nice sanity check and confirmation.[/quote]

Yes, I’m wanting to copy any log file not created today into an archive folder. Your line of code works great. Simple, elegant, no bs, it just works, just too cool, so many thanks. Now to take it a step further. I want to copy from the web server to a server we have setup to archive log files. After that I want an email sent to me so I know the job ran.

I’m going to give this a go in the morning. If the syntax is hosed up clue me in.

dir \webservername\sharename | where {$.creationtime -lt (Get-Date).AddDays(-1)} | copy -dest \archiveserver\sharename -whatif

I found this code this afternoon and it does work. But it seems so cluttered compared to your code. For noobs this adds to the confusion. The code I asked you to critique that came from the MS Scripting guys. I’d still like to understand why it don’t work for me.

$path ="c:\test1"
$recievingPath="c:\test2"
$DateToCompare = (Get-date).AddMonths(-15)
Get-ChildItem $path -Recurse -Include *.log | where-object {$
.lastwritetime –gt $DateToCompare} |Copy-Item -Destination $recievingPath
Get-ChildItem $recievingPath -Recurse -Include *.log | rename-item -NewName {$_.name.replace(".log","ARCHIVE.log")}
by jim.w.armstrong at 2012-10-11 16:04:56
[quote="bobsdesk"]I think you want LastWriteTime not CreationTime[/quote]

Thanks for taking the time clue me in about LastWriteTime. I appreciate it. I’ll dig into that cmd a little deeper too.
by JeffH at 2012-10-11 17:02:19
The example you found is using variables to basically do the same thing I suggested. Although it also looks like that code is renaming the files after they are moved. It’s not the most efficient code but I wouldn’t get distracted by it.
by jim.w.armstrong at 2012-10-12 07:04:59
[quote="JeffH"]The example you found is using variables to basically do the same thing I suggested. Although it also looks like that code is renaming the files after they are moved. It’s not the most efficient code but I wouldn’t get distracted by it.[/quote]

Always more than one way to do these things. Thanks again.