0

I currently have a script that I am using to extract specific .xml files from some folders. However, I need to do the same thing with zip files in these same folders, and the Expand-Archive cmdlet can't do exactly what I need to do. Would anyone be able to offer any help? Here is my original script:

param (
    [string]$loannumber,
    [string]$mids
)

$ErrorActionPreference = "Continue"

$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$config = [xml][IO.File]::ReadAllText("$MyDir\MessageGatherConfig.xml")

#$loannumber = '1479156692'
#$mids = 'M1,M2,DUREQ'

$selectedmids = $mids.Split(",")

foreach ($mid in $selectedmids) {
    $filestocopy = @()
    Write-Host "Checking for $mid messages..."

    $midfile = ($config.MessageGatherConfig.MessageFilePatterns.FilePattern | Where-Object {$_.messageid -eq $mid})
    $pattern = $midfile.pattern

    $copyfiles = $false

    foreach ($path in $midfile.Path) {

        $searchval = $pattern.Replace("LOANNUMBER", $loannumber)

        Write-Host "Searching $path for $searchval"

        $dircmd = "dir /b $path\$searchval"
        $files = ""

        $files = cmd.exe /c $dircmd

        if ($files -ne $null) {
            $copyfiles = $true
            $files = $files.replace('[', '`[')
            $files = $files.replace(']', '`]')

            $files2 = $files.Split([Environment]::NewLine)

            foreach ($filename in $files2) {
                $filestocopy += "$path\$filename"
            }
        }       

    }

    if ($copyfiles) {
        Write-Host "Copying $mid files to local folder"

        if (Test-Path $MyDir\$loannumber\$mid) {
            Remove-Item $MyDir\$loannumber\$mid -Force -Recurse 
        }

        New-Item $MyDir\$loannumber\$mid -type directory
    }
}
7
  • 2
    Screenshots are not helpful. Please paste an example of the code you're using and tell how it doesn't work (error messages, etc.). Commented Sep 26, 2017 at 17:24
  • I just posted my entire original script. It's not that it doesn't work. I just need it to work for zip files as well, and I am uncertain of how to do that. Commented Sep 26, 2017 at 17:33
  • What does "work for zip files as well" mean? Commented Sep 26, 2017 at 18:37
  • My script already works when extracting specific files from folders. These same folders have zip files that I also need to extract specific files from as well. I am seeking help with editing my script in order to accomplish that. Commented Sep 26, 2017 at 18:54
  • Why does Expand-Archive not work for you? Commented Sep 26, 2017 at 19:08

1 Answer 1

1

I would go with a method of extracting the zip to a temp directory and then using the function above to copy the files that you need. This simple function will extract the content to a temp directory and return the path to the extracted content.

function extractZipToTemp () {
    Param (
        $ZipFilePath
    )

    # Generate the path to extract the ZIP file content to.
    $extractedContentPath = "$([System.IO.Path]::GetTempPath())$(([guid]::NewGuid()).tostring())"
    # Extract the ZIP file content.
    Expand-Archive -Path $ZipFilePath -DestinationPath $extractedContentPath -Force
    # Return the path to the extracted content.
    return $extractedContentPath
}

If you want the content to remain local to the directory you are executing the script from, just tweak the above function as follows.

function extractZipToTemp () {
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [String]$ZipFilePath,

        [Parameter(Mandatory = $true, Position = 1)]
        [String]$ExtractPath
    )

    # Generate the path to extract the ZIP file content to.
    $extractedContentPath = "$extractPath\$($ZipFilePath | Split-Path -Leaf)"
    # Extract the ZIP file content.
    Expand-Archive -Path $ZipFilePath -DestinationPath $extractedContentPath -Force
    # Return the path to the extracted content.
    return $extractedContentPath
}

With either of the aforementioned methods, remember to cleanup after copying the files that you need.

Sign up to request clarification or add additional context in comments.

2 Comments

I will give this a shot. Thank you, sir.
No problem. Just let me know if you need more assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.