Robocopy matching files in subdirectories

45 views Asked by At

I'm trying to write a robocopy script which will copy all error files, regardless of what subfolder they are in, to one location.

I've done robocopy "C:\ProgramData\Microsoft" "D:\ErrorLogs" *.err /s, but that also copies the subfolder over.

I want all of the .err files to be in D:\ErrorLogs, regardless of whether they were originally in a subfolder or not.

Is that possible?

1

There are 1 answers

0
lit On

I am not sure that Robocopy.exe will ignore subdirectory paths. There will likely be directories and/or file that cannot be accessed, so -ErrorAction can be used.

Get-ChildItem -Recurse -Path "${Env:PROGRAMDATA}\Microsoft" -Filter '*.err' -ErrorAction SilentlyContinue |
    Copy-Item -Destination 'D:\ErrorLogs'