简体   繁体   中英

Unzip multiple zip folders with the original name

I am new at powershell and stuck at making code as I have around 20 zip folders located at D:\\Input . I want to unzip all the folders at D:\\Output with the original name of zip folder but with the following requirement that is for example I have zip folder located at D:\\Input with the name " ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826 " and it contains the following folder " 1ac7b-2d62-403c-8394-5bd33cbe7 ".

So when I unzip then I want the following folder at D:\\Output " 1ac7b-2d62-403c-8394-5bd33cbe7 ". After that script will rename this folder " 1ac7b-2d62-403c-8394-5bd33cbe7 " to the original zip folder that is " ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826 ". I have to the same step to 20 zip folders so I think for loop will be preferable. Can anyone please help me in this script

$ZipFilesPath = "D:\Input"
$UnzipPath = "D:\Output"

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($UnzipPath)

$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP

$progress = 1
foreach ($ZipFile in $ZipFiles) {
    Write-Progress -Activity "Unzipping to $($UnzipPath)" -PercentComplete (($progress / ($ZipFiles.Count + 1)) * 100) -CurrentOperation $ZipFile.FullName -Status "File $($Progress) of $($ZipFiles.Count)"
    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)


    $Location.Copyhere($ZipFolder.items(), 1040)
    $progress++
    $a= $ZipFile.Name
    Write-Host $a
}

As it's unclear upfront what the zip file contains, I suggest to

  • first create a folder in D:\\Output with the zip files BaseName and unzip there.

## Q:\Test\2019\05\23\SO_56279162.ps1
$ZipFilesPath =  "D:\Input"  # 'A:\Input'  #
$UnzipBase    =  "D:\Output" # 'A:\Output' #

$Shell = New-Object -com Shell.Application

$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP

$progress = 1
foreach ($ZipFile in $ZipFiles) {
    Write-Progress -Activity "Unzipping to $($UnzipPath)" `
      -PercentComplete (($progress / ($ZipFiles.Count + 1)) * 100) `
      -CurrentOperation $ZipFile.FullName `
      -Status "File $($Progress) of $($ZipFiles.Count)"

    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)

    $UnzipPath = Join-Path $UnzipBase $ZipFile.BaseName
    New-Item $UnzipPath -ItemType Directory | Out-NUll
    $Location = $Shell.NameSpace($UnzipPath)

    $Location.Copyhere($ZipFolder.items(), 1040)
    $progress++
}

Sample tree (on my RamDrisk A:)

> tree /F A:\
A:\
├───input
│       ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826.zip
│
└───Output
    └───ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826
        └───1ac7b-2d62-403c-8394-5bd33cbe7
                test.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM