none
using a text file to provide paths for copy-item RRS feed

  • Question

  • OK, please don't beat me over the head too much for this.  I've been trying various methods all day and nothing seems to be working.  I have a text file that has a list of folders (full paths including drive letters).  I want to copy all of the folders and their contents to another path.  The destination path does not change (d:\temp\destination) but each of the folders in the text file should end up being subfolders of the destination

    Sources:

    d:\source1 (has file01.txt, file02.txt)

    d:\source2 (has file03.txt, file04.txt)

    Destination

    d:\temp\destination

    Should end up with something like

    d:\temp\destination\source1\file01.txt

    d:\temp\destination\source1\file02.txt

    d:\temp\destination\source2\file03.txt

    d:\temp\destination\source2\file04.txt

    I've tried

    Copy-Item -Path D:\Temp\paths.txt -Destination D:\Temp\attachments -recurse -Force

    which just copies the file that contains my paths.

    I've tried

    foreach($line in (Get-Content -Path D:\Temp\paths.txt)){$dest_folder="D:\Temp\destination" | Copy-Item -path $line -destination $dest_folder -force -recurse}

    I get an error that says

    Copy-Item : The input object cannot be bound to any parameters for the command either because the command does not take
     pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

    Would anyone mind helping me out here?  If I need to create a file that has the source foldername as well as the destination path, I can do that, I just figured there has to be a way to specify a base directory and let powershell do its thing from there.

    Monday, November 17, 2014 12:41 AM

Answers

  • try

    $SourcesFile = ".\sources.txt"
    $Root = "d:\temp\destination"
    
    foreach ($Source in (Get-Content -Path $SourcesFile)) {
        $Destination = Join-Path -Path $Root -ChildPath (Split-Path -Path $Source -Leaf)
        Copy-Item -Path $Source -Destination $Destination -Recurse -Force 
    }


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    • Edited by Sam Boutros Monday, November 17, 2014 12:53 AM
    • Marked as answer by kynetic29 Monday, November 17, 2014 1:37 AM
    Monday, November 17, 2014 12:53 AM

All replies

  • try

    $SourcesFile = ".\sources.txt"
    $Root = "d:\temp\destination"
    
    foreach ($Source in (Get-Content -Path $SourcesFile)) {
        $Destination = Join-Path -Path $Root -ChildPath (Split-Path -Path $Source -Leaf)
        Copy-Item -Path $Source -Destination $Destination -Recurse -Force 
    }


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    • Edited by Sam Boutros Monday, November 17, 2014 12:53 AM
    • Marked as answer by kynetic29 Monday, November 17, 2014 1:37 AM
    Monday, November 17, 2014 12:53 AM
  • Sam,

    Thanks for the quick response.  I received the following error when I ran the script:

    Join-Path : Cannot bind parameter because parameter 'Path' is specified more than once. To provide multiple values to p
    arameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3".

    Monday, November 17, 2014 1:10 AM
  • copy and paste from above. You have an extra space..

    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    • Edited by Sam Boutros Monday, November 17, 2014 2:30 PM
    Monday, November 17, 2014 1:12 AM
  • Thank you.  I tried again and it worked wonderfully.  I need to go research split-path and -leaf to fully understand what you did.

    Thank you again.

    Monday, November 17, 2014 1:39 AM