How to delete all the files in a folder except read-only files?

后端 未结 3 760
春和景丽
春和景丽 2021-01-20 21:14

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

How to do it using powershell?

3条回答
  •  孤城傲影
    2021-01-20 21:54

    Check the attribute of each folder and file and then do a conditional based deletion. This is just the pseudo code.

    If (-not (a readonly file)) {
    delete file
    }
    

    So, to check if a given file or folder is readonly:

    $item = Get-Item C:\Scripts\Test.txt
    $item.IsReadOnly
    

    HTH

提交回复
热议问题