[英]Powershell unzip file with .zip extension and move it to another folder
[英]How to unzip all files in folder? (Not .zip extension)
當前,我正在編寫一個腳本,用於將錯誤壓縮的PDF文件移動到某個文件夾。 我已經做到了。 接下來需要做的是將壓縮的.pdf文件解壓縮到另一個文件夾中。
這是我的整個劇本。 除了最后兩行以外的所有內容都專用於查找壓縮的PDF文件並移動它們。
在第一部分中,腳本檢查文件夾中每個pdf文件的前幾個字節。 如果它們以“ PK *”開頭,則它們是zip文件,並移至壓縮文件夾中。 對於每個PDF / zip文件,其旁邊的文件夾中都有一個關聯的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擴展名。 .zip是唯一適用於Expand-Archive
過濾器。
所以我需要找到一種方法來獲得此功能來解壓縮文件,即使它們沒有合適的擴展名...
就像@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
對代碼進行分組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.