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 08 '18 at 11:53
  • 2
    When flattening a folder structure, what about duplicate file names from different sources? – LotPings Dec 08 '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 08 '18 at 13:48
  • @LotPings There aren't duplicate file names. – Phillip Duphnee Dec 08 '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 08 '18 at 14:07
  • Not posted as an answer because I don't know exactly how to do this in powershell but it should be almost trivial to iterate through the directory tree and run, e.g., `robocopy "c:\1\2\3" "d:\target" /copyall` for each source directory, just leave out the `/s` option. – Harry Johnston Dec 08 '18 at 19:27
  • Possibly useful: [long path support in Powershell](https://win32.io/posts/Long-Path-Support-In-Powershell). – Harry Johnston Sep 06 '19 at 23:48

1 Answers1

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.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79