您现在的位置是:网站首页> 编程资料编程资料
PowerShell遍历文件、文件夹的方法_PowerShell_
2023-05-26
514人已围观
简介 PowerShell遍历文件、文件夹的方法_PowerShell_
PowerShell遍历文件夹下的子文件夹和文件是一件很容易的事儿。Get-ChildItem这个cmdlet就有一个recurse参数是用于遍历文件夹的。
PowerShell中,使用Get-ChildItem来获取文件夹下面的子文件夹和文件(当然,它的功能不仅于此)。然后我们可以使用ForEach-Object的cmdlet来循环遍历下面的子对象。然后通过psiscontainer 属性来判断是文件夹还是文件。
Get-ChildItem,获取指定对象的所有子对象集合。
举例:
#获取D:\对象,返回值类型为System.IO.DirectoryInfo
Get-ChildItem D:\
#输出D:\下所有文件的文件名
Get-ChildItem D:\ | ForEach-Object -Process{
if($_ -is [System.IO.FileInfo])
{
Write-Host($_.name);
}
}
#列出今天创建的文件
Get-ChildItem D:\ | ForEach-Object -Process{
if($_ -is [System.IO.FileInfo] -and ($_.CreationTime -ge [System.DateTime]::Today))
{
Write-Host($_.name,$_.CreationTime);
}
}
#找出D盘根目录下的所有文件
Get-ChildItem d:\ | ?{$_.psiscontainer -eq $false}
如果要找文件夹,则把$false换成$true
相关内容
- PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例_PowerShell_
- PowerShell读取文件内容、替换文件内容、读取限定行的例子_PowerShell_
- PowerShell中把IP转换为长整形数字的方法_PowerShell_
- PowerShell统计文件夹下文件个数的方法_PowerShell_
- PowerShell生成随机密码的方法_PowerShell_
- PowerShell中的TimeSpan时间差对象使用实例_PowerShell_
- PowerShell中常用的一些特殊运算符介绍_PowerShell_
- PowerShell把IP地址转换成二进制的方法_PowerShell_
- PowerShell获取Windows用户列表、用户信息的方法_PowerShell_
- PowerShell脚本实现添加、修改任务计划的例子_PowerShell_
