code

PowerShell을 사용하여 파일에서 ReadOnly 특성을 제거하는 방법?

codestyles 2020. 8. 25. 08:03
반응형

PowerShell을 사용하여 파일에서 ReadOnly 특성을 제거하는 방법?


PowerShell (버전 1.0) 스크립트를 사용하여 파일에서 ReadOnly 특성을 제거하려면 어떻게해야합니까?


다음을 사용할 수 있습니다 Set-ItemProperty.

Set-ItemProperty file.txt -name IsReadOnly -value $false

이하 :

sp file.txt IsReadOnly $false

$file = Get-Item "C:\Temp\Test.txt"

if ($file.attributes -band [system.IO.FileAttributes]::ReadOnly)  
{  
  $file.attributes = $file.attributes -bxor [system.IO.FileAttributes]::ReadOnly    
}  

위의 코드는이 기사 에서 가져온 것입니다.

업데이트 의견에서 Keith Hill의 구현을 사용하면 (이를 테스트했으며 작동합니다) 다음과 같이됩니다.

$file = Get-Item "C:\Temp\Test.txt"

if ($file.IsReadOnly -eq $true)  
{  
  $file.IsReadOnly = $false   
}  

네이티브 PowerShell 은 아니지만 이를 위해 간단한 Attrib 명령을 사용할 수 있습니다 .

attrib -R file.txt

또는 간단히 다음을 사용할 수 있습니다.

get-childitem *.cs -Recurse -File | % { $_.IsReadOnly=$false }

위는 현재 폴더의 하위 트리에있는 모든 .cs 파일에 대해 작동합니다. 포함 된 다른 유형이 필요한 경우 "* .cs"를 필요에 맞게 조정하십시오.


PowerShell 커뮤니티 확장을 사용하는 경우 :

PS> Set-Writable test.txt
PS> dir . -r *.cs | Set-Writable
# Using alias swr
PS> dir . -r *.cs | swr

다음과 같이 반대로 할 수 있습니다.

PS> dir . -r *.cs | Set-ReadOnly
# Using alias sro
PS> dir . -r *.cs | sro

Shell("net share sharefolder=c:\sharefolder/GRANT:Everyone,FULL")
Shell("net share sharefolder= c:\sharefolder/G:Everyone:F /SPEC B")
Shell("Icacls C:\sharefolder/grant Everyone:F /inheritance:e /T")
Shell("attrib -r +s C:\\sharefolder\*.* /s /d", AppWinStyle.Hide)

문제 해결을 돕고이 코드를 도와 주신 분들께 감사드립니다.

이 코드는 나를 위해 일하고 있습니다 .. 읽기 및 쓰기 권한을 가진 모든 사람에게 폴더를 공유하려면 .net에서 사용할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/893167/how-to-remove-readonly-attribute-on-file-using-powershell

반응형