Add files of one type to one zip file and clean up using PowerShell

后端 未结 2 1757
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 07:21

How can I create a PowerShell script to get all files of type .BAK and add them to a ZIP file (can I have my PowerShell script take a parameter when called to control the name o

相关标签:
2条回答
  • 2021-01-24 07:54

    If you're using PowerShell v5 then you can use the Compress-Archive function instead of using 7zip:

    $filePath = "C:\SQLBackups"
    $ZipName = "v4.14.4_backups.zip"
    
    $BakFiles = Get-ChildItem -Recurse -Path $filePath -Include *.bak
    
    $BakFiles | Compress-Archive -DestinationPath "$filePath\$ZipName"
    $BakFiles | Remove-Item -Force
    
    0 讨论(0)
  • 2021-01-24 07:54

    You name the archive after each file you're processing in the loop, hence each file goes into its own archive. If you want to put all files into the same archive, define the archive name once outside the loop:

    $zipfile = Join-Path $filePath 'v4.14.4_backups.zip'
    foreach ($file in $bak) {
      sz a -t7z $zipfile $file.FullName
    }
    
    0 讨论(0)
提交回复
热议问题