locked
Is there a way to "Copy-Item" to New-Item -type Directory? RRS feed

  • Question

  • Hi,

    A little more info as follows;

    I need copy 4 x *.7z files from Location1 to a sub folder of Location2. The supb folder is created daily based on the current date in "yyyy-MM-dd" format.

    I am using New-Item -ItemType Directory -Path "Drive:\Folder1\Folder2\$((Get-Date).ToString('yyyy-MM-dd'))"to create the folder and I can use the Copy-Item cmdlet to move the files in separate ps1 scripts, but I can't get them to work from the same ps1 script.

    I am sure I have done this before though... can anybody help?

    Thanks

    Rob

    Monday, September 12, 2016 5:11 PM

Answers

  • $Folder = New-Item -ItemType Directory -Path "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"
    Get-ChildItem 'D:\TextFiles' '*.txt'| Copy-Item -Destination $Folder
    

    New-Item -ItemType Directory -Path "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"
    Get-ChildItem 'D:\TextFiles' '*.txt'| Copy-Item -Destination "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"

    • Marked as answer by Rob Dunphy Tuesday, September 20, 2016 2:21 PM
    Monday, September 12, 2016 5:27 PM

All replies

  • $Folder = New-Item -ItemType Directory -Path "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"
    Get-ChildItem 'D:\TextFiles' '*.txt'| Copy-Item -Destination $Folder
    

    New-Item -ItemType Directory -Path "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"
    Get-ChildItem 'D:\TextFiles' '*.txt'| Copy-Item -Destination "D:\Testing\$((Get-Date).ToString('yyyy-MM-dd'))"

    • Marked as answer by Rob Dunphy Tuesday, September 20, 2016 2:21 PM
    Monday, September 12, 2016 5:27 PM
  • Hi Rob,

    What happens when you try running this all together in one script? Should be easily doable.


    Monday, September 12, 2016 5:29 PM
  • Figured why this wasnt working. Sometime the BAK file is created just before midnight, and the script runs just after, so the file destiniation is different. Cannot believe I missed that...
    Tuesday, September 13, 2016 9:22 AM