首页 > 解决方案 > 将文件移动到多个文件夹并使用 powershell 压缩

问题描述

我需要将超过 7 天的文件从文件夹移动到子文件夹中,然后压缩它们并从源位置删除。

文件夹结构就像。(文件每天在这些文件夹中生成) C:\Users\529817\TEST\Country1 C:\Users\529817\TEST\Country2 C:\Users\529817\TEST\Country3

我需要将它们移动到里面的子文件夹并压缩它们并从源位置删除。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 个接口及其子文件夹?

我很感激帮助...

标签: powershell

解决方案


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

#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

}

推荐阅读