SRP*_*SRP 6 powershell powershell-2.0 powershell-3.0 powershell-4.0
我已经尝试了很多代码,但仍然无法删除该特定文件夹的只读属性。
下面的代码删除了该文件夹下存在的文件的只读属性,但不删除该文件夹的只读属性:
$Path = "C:\Suraj\powershell scripts\review script"
$Files = Get-ChildItem $Path -Recurse
ForEach ($File in $Files) {
Write-Host file:$File IsReadOnly: $File.IsReadOnly
if ($File.Attributes -ne "Directory" -and $File.Attributes -ne "Directory, Archive") {
try {
Set-ItemProperty -Path $Path"\"$File -name IsReadOnly -value $false
}
catch {
Write-Host "Error at file " $Path "\" $File
}
}
}
Run Code Online (Sandbox Code Playgroud)
文件夹是否ReadOnly
设置了属性,可以通过以下方式验证:
$folder = Get-Item -Path path/to/folder
$folder.Attributes
Run Code Online (Sandbox Code Playgroud)
默认输出将是:
Directory
要添加ReadOnly
属性,只需执行:
$folder.Attributes = $folder.Attributes -bor [System.IO.FileAttributes]::ReadOnly
Run Code Online (Sandbox Code Playgroud)
如果再次显示属性,它应该如下所示:
ReadOnly, Directory
要删除该ReadOnly
属性,只需执行:
$folder.Attributes = $folder.Attributes -band -bnot [System.IO.FileAttributes]::ReadOnly
Run Code Online (Sandbox Code Playgroud)
属性看起来又像这样:
Directory
正如您所看到的,确实可以添加和删除该ReadOnly
属性,但正如其他人在评论中已经提到的那样,它不会产生太大的效果。
该ReadOnly
属性还可以以更易读的方式添加和/或删除:
$folder.Attributes += 'ReadOnly'
$folder.Attributes -= 'ReadOnly'
Run Code Online (Sandbox Code Playgroud)
但请注意,如果您在该属性已存在时添加该属性,或者在该属性不存在时将其删除,则此方法无法可靠地工作。这是因为属性存储在位字段中。减去未设置的位将翻转许多其他位,而不仅仅是这一位。
归档时间: |
|
查看次数: |
14945 次 |
最近记录: |