【问题标题】:Archive file inside of folders文件夹内的存档文件
【发布时间】:2020-05-04 23:15:59
【问题描述】:

我正在尝试执行以下操作,例如,我们有一个文件夹列表,并希望在与 xml 文件位置相同的目录中创建存档:

文件夹 1

  • 子文件夹 1

    • XML 文件 1
    • XML 文件 2
  • 子文件夹 2

    • XML 文件 1
    • XML 文件 2
  • 子文件夹 3

    • XML 文件 1
    • XML 文件 2

我想要完成的是以下,我想在每个子文件夹中归档每月归档中超过 30 天的 xml 文件。

文件夹 1

  • 子文件夹 1

    • XML 文件 1
    • XML 文件 2
    • archive-mar-2020.zip
    • archive-feb-2020.zip
  • 子文件夹 2

    • XML 文件 1
    • XML 文件 2
    • archive-mar-2020.zip
    • archive-feb-2020.zip

这就是我现在所拥有的,所以我需要以某种方式将目标路径更改为当前文件夹或其他东西,但我不知道该怎么做

# set folder path
$log_path = "F:\test\*.xml"
$zip_path = "F:\test\*.7z"
$target_path = "F:\test-zip\"

# set min age of files
$max_days = "-30"
$delete_max_days = "-365"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$zip_date = $curr_date.AddDays($max_days)
$delete_zip_date = $curr_date.AddDays($delete_max_days)

#$zip_date = (Get-Date).AddMonths(0).Month

# filter files
Get-ChildItem $log_path -Recurse | Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false)}|

    ForEach {
    $Zip = $target_path + "{0:MMM}_{0:yyyy}.7z" -f $_.LastWriteTime
    & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2  $Zip $_.FullName |Out-Null
    If ($LastExitCode -eq 0) { Remove-Item $_.FullName }
    }

$deletefile = Get-ChildItem $zip_path | Where-Object { $_.LastWriteTime -lt $delete_zip_date } | Remove-Item

【问题讨论】:

  • 乍一看:您正在为$max_days$delete_max_days 创建字符串,它们应该是整数。删除那里的引号

标签: xml powershell


【解决方案1】:

这是未经测试的

我认为您需要先获取子文件夹列表并对其进行迭代,找到每个子文件夹中的 xml 文件。

根据格式化的 LastWriteTime (MMM-yyyy) 属性对文件进行分组,并以此为基础存档名称。

类似:

# set folder paths
$log_path    = "F:\test"
$zip_path    = "F:\test"
$target_path = "F:\test-zip"

# set min age of files
$max_days = -30
$delete_max_days = -365

# get the current date
$curr_date = (Get-Date).Date

# determine how far back we go based on current date
$zip_date = $curr_date.AddDays($max_days)
$delete_zip_date = $curr_date.AddDays($delete_max_days)

# first get a list of subfolders full names
$subDirs = (Get-ChildItem -Path $log_path -Directory -Recurse).FullName
foreach ($subFolder in $subDirs) {
    # within each subfolder, get the xml files
    Get-ChildItem -Path $subFolder -Filter '*.xml' -File -Recurse | Where-Object { $_.LastWriteTime -lt $zip_date } |
    Group-Object @{Expression = '{0:MMM-yyyy}' -f $_.LastWriteTime } |
    ForEach-Object {
        # $_.Name is the name of the group, which is the formatted LastWriteTime
        $zipFile = Join-Path -Path $target_path -ChildPath "archive-$($_.Name).7z"
        foreach ($file in $_.Group) {
            & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2  $zipFile $_.FullName | Out-Null
            if ($LastExitCode -eq 0) { Remove-Item $_.FullName }
        }
    }

}

$deletefile = Get-ChildItem -Path $zip_path -Filter '*.7z' -File | Where-Object { $_.LastWriteTime -lt $delete_zip_date } | Remove-Item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多