如何复制某些文件(w / o文件夹层次结构),但不覆盖现有文件? [英] How to copy certain files (w/o folder hierarchy), but do not overwrite existing files?
问题描述
我需要复制所有 *。doc
文件(但不包括名称匹配 *。doc
的文件)网络文件夹 \\server\source
(包括所有嵌套文件夹中的文件)到本地文件夹 C:\destination
,而不保留嵌套的文件夹层次结构(即所有文件应直接进入 C:\ destination
,并且不应在 C:\ destination
)。如果来自 \\server\source
的不同子文件夹中存在多个具有相同名称的文件,则只应复制第一个文件,而不会覆盖此文件 - 所有冲突应该跳过稍后发现的文件(可能有很多这样的情况,并且跳过的文件不应该通过网络传播,否则会花费太多时间)。这是我试图在PowerShell中实现它:
I need to copy all *.doc
files (but not folders whose names match *.doc
) from a network folder \\server\source
(including files in all nested folders) to a local folder C:\destination
without preserving the nested folders hierarchy (i.e. all files should go directly into C:\destination
and no nested folders should be created in C:\destination
). In case there are several files with the same name from different subfolders of \\server\source
, only the first one should be copied and never overwritten then — all conflicting files found later should be skipped (there could be many cases like this, and the skipped files should not be trasferred over the network, otherwise it would take too much time). Here is my attempt to implement it in PowerShell:
cp \\server\source\* -Recurse -Include *.doc -Container:$false -Destination C:\destination
至少有两个问题使用此命令:
There are at least two problems with this command:
- 它复制名称匹配
*。doc
的文件夹。 - 如果发生冲突,以后发现的任何文件都会通过网络传输,并覆盖之前的文件。
- It copies folders whose names match
*.doc
too. - In case of conflicting names any file found later is transferred over the network and overwrites the previous one.
您可以建议如何解决这些问题吗?
使用 copy的实现
, xcopy
, robocopy
, cscript
或 *。bat
code> *。cmd 。
本地操作系统是
Can you suggest how to fix these problems?
Implementations using copy
, xcopy
, robocopy
, cscript
or *.bat
, *.cmd
are also welcome.
The local OS is Windows 8 and the file system is NTFS.
推荐答案
我会首先生成文件列表,并在通过列表时验证。
I would produce the list of files first and validate as you go through the list.
像这样:
$srcdir = "\\server\source\";
$destdir = "C:\destination\";
$files = (Get-ChildItem $SrcDir -recurse -filter *.doc | where-object {-not ($_.PSIsContainer)});
$files|foreach($_){
if (!([system.io.file]::Exists($destdir+$_.name))){
cp $_.Fullname ($destdir+$_.name)
};
}
所以,使用 Get-ChildItem
以列出与过滤器匹配的源文件夹中的文件,通过 where-object
管道目录。
So, use Get-ChildItem
to list files in source folder matching the filter, pipe through where-object
to strip directories out.
然后遍历 foreach
循环中的每个文件,并使用 Exists $ c $检查目标中是否存在文件名(非Fullname) c> c $
system.io.file
方法。
Then go through each file in a foreach
loop and check if the filename (not Fullname) exists in the destination using the Exists
method of the system.io.file
.NET class.
如果没有,请仅使用原始文件名(放弃原始路径)进行复制。
If it doesn't, copy, using only original filename (dropping original path).
-whatif
选项,所以它只显示它会做什么,如果结果不是你想要的:-)
Use the -whatif
option on the copy when testing, so it only displays what it would do, in case result is not what you wanted :-)
这篇关于如何复制某些文件(w / o文件夹层次结构),但不覆盖现有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!