如何正确使用Copy-Item cmdlet复制管道文件 [英] How to use the Copy-Item cmdlet correctly to copy piped files

查看:319
本文介绍了如何正确使用Copy-Item cmdlet复制管道文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小脚本,该脚本应该将所有 .zip 文件复制到一个名为 F:\tempzip

I am building a small script which should copy all .zip files to a special folder called F:\tempzip.

我尝试了复制项 cmdlet,但我没有做到这一点。该脚本应从该文件夹(递归)复制所有文件 .zip。

I tried it with Copy-Item cmdlet, but I didn't manage to do it. The script should copy all files from this folder (recursively) which are ".zip".

这是我正在讨论的脚本的一部分:

This is the part of the script I am talking about:

get-childitem F:\Work\xxx\xxx\xxx -recurse `
   | where {$_.extension -eq ".zip"}       `
   | copy-item F:\tempzip

我必须添加什么?

推荐答案

将项目传递到复制项时,您需要告诉它 F:\tempzip 是目标路径。

When piping items to copy-item you need to tell it that "F:\tempzip" is the destination path.

| Copy-Item -Destination F:\tempzip

您还可以在 where 运算符,方法是使用 Get-ChildItem 的参数 -filter

You can also cutout piping to the where operator by using Get-ChildItem's parameter -filter.

Get-Childitem "C:\imscript" -recurse -filter "*.zip" | Copy-Item -Destination "F:\tempzip"

编辑:删除了不必要的 foreach 循环并更新了说明。

Edit: Removal of unnecessary foreach loop and updated explanation.

这篇关于如何正确使用Copy-Item cmdlet复制管道文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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