【问题标题】:In Powershell, how to make the zip output name the same as the input name在 Powershell 中,如何使 zip 输出名称与输入名称相同
【发布时间】:2021-06-07 19:59:49
【问题描述】:

我正在编写一个简单的 ps1 脚本来自动将日志文件压缩到他们自己的单独 zip 存档中(我最终将使用任务计划程序来安排),并且我希望输出名称与输入名称相同(例如 Log_20210309.log > Log_20210309.zip)

我觉得我很接近。下面的示例查找文件名中包含昨天日期的 *.log 文件,我需要帮助的地方是将文件名通过管道传输到 zip 输出文件名。

$Input = "C:\Script\Input\"
$Output = "C:\Scripts\Output\"

Get-ChildItem $Input -Filter *$((Get-Date).AddDays(-1).ToString('yyyyMMdd')).log | Compress-Archive -DestinationPath $Output$_.FileName -Force

Get-ChildItem 部分似乎工作正常(如果我用Compress-Archive -DestinationPath $Output -Force 替换管道后的所有内容,脚本就可以工作,但输出文件名将为空白(“.zip”))。我已经尝试了几十种管道部分的变体,包括:

Compress-Archive -DestinationPath $Output + $_.FileName -Force
Compress-Archive -DestinationPath $Output + "$_.FileName" -Force
ForEach-Object { Compress-Archive -DestinationPath $Output"$_.FileName" -Force }
ForEach-Object { Compress-Archive -DestinationPath $Output + "$_.FileName" -Force }

等等……

【问题讨论】:

  • 不要使用$input作为变量名,因为它是automatic variable
  • @Theo 感谢您的提示

标签: powershell zip output compression archive


【解决方案1】:

此脚本使用源目录、目标目录和文件扩展名的变量进行压缩。当它压缩正确的文件时,从Compress-Archive 命令中删除-WhatIf

$SourceDir = 'C:\Script\Input'
$OutputDir = 'C:\Script\Output'
$Extension = '.log'

Get-ChildItem -File -Recurse -Path $SourceDir -Filter $('*' + $Extension) |
    ForEach-Object {
        Compress-Archive `
            -DestinationPath (Join-Path $OutputDir $($_.BaseName + '.zip')) `
            -Path $_.FullName -WhatIf
    }

【讨论】:

    【解决方案2】:

    在此示例中,我使用 *.txt 而不是 *.log。这工作正常:

    $destinationPath="$PSScriptRoot\Output"
    
    ls "$PSScriptRoot\input\*.txt"|%{ 
        $fileName=$_.Name -replace '.txt','.zip'
        Compress-Archive -Path $_.FullName -DestinationPath "$destinationPath\$fileName" -CompressionLevel Optimal
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多