簡體   English   中英

使用 powershell 將文件移動到多個文件夾並壓縮

[英]moving files to multiple folders and zipping using powershell

我需要將超過 7 天的文件從文件夾移動到子文件夾,然后 zip 將它們從源位置刪除。

文件夾結構就像。 ( Files generate on these folders on daily basis) C:\Users\529817\TEST\Country1 C:\Users\529817\TEST\Country2 C:\Users\529817\TEST\Country3

我需要將它們移動到內部的子文件夾和 zip 並從源位置刪除。 C:\Users\529817\TEST\Country1\Archive C:\Users\529817\TEST\Country2\Archive C:\Users\529817\TEST\Country3\Archive

這是一個界面。 像這樣,我還有 8 個接口,里面有不同國家的文件。 需要對所有 8 個界面或主文件夾執行 7 天以上的 smae 歸檔方式。

我剛剛嘗試了下面的代碼,它對我有用。

1.


$SourcePath = "C:\Users\529817\TEST\Country1"
$TargetPath = "C:\Users\529817\TEST\Country1\ARCHIVE"
$YourDirToCompress ="C:\Users\529817\TEST\Country1\ARCHIVE"

$SourcePath_1 = "C:\Users\529817\TEST\Country2"
$TargetPath_1 = "C:\Users\529817\TEST\Country2\ARCHIVE"
$YourDirToCompress_1 ="C:\Users\529817\TEST\Country2\ARCHIVE"


$SourcePath_2 = "C:\Users\529817\TEST\Country3"
$TargetPath_2 = "C:\Users\529817\TEST\Country3\ARCHIVE"
$YourDirToCompress_2 ="C:\Users\529817\TEST\Country3\ARCHIVE"

$Days = "7"
$Date = Get-Date -format yyyy-MM-dd_HH-mm 

Write-Verbose $Activity
Get-Childitem -recurse -Path $SourcePath  | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Compress-Archive -DestinationPath $YourDirToCompress\$date.zip -update
Get-ChildItem -Recurse -Path $SourcePath  -Force | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Remove-Item -Force

Write-Verbose $Activity
Get-Childitem -recurse -Path $SourcePath_1  | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Compress-Archive -DestinationPath $YourDirToCompress_1\$date.zip -update
Get-ChildItem -Recurse -Path $SourcePath_1  -Force | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Remove-Item -Force

Write-Verbose $Activity
Get-Childitem -recurse -Path $SourcePath_2  | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Compress-Archive -DestinationPath $YourDirToCompress_2\$date.zip -update
Get-ChildItem -Recurse -Path $SourcePath_2  -Force | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | Remove-Item -Force

有人可以告訴我是否可以編寫一個循環來執行 3 個子文件夾以上的存檔,而不是編寫 compress-archive 和 delete 命令行 3 次。

我可以循環然后請告訴我是否需要為 8 個接口編寫 8 個不同的腳本,或者使用單個腳本它可以處理所有 8 個接口及其子文件夾?

我感謝您的幫助...

一種可能性:

$AllDir=@(
@('C:\Users\529817\TEST\Country1',  'C:\Users\529817\TEST\Country1\ARCHIVE'),
@('C:\Users\529817\TEST\Country2', 'C:\Users\529817\TEST\Country2\ARCHIVE'),
@('C:\Users\529817\TEST\Country3', 'C:\Users\529817\TEST\Country3\ARCHIVE')
)

$Days = "7"
$Date = Get-Date -format yyyy-MM-dd_HH-mm
$MaxDate =(get-date).AddDays(-$days)

$AllDir | %{

$SourcePath=$_[0]
$YourDirToCompress=$_[1]

Get-Childitem -recurse -Path $SourcePath  | Where LastWriteTime -lt $MaxDate | Compress-Archive -DestinationPath $YourDirToCompress\$date.zip -update
Get-ChildItem -Recurse -Path $SourcePath  | Where LastWriteTime -lt $MaxDate | Remove-Item -Force

}

如果你想要一個通用方法:

#take all country directory
$AllDirCountry=Get-ChildItem "C:\Users\529817\TEST\Country*" -directory

$Days = 7
$Date = Get-Date -format yyyy-MM-dd_HH-mm-ss-fffff
$MaxDate =(get-date).AddDays(-$days)

$AllDirCountry | %{

#build path
$SourcePath=$_.FullName
$YourDirToCompress="{0}\ARCHIVE" -f $_.FullName
$ArchiveDay="{0}\{1}" -f $YourDirToCompress, $Date
$ZipFile="{0}\{1}.zip" -f $YourDirToCompress, $Date


#remove archive day
Remove-Item $ArchiveDay -Recurse -Force -ErrorAction SilentlyContinue

#create archive directory if not exists
New-Item $YourDirToCompress -ItemType Directory -Force

#create archive of day directory if not exists
New-Item $ArchiveDay -ItemType Directory -Force

#move directory and file except files into archive directory
Get-Childitem -recurse -Path $SourcePath  | Where {$_.LastWriteTime -lt $MaxDate -and $_.Directory -notlike "$YourDirToCompress*"} | move-Item -Destination $ArchiveDay -ErrorAction SilentlyContinue

#compress archive day
Compress-Archive "$ArchiveDay" -DestinationPath $ZipFile

#remove archive day
Remove-Item $ArchiveDay -Recurse -Force -ErrorAction SilentlyContinue

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM