Get-Date.AddDays advice?

by jim.w.armstrong at 2012-10-23 21:10:46

Trying to move any http log file older than 30 days. The script is ran from the web server.

This works, moves any log file older than 5 days.
dir d:\logfiles\website\w3svc11 | where {$.creationtime -lt (Get-Date).AddDays(-5)} | copy -dest \servername\e$\logfiles\website_log_archive\2012

This doesn’t work. It don’t throw an err msg, it returns to the cmd line. Check the folders and the files haven’t been moved. Should this work the way it is?
dir d:\logfiles\website\w3svc11 | where {$
.creationtime -lt (Get-Date).AddDays(-30)} | copy -dest \servername\e$\logfiles\website_log_archive\2012

I’ve run the scripts from within the Powershell shell. From the DOS shell. From the Search Program and Files window input field.

I’ve searched the web and what I find is most examples use variables with the Get-Date cmd and your adding or subtracting using numbers so -5 or -30 shouldn’t make difference. But one works and the other don’t.
A forum member helped me out with this script from an earlier thread and it seems less complicated without the use of the variables so I prefer it.
by coderaven at 2012-10-23 21:29:40
In my test it worked as it should. Try making sure you Get-date is fully enclosed like this - ((Get-Date).AddDays(-30)). Also I would remove your copy statement and test that you get the desired results before putting it all together. I will keep giving it a look.
by Makovec at 2012-10-24 01:29:26
Hi,
It should work for both. Just to ensure you have files that are possibly going to move, try this:
dir | Format-Table Name, CreationTime, @{l=‘Diff’;e={(New-TimeSpan (Get-Date) $.CreationTime).Days}} -Auto
It will list file name, creation time and day difference. If you’ll not see anything bigger than -30, you have no files to meet your criteria.

David
by jim.w.armstrong at 2012-10-24 14:54:33
Thanks for the script it’s very helpful. It’s pinpointed my problem. Moving and copying the files around while tinkering with the script changed the file creation date. I’ve tested it successfully with some files that are older than 30 days. I’ve got the script setup to launch from Task Scheduler later this evening.

[quote=“Makovec”]Hi,
It should work for both. Just to ensure you have files that are possibly going to move, try this:
dir | Format-Table Name, CreationTime, @{l=‘Diff’;e={(New-TimeSpan (Get-Date) $
.CreationTime).Days}} -Auto
It will list file name, creation time and day difference. If you’ll not see anything bigger than -30, you have no files to meet your criteria.

Thanks for the hint about ensuring Get-Date is fully enclosed.

David[/quote][quote=“coderaven”]In my test it worked as it should. Try making sure you Get-date is fully enclosed like this - ((Get-Date).AddDays(-30)). Also I would remove your copy statement and test that you get the desired results before putting it all together. I will keep giving it a look.[/quote]

I’ll get back to you guys tomorrow. I want to see if it works from Task Scheduler overnight.
by jim.w.armstrong at 2012-10-25 19:43:49
Thanks again to both of you.