How to copy certain files (w/o folder hierarchy), but do not overwrite existing files?

后端 未结 5 2060
孤街浪徒
孤街浪徒 2020-12-08 09:03

I need to copy all *.doc files (but not folders whose names match *.doc) from a network folder \\\\server\\source (including files in

5条回答
  •  星月不相逢
    2020-12-08 09:19

    The previous answers seem rather overcomplicated to me, unless I'm misunderstanding something. This should work:

    Get-ChildItem "\\server\source\" *.doc -Recurse | ?{-not ($_.PSIsContainer -or (Test-Path "C:\Destination\$_"))} | Copy-Item -Destination "C:\Destination"
    

    None of the built-in commands - copy, xcopy, or robocopy - will do what you want on their own, but there's a utility called xxcopy that will, conveniently available at http://www.xxcopy.com. It has a number of built-in options specifically for flattening directory trees into a single directory. The following will do what you described:

    xxcopy "\\server\source\*.doc" "C:\Destination" /SGFO
    

    However, xxcopy has various other options for handling duplicate filenames than just copying the first one encountered, such as adding the source directory name to the filename, or adding sequential numerical identifies to all but the first one, or all but the newest or oldest. See this page for details: http://www.xxcopy.com/xxcopy16.htm

提交回复
热议问题