Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何解压缩文件夹中的所有文件?(不是.zip扩展名)_Powershell_Zip_Unzip - Fatal编程技术网

Powershell 如何解压缩文件夹中的所有文件?(不是.zip扩展名)

Powershell 如何解压缩文件夹中的所有文件?(不是.zip扩展名),powershell,zip,unzip,Powershell,Zip,Unzip,目前,我正在编写一个脚本,将错误压缩的PDF文件移动到某个文件夹。我已经做到了。接下来我需要开始工作,就是将压缩后的.pdf文件解压缩到另一个文件夹中 这是我的全部剧本。除了最后两行之外,所有内容都用于查找压缩的PDF文件并移动它们 在第一部分中,脚本检查文件夹中每个pdf文件的前几个字节。如果它们以“PK*”开头,则它们是zip文件并被移动到压缩文件夹。 对于每个PDF/zip文件,在 这些文件也需要移到同一个文件夹中。从那里,zip文件需要解压并重新定位到“解压” 最后两行用于解压缩 $pd

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

这是我的全部剧本。除了最后两行之外,所有内容都用于查找压缩的PDF文件并移动它们

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

这些文件也需要移到同一个文件夹中。从那里,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


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

就像@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
  • 对长参数使用
    哈希表。这使得代码的宽度更紧凑,更易于阅读
  • 使用
    #region
    #endregion
    对代码进行分组

移动文件时只需附加扩展名
.zip
move$\.FullName(连接路径$newLocation($\.Name+'.zip'))
听起来可以工作,我该如何实现它
Get ChildItem'Z:\Documents\16_-Med.\u-App\Auftraege\PDFPrzemek\Zip'.\124; Expand Archive-DestinationPath'Z:\Documents\16_-Med.\u-App\Auftraege\PDFPrzemek\Zip\unzip'-Force
谢谢你的贡献,但我真的不明白你的答案。。。你重写了我的整个剧本吗?如果是这样的话,检查PDF是否真的是zip的If语句现在消失了,我不知道如何再次实现它。是的,很抱歉没有更正确地解释一切。我将编辑我的答案,使其包含更多上下文。这是我关于这个主题的第一个问题,它也解释了一切。不,HL7文件不是ZIP文件。它不需要以任何方式修改。太好了!这工作几乎完美!我刚刚测试了它,ZIP文件中包含了3个文件,它们都被称为“scan.pdf”。这三个名字相同的人中只有一个被解压并移动到解压状态。你知道这可能是什么原因吗?非常感谢你的帮助!它们都被解压,但最后一个总是用相同的名称覆盖前一个。如果您不想这样做,您应该首先检查解压缩文件的名称,并在其上附加一个数字