手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >PowerShell遍历文件、文件夹的方法
PowerShell遍历文件、文件夹的方法
摘要:PowerShell遍历文件夹下的子文件夹和文件是一件很容易的事儿。Get-ChildItem这个cmdlet就有一个recurse参数是用...

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遍历文件、文件夹的方法】相关文章:

PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子

PowerShell生成随机密码的方法

PowerShell判断某天是星期几的方法

PowerShell脚本清理指定天数前的临时文件夹实现代码

PowerShell中使用正则表达式跨行匹配字符串的方法

PowerShell中把相对路径转换为绝对路径的2个方法

PowerShell函数参数设置为即可选又必选的方法

PowerShell Out-File禁止覆盖文件的方法

PowerShell中使用通配符匹配文件路径的例子

PowerShell查找分区中最大文件的方法(查找文件并按大小排序)

精品推荐
分类导航