2016-09-01 78 views
0

我寫了一個小腳本來搜索我的一臺服務器上的所有文件夾來查找所有超過3年的文件。Powershell壓縮 - 存檔在老年文件

但是,我想要將符合條件的所有文件寫入相同名稱的存檔,並且有點卡在如何實現。

這是到目前爲止我的代碼...

$limit = (Get-Date).AddYears(-3) 
$Path = "L:\" 
$Path2 = "archive.zip" 

$PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2} 

我所試圖實現的是基本;

> l:\ 

目錄有3歲以上3個文件...

Red.PDF Hello.Doc Me.JPG

我想取這三個文件並將它們添加到一個存檔文件;

L:\Archive-ddmmyyyy.zip 

在一個理想的世界中所有已歸檔的將被寫入這些文件/添加到在L csv文件的名稱:\題爲archivedlog.csv列出文件名,日期,它已創建並存檔的日期。

最後的循環會刪除原來的三個文件,並保持檔案不變。這最後一部分我認爲我可以實現其他元素不是很多。

我到目前爲止是找到老化的文件,但在壓縮方面,例如說'路徑'Red.PDF'或者不存在或不是有效的系統文件路徑。

這可能是由於然而對保存的文檔的服務器數量的文件格式不好實際上是格式...

「太太P辛普森信12.07.2012.docx要麼不存在或不是有效的文件系統路徑「。

猜測由於重複'。'在文件名中,這可能會導致問題,但我一個人不知道...

任何人都可以指出我在這個正確的方向嗎?

我應該說,這是爲了緩衝L:\中的每個目錄,爲每個子目錄中的所有文件生成一個檔案,該檔案屬於這個類別。

親切的問候

[R

回答

1

更換

Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2} 

隨着

Compress-Archive -Path $PathB.FullName -DestinationPath {$Path + $Path2} 

否則Compress-Archive會希望該文件在同一目錄下。

編輯查看-LiteralPath路徑切換如果您希望使用具有奇怪字符的文件名。 .雖然不錯。

0

由於大量到gms0ulman,你們的幫助讓我在正確的軌道上...
我分享我終於想出了它應該幫助任何人在未來,

## Sets the time scale in years for files older than that we want to delete. 
$limit = (Get-Date).AddYears(-3) 
## Sets the starting path for the process 
$Path = "L:\" 
## Sets the save file name 
$Path2 = "archive + $(get-date -f dd-MM-yyyy).zip" 
## Sets the archive logfile location 
$write = "l:\archivelog-donotremove.csv" 

## Runs through each file to identify files in scope and writes to object 
$PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
## Adds all objects to zip file 
Compress-Archive -Path $Pathb.FullName -DestinationPath "$Path + $Path2" -Force 


## Appends all data to the log file that has been archived 
$PathB | export-csv -Append -Path $write -NoTypeInformation -Force 

## Deletes all non compressed files that were identified 
$Removal = Get-ChildItem -Path $Pathb.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } | foreach { $_.Delete()} 
相關問題