2017-09-26 6 views
0

現在、一部のフォルダから特定の.xmlファイルを抽出するために使用しているスクリプトがあります。しかし、私は同じフォルダ内のzipファイルで同じことをする必要があり、Expand-Archiveコマンドレットは私がする必要があることを正確に行うことができません。誰でも助けてもらえますか?ここに私の元のスクリプトは次のとおりです。PowerShellスクリプトを使用して.zipファイルから特定のファイルを抽出する

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 
    } 
} 
+2

スクリーンショットは役に立ちません。使用しているコードの例を貼り付けて、それが動作しないことを伝えてください(エラーメッセージなど)。 –

+0

私は元のスクリプト全体を投稿しました。うまく動かないわけではありません。私はそれだけでもzipファイルのために働く必要があります、そして、私はそれを行う方法が不明です。 –

+0

「zipファイルでもうまくいく」とはどういう意味ですか? –

答えて

0

私は一時ディレクトリにzipファイルを解凍して、必要なファイルをコピーするために上記の関数を使用する方法となるだろう。この単純な関数は、コンテンツをtempディレクトリに抽出し、抽出されたコンテンツにパスを返します。

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 
} 

あなたは、コンテンツは、あなたが以下のようにちょうど上記の機能を微調整、からスクリプトを実行しているディレクトリにローカルのままにしたい場合。前述の方法のいずれかで

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 
} 

、必要なファイルをコピーした後、クリーンアップすることを忘れないでください。

+0

私はこれを撃つでしょう。ありがとうございます。 –

+0

問題ありません。より多くの援助が必要な場合は、私に知らせてください。 – ekeeling

関連する問題