The following Powershell script compresses all the .BAK files using winrar.
1: cd D:\MSSQL\MSSQL.1\MSSQL\Backup
2: $dirs = get-childitem|where{$_.PSIsContainer}
3: foreach ($dir in $dirs) {
4: cd $dir
5: $files = get-childitem *.bak
6: foreach ($file in $files) {
7: #Check for null
8: if ($file -ne $NULL){
9: #write-host $file.Name.Replace(".bak", "")
10: & "c:\program files\winrar\rar" a $file.Name.Replace(".bak", "") $file.Name
11: remove-item $file
12: }
13: }
14: cd ..
15: }
Walk through:
1. Change directory to the backup root directory
2. Get all the subdirectories and iterates through each one
3. Get all *.BAK files and iterate through them
4. Use the winrar command line to compress the file
5. remove the .BAK file
Happy Powershelling!