March 10, 2014 at 6:54 pm
Hi
I am trying to copy a multiple files the have the current date as the creation date using a wild card but can't seem to get the syntax correct. Any help would be appreciated.
Copy-item "D:\Solomon\Solomon IV\EventLog\DD500*.log" (CreationTime -eq (Get-Date)) ("C:\Temp\Emaillogs")
March 11, 2014 at 4:38 am
Here is how I built it up:
#Loop through each file displaying it's creation time
Foreach($f in Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log")
{
Write-Host $f.CreationTime
}
#Loop through each file displaying it's creation time (only for today)
Foreach($f in Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log")
{
if($f.CreationTime.Date -eq (Get-Date).Date) { Write-Host $f.CreationTime }
}
#Loop through each file copying it (only for today)
Foreach($f in Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log")
{
if($f.CreationTime.Date -eq (Get-Date).Date)
{
Copy-Item $f.FullName -Destination "C:\Temp\Emaillogs"
}
}
Optimisation 1 for me would be to assign (Get-Date).Date to a variable so that the last one would be:
#Loop through each file copying it (only for today)
$todaysDate = (Get-Date).Date
Foreach($f in Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log")
{
if($f.CreationTime.Date -eq $todaysDate)
{
Copy-Item $f.FullName -Destination "C:\Temp\Emaillogs"
}
}
I am sure that there are more elegant and efficient ways to do this (particularly by piping results) so watch out for others' posts!!!
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 11, 2014 at 8:38 am
Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log"| Where-Object {$_.CreationTime -gt (Get-Date).Date} | Copy-Item -Destination "C:\Temp\Emailogs"
March 11, 2014 at 9:00 am
bruce 1565 (3/11/2014)
Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log"| Where-Object {$_.CreationTime -gt (Get-Date).Date} | Copy-Item -Destination "C:\Temp\Emailogs"
There you go. Bruce 1565 has provided a far more elegant solution.
I still prefer "_.CreationTime.Date -eq" over ".CreationTime -gt" but that is a question of taste (different, not necessarily better).
Also, if eeking out ever last drop of performance is essential in your scenario the optimisation of calculating todays' date once is still valid. Otherwise, it is fine how it is.
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 11, 2014 at 9:04 am
If you want to avoid PoSH altogether...
http://technet.microsoft.com/en-us/library/cc753551.aspx
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2014 at 9:20 am
Jeff has a great point and it shouldn't be an issue but please note:
Applies To: Windows 8, Windows Server 2008, Windows Server 2012, Windows Vista
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 11, 2014 at 9:33 am
Gary Varga (3/11/2014)
Jeff has a great point and it shouldn't be an issue but please note:Applies To: Windows 8, Windows Server 2008, Windows Server 2012, Windows Vista
That's what they say. It works on my Windows 7 box, as well. It thought it worked on XP Pro but I was mistaken.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2014 at 9:52 am
Jeff Moden (3/11/2014)
Gary Varga (3/11/2014)
Jeff has a great point and it shouldn't be an issue but please note:Applies To: Windows 8, Windows Server 2008, Windows Server 2012, Windows Vista
That's what they say. It works on my Windows 7 box, as well. It thought it worked on XP Pro but I was mistaken.
Technically, it might be there but, as you say, not open to the command line by default. I wouldn't be surprised if you could locate it in a DLL as sometimes these things get surfaced in an update, service pack or even a version later.
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 11, 2014 at 1:30 pm
I have to thank everyone so much. These are all good solutions and I have learned a few thing. One last thing...Any suggestions on articles or powershell script libraries you guys know of that will help my powershell vocabulary?
Doug
March 12, 2014 at 3:39 am
Steve Jones (you may have heard of him ;-)) mentioned that there is book called Learn Windows PowerShell 3 in a Month of Lunches[/url] which, if I recall correctly, some people praised.
Aside from that I tend to do the opposite of what you should do in real life i.e. try and use PowerShell to achieve everything (within reason). My thinking is that practice and discovery in this way will improve your skills. I am certainly NOT advocating employing PowerShell for every task in production, however, any task that can be achieved in a non-production environment will aid your learning even if there is a more appropriate technology or method. In fact recognising that something else would have been a better solution has value too!!!
Also I tend to search for specific solutions to particular problems which I build up myself (just through standard search engine usage).
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply