【问题标题】:How to unzip all files in folder? (Not .zip extension)如何解压文件夹中的所有文件? (不是 .zip 扩展名)
【发布时间】:2020-01-07 03:13:52
【问题描述】:

目前,我正在编写一个脚本,将错误压缩的 PDF 文件移动到某个文件夹。我已经做到了。我需要开始工作的下一件事是将压缩的 .pdf 文件解压缩到不同的文件夹中。

这是我的整个脚本。除了最后 2 行之外的所有内容都专用于查找压缩的 PDF 文件并移动它们。

在第一部分,脚本检查文件夹中每个 pdf 文件的前几个字节。如果它们以“PK*”开头,它们是 zip 文件并被移动到压缩文件夹中。 对于每个 PDF/zip 文件,folder next to it. 中都有一个关联的 HL7 文件

这些也需要移动到同一个文件夹。从那里需要解压缩 zip 文件并将其重新定位到“解压缩”

最后两行用于解压。

$pdfDirectory = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\struktur_id_1225\ext_dok'
$newLocation = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip'

Get-ChildItem "$pdfDirectory" -Filter "*.pdf" | foreach { 
    if ((Get-Content $_.FullName | select -First 1 ) -like "PK*") {
        $HL7 = $_.FullName.Replace("ext_dok","MDM")
        $HL7 = $HL7.Replace(".pdf",".hl7")
        move $_.FullName $newLocation;
        move $HL7 $newLocation
    }
}

Get-ChildItem 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip' |
Expand-Archive -DestinationPath 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip\unzipped' -Force

遗憾的是,这不起作用。 我怀疑这是因为这些文件没有 .zip 扩展名。唯一适用于 Expand-Archive 的过滤器是 .zip。

所以我需要找到一种方法来让这个函数解压缩文件,即使它们没有合适的扩展名......

【问题讨论】:

  • 移动文件时只需附加扩展名.zipmove $_.FullName (Join-Path $newLocation ($_.Name + '.zip'))
  • 听起来它可以工作,我将如何实现它? Get-ChildItem 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip' | Expand-Archive -DestinationPath 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip\unzipped' -Force

标签: powershell zip unzip


【解决方案1】:

就像@Ansgar 所说的那样:

Param (
    $SourcePath = 'C:\Users\xxx\Downloads\PDF',
    $ZipFilesPath = 'C:\Users\xxx\Downloads\ZIP',
    $UnzippedFilesPath = 'C:\Users\xxx\Downloads\Unzipped'
)

$VerbosePreference = 'Continue'

#region Test folders
@($SourcePath, $ZipFilesPath, $UnzippedFilesPath) | Where-Object {
    -not (Test-Path -LiteralPath $_)
} | ForEach-Object {
    throw "Path '$_' not found. Make sure that the folders exist before running the script."
}
#endregion

#region Get all files with extension .pdf
$Params = @{
    Path   = Join-Path -Path $SourcePath -ChildPath 'ext_dok'
    Filter = '*.pdf'
}
$PDFfiles = Get-ChildItem @Params

Write-Verbose "Got $($PDFfiles.count) files with extension '.pdf' from '$($Params.Path)'"
#endregion

#region Move PDF and HL7 files
$MDMpath = Join-Path -Path $SourcePath -ChildPath 'MDM'

foreach ($PDFfile in ($PDFfiles | Where-Object {
    (Get-Content $_.FullName | Select-Object -First 1) -like 'PK*'})
) {
    $MoveParams = @{
        Path        = $PDFfile.FullName
        Destination = Join-Path -Path $ZipFilesPath -ChildPath ($PDFfile.BaseName + '.zip')
    }
    Move-Item @MoveParams
    Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)'"

    $GetParams = @{
        Path        = Join-Path -Path $MDMpath -ChildPath ($PDFfile.BaseName + '.hl7')
        ErrorAction = 'Ignore'
    }
    if ($HL7file = Get-Item @GetParams) {
        $MoveParams = @{
            Path        = $HL7file
            Destination = $ZipFilesPath
        }
        Move-Item @MoveParams
        Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)$($HL7file.Name)'"
    }
}
#endregion

#region Unzip files
$ZipFiles = Get-ChildItem -Path $ZipFilesPath -Filter '*.zip' -File

foreach ($ZipFile in $ZipFiles) {
    $ZipFile | Expand-Archive -DestinationPath $UnzippedFilesPath -Force

    Write-Verbose "Unzipped file '$($ZipFile.Name)' in folder '$UnzippedFilesPath'"
}
#endregion

一些提示:

  • 在脚本开头添加Param () 子句以包含所有可以更改的变量。
  • 尽量使用完整的参数名称来清楚地表明什么是什么。使用Get-ChildItem -Path xxx 而不是Get-ChildItem xxx
  • 对长参数使用hash tables。这使代码的宽度更紧凑,更易于阅读。
  • 使用#region#endregion 对您的代码进行分组。

【讨论】:

  • 感谢您的贡献,但我不太明白您的回答...您是否重写了我的整个脚本?如果是这样,那么检查 PDF 是否真的是 zip 的 If 语句现在就消失了,我不知道如何再次实现它。
  • 是的,很抱歉没有更正确地解释所有内容。我将编辑我的答案,使其包含更多上下文。 Here 是我关于这个话题的第一个问题,也解释了一切。
  • 不,HL7 文件不是 ZIP 文件。它不需要以任何方式修改。
  • 太棒了!这几乎完美!我刚刚测试过,ZIP 文件中包含 3 个文件,都称为“scan.pdf”。同名的 3 个中只有一个被解压缩并移至解压缩。您知道可能是什么原因造成的吗?非常感谢您提供的非常有用的帮助!
  • 他们都解压了,但最后一个总是会覆盖前一个同名的。如果你不想这样,你应该先检查解压缩文件的名称并在它们后面附加一个数字
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 2015-04-05
  • 2019-07-07
相关资源
最近更新 更多