Showing posts with label .bak files. Show all posts
Showing posts with label .bak files. Show all posts

Friday, January 13, 2012

7zip and PowerShell

 

Hi,

first of all “Happy New year” to all of you.

yesterday one of my colleague asked me to write to write a script which can zip .bak files but the zip files should be in same folder which original .bak exists.

Logic behind this is to compress .bak files {normally SQL backups} in there current folders so that they are easy to find.

after spending 2-3 hours i am successfully able to get this done.

First you can download and installs the 7zip from this link : http://www.7-zip.org/download.html

You can download the whole script from here : http://dl.dropbox.com/u/17858935/PowerShell_%26_7Zip.zip

-------------------------------------------------------------------------------------------------------------------------------------------------------------

if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}

set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"

$filePath = "C:\Users\aman.dhally\Desktop\Desktop-2\ASD\Test-3"

$bak = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".bak" }

foreach ($file in $bak) {

                                  $name = $file.name

                                  $directory = $file.DirectoryName

                                  $zipfile = $name.Replace(".bak",".7z")

                                  sz a -t7z "$directory\$zipfile" "$directory\$name"    

                           }

--------------------------------------------------------------------------------------------------------------------------------------------------------------

you need to change the $filePath with your path .

You can use the same script to compress any files

Output Screenshots

1 

2 

3 

I hope it helps someone :)

Thanks

Aman Dhally