0

I am trying to write a script that copies all files in my source folder to another folder but without the folder structure, so just the files.

So far I have:

robocopy "<source>" "<target>" /s /copyall

Example: I have C:\1\2\3.txt and C:\4\5\6.jpg and in my target I only want D:\target\3.txt and D:\target\6.jpg

Any ideas?

  • 2
    Since you tagged this with Powershell: how about find-item|copy-item? – Gerald Schneider Dec 8 '18 at 11:53
  • 2
    When flattening a folder structure, what about duplicate file names from different sources? – LotPings Dec 8 '18 at 13:26
  • @GeraldSchneider unfortunately I can not use those because I need to use some specifications. I didn't mention so in my original post because I wanted to keep my question simple. I need to specify the maxage and filetypes of the files to copy. As far as I've seen that's not possible with find-item. – Phillip Duphnee Dec 8 '18 at 13:48
  • @LotPings There aren't duplicate file names. – Phillip Duphnee Dec 8 '18 at 13:49
  • 2
    Even if get-childitem (not find-item) does not provide parameters for this directly you can easily filter the result with where before piping it into copy – Gerald Schneider Dec 8 '18 at 14:07
4

As already stated in the comments, you can achieve this easily with Powershell:

Get-ChildItem -Recurse -File -Filter "*.txt" $source |
  Where-Object { $_.LastAccessTime -gt (get-date -date "2018-12-01 00:00:00") } |
  Copy-Item -Destination $target

This runs Get-ChildItem recursively, looking only for files matching the filter *.txt. Afterwards the result is filtered by the LastAccessTime attribute of the file and only files newer than date X are kept. The result of that is piped into Copy-Item.

Of course, you can also run robocopy at the end, but that becomes pretty redundant.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.