Joben.Rara.blogs(".NET")

There are two ways to write error-free programs; only the third one works.

"Clean up sources before packing them" by SBC Packers

I recently did a walkthrough of the Web Service Software Factory for a group of developers. They are considering its use for their Web Services development. I used the accompanying Hands-On-Lab for the exercise and made some minor modifications to make it "fit" their current environment.

After the walkthrough, I needed to send them a copy of the HOL including the changes I made. That should be easy enough, right? Compress and Send. First, I need to delete all the \bin and \obj directories to keep the compressed file's size to a minimum. Problem is, there are about 10 exercises and each of the solution folder is about 3 levels deep in each exercise folder. I'm lazy so I need to find an easier way. I could have used "Clean Sources" but that would mean running away from a good challenge of doing the same thing using PowerShell [:-)].

Here's my PowerShell script for deleting the \bin and \obj directories under all those solution folders.

The canonical version

get-childitem * -recurse | where-object { $_.PSIsContainer -eq $true } | where-object { $_.Name -eq "bin" -or $_.
Name -eq "obj" } | foreach-object { remove-item -path $_.FullName -recurse -whatif }

This should read like this: "Get everything in this directory and its sub-directories. Select Items which are containers (a.k.a. directories). Select folders whose names are "bin" or "obj". For each folder, delete the folder and all the files/folders that it contains.

The "readable" version (using aliases)

dir * -recurse | where { $_.PSIsContainer -eq $true } | where { $_.Name -eq "bin" -or $_.Name -eq
"obj" } | foreach { remove-item -path $_.FullName -recurse -whatif }

BTW, remove the -whatif in the end if you actually want the command to execute. Keeping the -whatif in allows you to "see" what would happen without actually executing the command.

Groove to Snap's I've got the Power [;-)] Man, I'm really showing my age.

Posted: 05-15-2007 5:43 PM by velocity | with 3 comment(s)
Filed under:

Comments

cruizer said:

SBC Packers, di ba sa kay Rex Navarrete yan? :D

# May 15, 2007 6:45 PM

keithrull said:

Great sample velocity! Before PS i was doing all this in a batch file because it was not as enjoyable as typing it. with PS you tend to type more because you get to do alot more. I remember seeing Jon Galloway on our usergroup meeting with his Powershell and SMO demo and my jaw dropped instantly.

# May 15, 2007 6:46 PM

velocity said:

Hi Keith,

Moving forward, PS is going to be present as an admin tool in every server product that MS ships. Exchange has it now. The new System Center products also come with PS commands bundled with it. With SQL2005, loading SMO into PS is, as you've seen, possible. I guess Katmai will have PS commands when it comes out. Scripting support is part of the Common Engineering Criteria.

Yes cruizer, Rex is the "grand Packer of them all". :-)

# May 15, 2007 9:07 PM