如何删除文件夹中除只读文件之外的所有文件? [英] How to delete all the files in a folder except read-only files?

查看:44
本文介绍了如何删除文件夹中除只读文件之外的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除一个文件夹中除只读文件之外的所有文件和子文件夹.

I would like to delete all the files and subfolders from a folder except read-only files.

如何使用powershell?

How to do it using powershell?

推荐答案

唯一可以只读的对象是文件.当您使用 Get-ChildItem cmdlet 时,您将返回 System.IO.FileInfoSystem.IO.DirectoryInfo 类型的对象.FileInfos 有一个名为 IsReadOnly 的属性.所以你可以做这一个班轮:

The only objects that can be read-only are files. When you use the Get-ChildItem cmdlet you are getting objects of type System.IO.FileInfo and System.IO.DirectoryInfo back. The FileInfos have a property named IsReadOnly. So you can do this one liner:

dir -recurse -path C:\Somewhere |?{-not $_.IsReadOnly -and -not $_.PsIsContainer} |Remove-Item -Force -WhatIf

PsIsContainer 属性是由 PowerShell 创建的(Ps 前缀给出了它)并告诉该项目是文件还是文件夹.我们可以使用它只将文件传递给 Remove-Item.

The PsIsContainer property is created by PowerShell (Ps prefix gives it away) and tells whether or not the item is a file or folder. We can use this to pass only files to Remove-Item.

当您准备真正删除时删除 -WhatIf.

Remove -WhatIf when you are ready to delete for real.

这篇关于如何删除文件夹中除只读文件之外的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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