locked
Copy Files and Folders RRS feed

  • Question

  • I'm trying to copy items from a source into a destination, but I need it to bring the folders and files as well. The code that I have only copies the files in the folder and not the folders.  I only want to bring back .txt files in the folders.

    Get-ChildItem-Path"C:\Source"-Recurse|

    Where{$_.FullName -Like'*.txt*'} |

    %{Copy-Item$_.FullName -Destination"C:\Destination"}

    Tuesday, August 4, 2015 3:00 PM

Answers

  • You are not optioning it correctly.

    If you want to hide the result then wrap it in a batch file and parse the exit code and "exit 0"


    \_(ツ)_/

    • Proposed as answer by Wizend Friday, August 14, 2015 12:12 PM
    • Marked as answer by Elaine Jing Tuesday, September 8, 2015 1:51 AM
    Tuesday, August 4, 2015 5:58 PM

All replies

  • Try

    Copy-Item -Path C:\Source\* -Destination C:\Destination -Include *.txt -Recurse -Container


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by clayman2 Tuesday, August 4, 2015 3:04 PM typo
    Tuesday, August 4, 2015 3:03 PM
  • That only returned the .txt file that was outside the subfolders.
    Tuesday, August 4, 2015 3:13 PM
  • Try adding a slash after C:\Destination\, I've had this issue before, just can't remember off the top of my head

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, August 4, 2015 3:15 PM
  • In one of my scripts I have

    Copy-Item "$($deployed.file)\*" $componentsPath -Recurse -Container

    Which copies the directory structure and places the files in the correct location. Maybe removing the -Include *.txt will help, but then it will copy everything from the source


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, August 4, 2015 3:26 PM
  • This is what RoboCopy does best.


    \_(ツ)_/

    Tuesday, August 4, 2015 4:18 PM
  • Yea I was using Robocopy to copy some SQL Backups, but I was running the command through a SQL Agent Job and the Robocopy was excluding some folders so the exit code it gives you is a 3 or a 1.  It bounces between the two.  The SQL Agent Job step is a CMD Job Step so it expects a 0 exit code.  However I changed it to say 3, but it sometimes Robocopy exits with exit code 1 as well, so I get an error.
    Tuesday, August 4, 2015 5:45 PM
  • You are not optioning it correctly.

    If you want to hide the result then wrap it in a batch file and parse the exit code and "exit 0"


    \_(ツ)_/

    • Proposed as answer by Wizend Friday, August 14, 2015 12:12 PM
    • Marked as answer by Elaine Jing Tuesday, September 8, 2015 1:51 AM
    Tuesday, August 4, 2015 5:58 PM