Extract Specific Filetypes From Multiple Zips to one Folder in Powershell

前端 未结 3 1576
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 02:20

I have Several zip files that Contain multiple filetypes. The ONLY ones I am interested in are the .txt files. I need to extract the .txt files only and place them in a fold

3条回答
  •  长情又很酷
    2020-12-12 02:35

    Here is one approach:

    1. Go through each .zip file in a folder.
    2. Extract archive into separate folder.
    3. Extract .txt file from folder.
    4. Copy files into destination folder containing all .txt files. This will overwrite files if they already exist in the destination folder.
    5. Cleanup extracted folders once finished.

    Demo:

    function Copy-ZipArchiveFiles {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [ValidateScript({
                if (-not(Test-Path $_ -PathType Container))
                {
                    throw "The source path $_ does not exist. Please enter a valid source path."
                }
                else
                {
                    $true
                }
            })]
            [string]$Path,
            [Parameter(Mandatory=$true)]
            [ValidateScript({
                if ([string]::IsNullOrEmpty($_.Trim()))
                {
                    throw "The Destination path is null or empty. Please enter a valid destination path."
                } 
                else 
                {
                    $true
                }
            })]
            [string]$Destination,
            [Parameter(Mandatory=$false)]
            [AllowNull()]
            [AllowEmptyString()]
            [AllowEmptyCollection()]
            [string[]]$Include
        )
    
        # Create destination folder if it doesn't already exist
        if (-not(Test-Path -Path $Destination -PathType Container))
        {
            try 
            {
                New-Item -Path $Destination -ItemType Directory -ErrorAction Stop
            }
            catch
            {
                throw "The destination path $Destination is invalid. Please enter a valid destination path."
            }
        }
    
        # Go through each .zip file
        foreach ($zipFile in Get-ChildItem -Path $Path -Filter *.zip)
        {
    
            # Get folder name from zip file w/o .zip at the end
            $zipFolder = Split-Path $zipFile -LeafBase
    
            # Get full folder path
            $folderPath = Join-Path -Path $Path -ChildPath $zipFolder
    
            # Expand .zip file into folder if it doesn't exist
            if (-not(Test-Path -Path $folderPath -PathType Container)) 
            {
                Expand-Archive -Path $zipFile.FullName -DestinationPath $folderPath
            }
    
            # Copy files into destination folder
            foreach ($file in Get-ChildItem $folderPath -Include $Include -Recurse)
            {
                Copy-Item -Path $file.FullName -Destination $Destination
            }
    
            # Delete extracted folders
            Remove-Item -Path $folderPath -Recurse -Force
        }
    }
    

    Usage:

    Copy-ZipArchiveFiles `
        -Path "C:\path\to\zip\files" `
        -Destination "C:\path\to\text\files" `
        -Include "*.txt"
    

    Note: Could also use this for multiple extension types as well by passing -Include *.txt, *.pdf. I also went a bit overboard in the parameter error checking, but I believe in writing robust code. Good habit to get into when writing your own cmdlets anyways :)

提交回复
热议问题