locked
Move files that arrived in the folder today RRS feed

  • Question

  • Hi All,

    Completly new to PowerShell and scripting in general, I would like to have a ps script to move files n  a to folder b.  Files arrive in folder a daily and are stored there as an archive, each day I will have task scheduler run the ps script, the script will look for files that arrived in the folder with today's date and copy them to folder b.

    Here is what I have so far:

    $path = C:\Users\cb\Desktop\FolderA
    $Destination = C:\Users\cb\Desktop\FolderB
    Foreach($file in(Get-ChildItem $path))
    {
    	If( $file.LastWriteTime -ge (Get-Date).date)
    	{
    		Copy-Item -Path $file.fullname -Destination $Destination
    	}
    }

    I know if I use -g  and put -1, for example, I can get files that were modified one day earlier. How do I look for files that arrived in FolderA today?


    CuriousCoder




    Thursday, January 4, 2018 1:38 PM

Answers

  • Hi Friend,

    Please try below and check result

    $path = C:\Users\cb\Desktop\FolderA
    $Destination = C:\Users\cb\Desktop\FolderB
    Foreach($file in(Get-ChildItem $path))
    {
    	If( ($file.LastWriteTime).date -eq (Get-Date).date)
    	{
    		Copy-Item -Path $file.fullname -Destination $Destination
    	}
    }

    Please make sure to mark this as answer or vote me if you find this really useful.

    Regards

    Kam

    Have a nice day!!!

    Thursday, January 4, 2018 2:28 PM
  • Hi,

    Based on my research, I will give you the following recommendations. Hope it is helpful to you:
    1. We need to add single quotation marks for creating a string variable, for example:
    $path = 'C:\Users\cb\Desktop\FolderA'
    $Destination = 'C:\Users\cb\Desktop\FolderB'
    2. Based on your situation, CreationTime may be more suitable to look for files that arrived today.
    3. We can make the script done in one line by using Where-Object, for your reference:
    Get-ChildItem -Path C:\Users\cb\Desktop\FolderA | Where-Object {$_.CreationTime.Date -eq (Get-Date).Date} | Move-Item -Destination C:\Users\cb\Desktop\FolderB

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, January 5, 2018 6:46 AM

All replies

  • Hi Friend,

    Please try below and check result

    $path = C:\Users\cb\Desktop\FolderA
    $Destination = C:\Users\cb\Desktop\FolderB
    Foreach($file in(Get-ChildItem $path))
    {
    	If( ($file.LastWriteTime).date -eq (Get-Date).date)
    	{
    		Copy-Item -Path $file.fullname -Destination $Destination
    	}
    }

    Please make sure to mark this as answer or vote me if you find this really useful.

    Regards

    Kam

    Have a nice day!!!

    Thursday, January 4, 2018 2:28 PM
  • I put "-whatif" at the end of your copy-item and ran a test.  It worked for files from today.

    Have you tried something and it didn't work?

    Thursday, January 4, 2018 8:09 PM
  • Well, if you want to move instead of copying the files, you may want to consider replacing 'Copy-Item'  with 'Move-Item'. Other than that I can see no issue with your posted sample.

    There's no place like 127.0.0.1

    Thursday, January 4, 2018 9:31 PM
  • Hi,

    Based on my research, I will give you the following recommendations. Hope it is helpful to you:
    1. We need to add single quotation marks for creating a string variable, for example:
    $path = 'C:\Users\cb\Desktop\FolderA'
    $Destination = 'C:\Users\cb\Desktop\FolderB'
    2. Based on your situation, CreationTime may be more suitable to look for files that arrived today.
    3. We can make the script done in one line by using Where-Object, for your reference:
    Get-ChildItem -Path C:\Users\cb\Desktop\FolderA | Where-Object {$_.CreationTime.Date -eq (Get-Date).Date} | Move-Item -Destination C:\Users\cb\Desktop\FolderB

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, January 5, 2018 6:46 AM
  • In addition to Albert Ling's recommendations: Using single or double quotation marks would be equal in this case. They only deliver different results if a variable is used inside the string.

    Furthermore, you should use the possiblity to run selected lines (or parts of lines) of code with F8. This way, you can debug your script on your own. Simply look what different versions do, look what is inside your variables.

    Friday, January 5, 2018 7:42 AM