2016-03-21 18 views
0

Bir dizinde .config dosyalarını yinelemeli olarak almak ve dizin yapısını korurken bunları sıkıştırmak için PowerShell 5.0 Compress-Archive cmdlet'ini nasıl kullanabilirim? Örnek:PowerShell Dosya Uzantısına Göre Sıkıştır-Arşivle

Directory1 
    Config1.config 
Directory2 
    Config2.config 

amacı da yukarıda dizin yapısını ve tek yapılandırma dosyalarını içeren tek bir zip dosyası.

+0

Ne demek istiyorsun? bir yapılandırma dosyası bulun, zip (sadece yapılandırma dosyası) ve zip dosyasını yapılandırma dosyasıyla aynı konumda bırakın. –

+0

Bir örnek ekledim. –

+0

Dosya yapısı, sadece net kısım olan tbh idi. İstenen çıktı neye benziyor? Tüm yapılandırma dosyalarını içeren bir zip dosyası ister misiniz? Veya yapılandırma dosyası başına bir zip dosyası? –

cevap

2

Dosyaları geçici bir dizine kopyalamanızı öneririm ve bunu sıkıştırır. Ex:

$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 
} 
İlgili konular