我有一项任务,需要压缩每个子父目录(月份)内的所有子目录(子目录是每月的每一天)并移动到存档文件夹。然后删除原始文件。还有一个选项是针对 3 周前最后修改的文件夹执行此操作。
例子
E:
-Share1 (Static, does not change)
-2019 (Kept for this year)
- 01 (Month)
-01 (1st day)
-02 (second day)
-03 (third day
.............
-31 (31st day of the month, where applicable)
-02 (second Month)
-01 (1st day)
-02 (second day)
-03 (third day
.............
-31 (31st day of the month, where applicable)
-Share~12....
-2019
- 01 (Month)
-01 (1st day)
-02 (second day)
-03 (third day
.............
-31 (31st day of the month, where applicable)
-02 (second Month)
-01 (1st day)
-02 (second day)
-03 (third day
.............
-31 (31st day of the month, where applicable)
(12 个静态父文件夹/共享)我需要脚本遍历每个父文件夹,深度为 3(天),并将 01 - 31 文件夹(月份中的天数)压缩到存档文件夹,并保持文件夹结构不变。然后,在成功压缩后,删除刚刚压缩的天数文件夹。
我对 Powershell 脚本的编写经验很少,但搜索过很多资源,从其他人那里获取了一些内容并将它们整合在一起。我目前有一个脚本,它可以压缩指定的子目录(每月的第一天)并将其移动到“存档”目录,同时删除原始目录。
我没有能力让脚本从 2 号运行到 31 号并执行相同的操作。
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$files="E:\Live_data\2019\01"
$target="E:\Archived_data\2019\01"
$dirs = Get-ChildItem -Path $files | Where-Object { $_.Attributes -eq "Directory" }
# Creates 7z files based on the parent folders name and drops it inside the archive folder
Foreach ($dir in $dirs)
{
$name = $dir.name
$newname = $name.ToLower() -replace(" ","")
sz a -t7z "$target\$newname" "$files\$dir"
}
# Once archive created delete folder
if(Test-Path $target) {
Remove-Item -Path $files -Force -Recurse -Confirm:$false
Write-Host "directory removed: $files"
}
我将非常感激任何能给我提供帮助的帮助。
答案1
我的回答基于@KeithMiller 的回答。
它通过指定范围而不是通配符来扩展 Get-ChildItem 中多个目录级别的想法,将子文件夹限制为月/日数字
并且仅当之前的 7z 成功时才会删除文件。
Q:\Test\2019\MM\dd
使用具有相同结构的文件夹副本进行测试。
## Q:\Test\2019\04\28\SO_1430440.ps1
if (!(Test-Path "$ENV:ProgramFiles\7-Zip\7z.exe")) {
throw "$ENV:ProgramFiles\7-Zip\7z.exe needed"
}
Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$files = "E:\Live_data\2019"
$target = "E:\Archived_data\2019"
Get-ChildItem -Path "$files\[01][0-9]\[0-3][0-9]" -Directory |
# Where {$_.LastWriteTime.Date -gt [Datetime]::Today.AddDays(-21)}|
Foreach-Object {
$month = $_.Parent.Name
$day = $_.Name
If(!(Test-Path "$target\$month")){MD "$target\$month" | Out-Null}
sz a -t7z "$target\$month\$day" $_.FullName
$output = (sz a -t7z "$target\$month\$day" $_.FullName 2>&1)
if ($LastExitCode -ne 0){
"ERROR executing: sz a -t7z `"$target\$month\$day`" $_.FullName"
$output
pause
return
} else { #remove day folder
Remove-Item -Path $_.FullName -Force -Recurse -Confirm:$false
}
}
其中Where-Object
包括限制 LastWriteTime 大于 21 天的文件夹,但已被注释掉。
答案2
类似这样的事情应该可以工作:
$files = "E:\Live_data\2019"
$target = "E:\Archived_data\2019"
Get-ChildItem -Path "$files\*\*" -Directory |
Foreach-Object {
$month = $_.Parent.Name
$newname = $_.name.ToLower() -replace(" ","")
sz a -t7z "$target\$month\$newname" $_.FullName
if(Test-Path "$target\$month\$newname") {
Remove-Item -Path $_.FullName -Force -Recurse -Confirm:$false
}
If (!(Get-ChildItem -path $_.Parent.FullName -Directory)) { # all sub-folders archived
Remove-Item -Path $_.Parent.FullName -Force
Write-Host ("directory removed: {0}" -f $_.Parent.FullName)
}
}
基思