2016-03-21 16 views
0

PowerShell 5.0 Compress-Archiveコマンドレットを使用して、ディレクトリ内の.configファイルを再帰的に取得し、ディレクトリ構造を維持しながら解凍する方法を教えてください。例:ファイル拡張子によるPowerShellの圧縮アーカイブ

Directory1 
    Config1.config 
Directory2 
    Config2.config 

目的は、上記のディレクトリ構造と設定ファイルのみを含む単一のzipファイルです。

+0

どういう意味ですか? config-fileを見つけ、それを圧縮し(config-fileのみ)、zip-fileをconfig-fileと同じ場所に残しておきますか? –

+0

私はイラストを追加しました。 –

+0

ファイル構造は唯一の明確な部分でした。希望の出力はどのように見えますか?すべての設定ファイルを含むzipファイルが1つ必要ですか?または設定ファイルごとに1つのzipファイル? –

答えて

2

ファイルを一時ディレクトリにコピーして圧縮することをお勧めします。例:

$path = "test" 
$filter = "*.config" 

#To support both absolute and relative paths.. 
$pathitem = Get-Item -Path $path 

#If sourcepath exists 
if($pathitem) { 
    #Get name for tempfolder 
    $tempdir = Join-Path $env:temp "CompressArchiveTemp" 

    #Create temp-folder 
    New-Item -Path $tempdir -ItemType Directory -Force | Out-Null 

    #Copy files 
    Copy-Item -Path $pathitem.FullName -Destination $tempdir -Filter $filter -Recurse 

    #Get items inside "rootfolder" to avoid that the rootfolde "test" is included. 
    $sources = Get-ChildItem -Path (Join-Path $tempdir $pathitem.Name) | Select-Object -ExpandProperty FullName 

    #Create zip from tempfolder 
    Compress-Archive -Path $sources -DestinationPath config-files.zip 

    #Remove temp-folder 
    Remove-Item -Path $tempdir -Force -Recurse 
} 
関連する問題