Monthly Archives: January 2011

Polling the existence of a file with PowerShell

Sometimes you run into a situations where a given task spawns a separate thread and completes it’s work on that separate thread. Eg: sending a bit XMLA to SQL Server Analysis Services with Microsoft.AnalysisServices.Deployment.exe and then waiting for the processing to be completed. Anyway, here is a simple function that will wait untill a given file exists:

function WaitForFile($File) {
 while(!(Test-Path $File)) {
  Start-Sleep -s 10;
 }
}

Get current file in PowerShell

A while ago i wrote a small script to take care of deployment. Configuring the source folders went as following:

param(
	$BaseDir = (Get-Location).Path,
	$WebDir = (Resolve-Path "$BaseDir\web"),
	$DatabaseDir = (Resolve-Path "$BaseDir\database")
)

The problem with this code is that it only works when your current working directory is set to the location of this script. An administrator (or build system) invokes the script as following:

PS C:\Users\Admin>& 'D:\Deployments\20110124\Deploy.ps1';

Because we don’t want to annoy the consumer of our script with the burden of making sure he is in the correct directory we modified our code as following:

param(
	$BaseDir = (Split-Path $MyInvocation.MyCommand.Definition),
	$WebDir = (Resolve-Path "$BaseDir\web"),
	$DatabaseDir = (Resolve-Path "$BaseDir\database")
)

A quick win :)

Get entire message body from an Intent

I recently started programming the Android and noticed that most examples for processing an incoming SMS are not entirely correct.

An SMS message is limited to 160 characters. Current mobile phones break up a larger message in multiple messages transparently for the user. When Android notifies you about an incoming SMS it has all parts (of that large message) available. So here is how you reconstruct the entire message body from an Intent

Bundle bundle = intent.getExtras();        
if (bundle == null) return;

StringBuilder message = new StringBuilder();
Object[] pdus = (Object[]) bundle.get("pdus");

// Rebuild this entire message from the multi part smses/pdus
for (Object pdu : pdus){
 // Notice that i use the deprecated android.telephony.gsm.SmsMessage
 // android.telephony.SmsMessage throws when i call createFromPdu 
 SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);                
 message.append(msg.getMessageBody().toString());
}