复制文件,包括其相对路径
我需要将大量文件复制到备份文件夹,但我想保留它们的相对路径。我只需要特定的文件;即
C:\scripts\folder\File.ext1
C:\scripts\folder2\file2.ext2
C:\scripts\file3.ext1
但我只需要像这样复制 ext1 文件:
C:\backup\folder\File.ext1.bak
C:\backup\file3.ext1.bak
源路径有多个深度。 这就是我必须复制文件的内容:
$files = gci -path C:\scripts\ -recurse -include *.ext1
$files | % { Copy-Item $_ "$($_).bak"; move-item $_ -destination C:\backup\ }
这只是将所有文件转储到 C:\backup\ 中,并且似乎没有获取任何路径。不确定该部分将如何完成。
I need to copy a large number of files to a backup folder but I want to maintain their relative paths. I only need specific files; i.e.
C:\scripts\folder\File.ext1
C:\scripts\folder2\file2.ext2
C:\scripts\file3.ext1
But I only need to copy the ext1 files like so:
C:\backup\folder\File.ext1.bak
C:\backup\file3.ext1.bak
The source paths are of multiple depths.
This is what I have to copy the files:
$files = gci -path C:\scripts\ -recurse -include *.ext1
$files | % { Copy-Item $_ "$($_).bak"; move-item $_ -destination C:\backup\ }
This just dumps all the files into C:\backup\ and does not appear to get any of the paths. Not sure how that part would be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西可能会起作用:
它并不聪明,但它很快而且很容易。脏,而且不需要太多努力就能工作。
Something like this could work:
It is not clever, but it's quick & dirty and works without a lot of effort.
get-childitem 返回绝对路径,但您可以使它们相对于当前工作目录,如下所示:
因此,要将经过过滤的文件集从当前目录递归复制到目标目录:
需要 new-item 来创建父目录
get -item 提供它创建的所有新文件的显示
当然 robocopy 可以完成所有这些,但有时您想要进行更特殊的过滤或文件名修改...
get-childitem returns absolute paths, but you can make them relative to the current working directory as follows:
So to copy a filtered set of files from the current directory recursively to a destination directory:
new-item is needed to create the parent directories
get-item provides a display of all the new files it created
Of course robocopy does all this, but there will be times when you want to do more special filtering or filename mangling...
使用机器人复制。
哎呀。我没有注意到您也想添加 .bak 扩展名。我仍然认为使用 robocopy 复制文件是个好主意:
Use robocopy.
Oops. I failed to notice you wanted to add the .bak extension too. I still think it is a good idea to use robocopy to copy the files then:
您可以尝试使用
新项目来强制创建路径。
让·保罗
You can try this
the new-item is there in order to force path creation.
Jean Paul