Copy file to folder with current date

  • 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")

  • 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!!!

  • Get-ChildItem "D:\Solomon\Solomon IV\EventLog\DD500*.log"| Where-Object {$_.CreationTime -gt (Get-Date).Date} | Copy-Item -Destination "C:\Temp\Emailogs"

  • 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!!!

  • If you want to avoid PoSH altogether...

    http://technet.microsoft.com/en-us/library/cc753551.aspx

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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!!!

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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!!!

  • 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

  • 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