powershell - How do I check if all the files in a folder is read only recursively? -


currently getting the total file number , file number of read files , see if same:

function allreadonly{     param([string]$path)     $file_count = get-childitem $path -recurse -file | measure-object | %{$_.count}     $read_file_count = get-childitem $path -recurse -file -attributes readonly | measure-object | %{$_.count}     $read_file_count     $file_count } 

even if correct takes long time , can't feel there should better way this.

currently you're looping twice on files, improvement if incremented 2 variables in same loop, , returned boolean value indicating whether count different or not:

function allreadonly {     param([string]$path)      $all = 0     $ro  = 0     get-childitem $path -recurse -file | foreach-object {       $all++       if ($_.attributes.value__ -band 1) { $ro++ }     }      $all -eq $ro } 

however, since want check if all files read-only suffice return $false encounter first writable file:

function allreadonly {     param([string]$path)      get-childitem $path -recurse -file | foreach-object {       if (-not ($_.attributes.value__ -band 1)) {         $false         continue       }     }      $true } 

edit:

$_.attributes.value__ numeric value of file's attributes. binary value 1 indicates read-only flag set, if bitwise/logical , of attributes value , 1 returns value != 0 flag set, otherwise it's not set. that's because logical , returns true/1 if both operands true/1 , false/0 otherwise.

example:

    101001 , 000001     ------     000001 ← flag set      101000 , 000001     ------     000000 ← flag not set

see wikipedia further information boolean algebra.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -