在powershell中保留文件夹结构的同时复制文件的正确方法是什么? [英] What's the proper way to copy files while preserving folder structure in powershell?

查看:846
本文介绍了在powershell中保留文件夹结构的同时复制文件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个现有的文件夹 c:\MyApps\Websites\MySite 已经有一个现有的网站。我已下载位于 c:\temp\MySite\artifacts 下的最新位。



当我尝试运行此

  $ sourcec:\temp\MySite
$ destination c:\MyApps\Websites\MySite
copy-item $ source $ destination -recurse -force

如果 c:\MyApps\Websites\MySite 已经存在,那么它会尝试将其放在 c:\ MyApps\Websites\MySite\artifacts ,但如果它不存在,它会正确复制。不知道这里发生了什么。任何建议?

解决方案

只需使用 robocopy 命令。



robocopy $ source $ destination / E



它随Windows 7一起提供,所以它是一个内部命令,所以它仍然是一个'Powershell'的方式,imho,但如果你想使用copy命令,那么你很接近,但是你当前的实现抓住 source 文件夹,并将它放在 target C:\target\source\files ,而不是 C:\target\files )。当你想使 target 的内部看起来像 source 里面时,你需要做:



cp C:\source\ * C:\target -r -fo



请注意 * ,它抓取内容 c>文件夹。如果你想让 target 先被清除,只要做 rm -r -fo C:\target \ *



Powershell不处理长路径,因此如果文件名较长或文件夹较深,则需要使用robocopy。 strong>


I can never seem to get this right.

I have an existing folder c:\MyApps\Websites\MySite that already has an existing website running in it. I've downloaded the latest bits which are located under c:\temp\MySite\artifacts.

when I try to run this

$source "c:\temp\MySite"
$destination "c:\MyApps\Websites\MySite"
copy-item $source $destination -recurse -force

if c:\MyApps\Websites\MySite already exists then it tries to put it in c:\MyApps\Websites\MySite\artifacts, but if it doesn't already exist it copies it properly. Not sure what's going on here. Any suggestions?

解决方案

Just use the robocopy command. It's designed for this kind of thing.

robocopy $source $destination /E

It's shipped with Windows 7, so it's an internal command, so it's still a 'Powershell' way, imho, but if you're wanting to use the copy command, then you are very close, but your current implementation grabs the source folder and puts it inside target (ie the result would be C:\target\source\files, not C:\target\files). When you want to make the inside of target look like the inside of source, you need to do:

cp C:\source\* C:\target -r -fo

Notice the *, this grabs the contents of the source folder. If you want target to be cleared first, just do rm -r -fo C:\target\* before the copy.

Powershell doesn't handle long paths, so you will need to use robocopy anyway if you have a long filename or deep folders.

这篇关于在powershell中保留文件夹结构的同时复制文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆