2009-01-21 29 views
19

Tôi có một tập lệnh Powershell sao chép tệp từ một vị trí này sang vị trí khác. Khi bản sao hoàn tất, tôi muốn xóa thuộc tính Lưu trữ trên các tệp ở vị trí nguồn đã được sao chép.Làm cách nào để thay đổi thuộc tính của tệp bằng Powershell?

Làm cách nào để xóa thuộc tính Lưu trữ của tệp bằng Powershell?

+0

này cũng có thể giúp: http://cmschill.net/stringtheory/2008/04/bitwise- toán tử/** Chỉnh sửa **: Câu trả lời có thể có từ archive.org ngay bây giờ khi liên kết trả về 404: https://web.archive.org/web/20100105052819/http://cmschill.net/stringtheory/2008/ 04/bitwise-operator /. –

Trả lời

11

Từ here:

function Get-FileAttribute{ 
    param($file,$attribute) 
    $val = [System.IO.FileAttributes]$attribute; 
    if((gci $file -force).Attributes -band $val -eq $val){$true;} else { $false; } 
} 


function Set-FileAttribute{ 
    param($file,$attribute) 
    $file =(gci $file -force); 
    $file.Attributes = $file.Attributes -bor ([System.IO.FileAttributes]$attribute).value__; 
    if($?){$true;} else {$false;} 
} 
26

Bạn có thể sử dụng lệnh tốt cũ dos attrib như thế này:

attrib -a *.* 

Hoặc để làm điều đó bằng Powershell bạn có thể làm một cái gì đó như thế này:

$a = get-item myfile.txt 
$a.attributes = 'Normal' 
7

Do các thuộc tính về cơ bản là trường bitmask, bạn cần phải chắc chắn xóa trường lưu trữ khi rời khỏi phần còn lại một mình:

 
PS C:\> $f = get-item C:\Archives.pst 
PS C:\> $f.Attributes 
Archive, NotContentIndexed 
PS C:\> $f.Attributes = $f.Attributes -band (-bnot [System.IO.FileAttributes]::Archive) 
PS C:\> $f.Attributes 
NotContentIndexed 
PS H:\> 
0

Bạn có thể sử dụng lệnh sau để chuyển đổi hành vi

$file = (gci e:\temp\test.txt) 
$file.attributes 
Archive 

$file.attributes = $file.Attributes -bxor ([System.IO.FileAttributes]::Archive) 
$file.attributes 
Normal 

$file.attributes = $file.Attributes -bxor ([System.IO.FileAttributes]::Archive) 
$file.attributes 
Archive 
1
$attr = [System.IO.FileAttributes]$attrString 
$prop = Get-ItemProperty -Path $pathString 
# SetAttr 
$prop.Attributes = $prop.Attributes -bor $attr 
# ToggleAttr 
$prop.Attributes = $prop.Attributes -bxor $attr 
# HasAttr 
$hasAttr = ($prop.Attributes -band $attr) -eq $attr 
# ClearAttr 
if ($hasAttr) { $prop.Attributes -bxor $attr } 
1

câu trả lời của Mitch hoạt động tốt đối với hầu hết các thuộc tính, nhưng will not work cho "nén". Nếu bạn muốn thiết lập các thuộc tính nén vào một thư mục sử dụng PowerShell bạn phải sử dụng các công cụ dòng lệnh compact

compact /C /S c:\MyDirectory 
Các vấn đề liên quan