将文件移动到多个文件夹并使用powershell压缩

将文件移动到多个文件夹并使用powershell压缩,powershell,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\Cou

我需要将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归档方式

我只是尝试了下面的代码,它对我有效

一,


有人能告诉我,是否可以编写一个循环来归档3个子文件夹,而不是编写压缩归档和删除命令行3次

I循环是可能的,那么请让我知道是否需要为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

}

是的,可以,请务必阅读PS文档中有关循环的内容(有几种方法)。请记住,SO不是免费的编码服务。非常感谢,它很有效!!我正在尝试使用任务调度器自动运行它,但脚本不是由任务调度器运行的。powershell版本是5.1.17763.1490,操作系统是windows 10。执行策略设置为旁路。您好,我测试了较低的文件数,它运行得非常快。但对于2500个5kb-6kb的xml文件,完成归档和删除过程大约需要6分钟。是否有任何方法可以加速该过程,或者这取决于系统内存使用情况和性能??这就是为什么我提出了另一个解决方案,其中创建压缩而不更新我们如何使用7zip而不是压缩存档?
#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

}