Windows – Use robocopy to only copy files not folders

powershellrobocopywindows

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?

Best Answer

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.