如何使用powershell迭代文件? [英] how to iterate over files with powershell?

查看:46
本文介绍了如何使用powershell迭代文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 powershell 脚本遍历文件夹中的文件;

I want to iterate over files in a folder using powershell script;

推荐答案

Get-ChildItem | ForEach-Object { <# do what you need to do #> }

或更短:

gci | % { <# ... #> }

或者如果你想要一个显式的循环结构:

or if you want an explicit looping construct:

foreach ($file in Get-ChildItem) {
    # ...
}

但是请注意,foreach 只会在 Get-ChildItem 的所有输出被收集后运行.在我看来,大多数 Powershell 代码应该尽可能多地使用管道.

Note however, that foreach will only run once all output from Get-ChildItem is collected. In my opinion most Powershell code should use the pipeline as much as possible.

这篇关于如何使用powershell迭代文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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