none
Copy items to different folder based on Text file

    Question

  • $List = gc 'C:\scripts\Lydia\Lydia Move.txt'
    $Source = "C:\Temp\Alexander"
    $Destination = "C:\Temp\Alexander\Test" 
    
    foreach ($item in $list) {
    
    if (Test-Path $source\$item) 
    {Get-ChildItem -Path $Source |Copy-Item -Destination $Destination -Force -Recurse -PassThru -Container
    "$([DateTime]::Now)" + "`t$source\$item`t is copied onto $destination"| out-file C:\scripts\Lydia\Log.txt -Append}
    else  
    {Write-Host "$item is missing from $source. Rest is moved"}
    
    }
    In the text file, there is only the name+extenstion of the file like picture.jpg. It moves the all the content over. I need to just move some files based from the text file, and keep the folder structure. Any tips?
    Tuesday, February 6, 2018 8:44 AM

All replies

  • For copy jobs use robocopy. It's made for. You can specify source, target and file pattern. It creates target folder structure and it creates log files for you as well. No need to invent the wheel over and over.  ;-)

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 6, 2018 8:54 AM
  • Sounds good, but am not familiar with robocopy. How would it work to just copy spesific files with robocopy?
    Tuesday, February 6, 2018 9:02 AM
  • Can't seem to find the recurse function for robocopy. How to make it look through subfolders too?
    Tuesday, February 6, 2018 9:28 AM
  • robocopy /?
    has everything you need.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 6, 2018 9:41 AM
  • As an example:

    robocopy <sourceDIR> <destDIR> /MIR

    mirrors the whole sourceDIR into the destDIR

    Tuesday, February 6, 2018 10:06 AM
  • Hi,

    >> Get-ChildItem -Path $Source |Copy-Item -Destination $Destination -Force -Recurse -PassThru -Container
    I suppose this line is copying all the items under "C:\Temp\Alexander" to the destination. Please have a try with the following script and see if it works:
    $list = Get-Content -Path 'C:\scripts\Lydia\Lydia Move.txt'
    $source = 'C:\Temp\Alexander'
    $destination = 'C:\Temp\Alexander\Test'
    $logFile = 'C:\scripts\Lydia\Log.txt'
    
    foreach ($item in $list) {
        $sourcePath = Join-Path -Path $source -ChildPath $item
        if (Test-Path -Path $sourcePath) {
            Copy-Item -Path $sourcePath -Destination $destination -Force
            "$([DateTime]::Now)" + "`t$source\$item`t is copied onto $destination" | Out-File -FilePath $logFile -Append
        }
        else {
            Write-Warning -Message "$item is missing from $source. Rest is moved"
        }
    }

    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

    Wednesday, February 7, 2018 6:06 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    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, February 9, 2018 8:51 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    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

    Tuesday, February 13, 2018 9:09 AM