Tuesday, December 11, 2007

Automating file copying with Windows Mobile and Powershell

My needs

My primary use of pocketpc is reading articles somewhere in underground on the way from r1 home to work and opposite. So I need some way to get data in my PDA. The way that advocates by something like iPhone or BlackBerry is direct connect from PDA to Internet.

My problems

But the problem that here, in Russia, we don't have this option. Internet connection by cellular networks are very expensive and r2 works not as well as wish. It has some reasons - mobile communication market here was saturated not so long ago and companies not think yet about Internet as way to increase revenue but their networks already overbusy. Wi-Fi is not widespread enough.

My choice

So I need some other way. In my case I prefer to save some interesting and big enough articles from morning newspaper and blogs to PDA's memory. And here I also have some choices to solve task. In my particular case I need to copy data to PDA a few times for a day from two computers - at home (when I'm going to work) and at work (when to home). So previously I use the following scenario:

r3

Surfing the web, reading mail I save interesting on memory card (or in directory and later copy to card) on PC through additional card reader or with embedded reader in notebook.

New problems

I was happy with this scenario but recently I changed my 2 gb capacity card to 4 gb capacity. This cause that card reader which I use for PC is no longer can help me - it doesn't understand my new card. So I have to enhance my scenario.

r4

While with notebook I work as earlier - from PC I copy directly to PDA. It means that I use dock station which come with my PDA. It's a bit interesting thing - when you connect this dock to your PC the tool called ActiveSync (or WMDC in vista) simulate your PDA as new disk in the system.

r5

But it's not truth - you can't access it with traditional I/O - you need to use special interface called RAPI. So if I previously in my Powershell script just move files from one storage to another then now I need to rewrite my script to work with RAPI.

It's may be the best thing about Powershell - if you use old cmd.com for scripting or even cygwin you can't access RAPI so simple - and with Powershell I can just use .NET wrapper around RAPI.

And here it is

As you can see this script contains Exception handling technique which different from usual in modern OO languages. It can seems to be not very necessary here, but it gives us opportunity to check problems when for some reasons (like no more free memory) we can't copy one file and hence must not delete it from PC - but still can copy smaller size file.

As for the rest we loads wrapper-library check device, check connection and copy files, when we can't connect we use another .NET library called Windows.Forms to show MessageBox about it.

[System.Reflection.Assembly]::LoadFrom("D:\work\powershell\OpenNETCF.Desktop.Communication.dll")| Out-Null $rapi = New-Object OpenNETCF.Desktop.Communication.RAPI $desktopPath = "C:\Documents and Settings\username\Desktop\2read\" $devicePath = "\SD-MMC card\docs"; echo "Connecting to device..." Function ConnectDevice(){ if($rapi.DevicePresent) { $rapi.Connect() } Write-Output $rapi.Connected } Function CopyFile($rapi,$file){ $script:copiedSuccessful=$true trap [Exception] { Write-Host Write-Error $("TRAPPED: " + $_.Exception.GetType().FullName); Write-Error $("TRAPPED: " + $_.Exception.Message); $script:copiedSuccessful = $false continue; } $deviceFile = $devicePath+"\"+$file.Name echo $file.FullName to $deviceFile $rapi.CopyFileToDevice($file.FullName,$deviceFile,$true) if($script:copiedSuccessful -eq $true) { Remove-Item -Path $file.FullName -force -verbose } } Function CopyFiles($rapi){ $files = Get-ChildItem -Path $desktopPath -recurse if($files -ne $null){ foreach($file in $files){ CopyFile $rapi $file } } else { echo "NO FILES FOUND" } } if(ConnectDevice){ CopyFiles($rapi) } else{ [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")| Out-Null echo "NOT CONNECTED!" [System.Windows.Forms.MessageBox]::Show("NOT CONNECTED!") }

Wednesday, December 5, 2007

Tools matter... sometimes

Commonly used in out team tool for SQL database works:

t2

and new for us tool that I test for some time:

t1

This screenshots are tree(or explorer)-like view of the same database (not our, db that we must integrate) at the same time. Do you see difference? Second tool show us in tree that one trigger is disable by shading icon, first can say us that only when we open that trigger (so to find that trigger from GUI we must open all).

It's a very little thing, but sometimes (as today) it's very helpful. Today we have a bug in that system. The first idea was that problem somewhere in trigger, but when try test this (by disable) not help to stop system fall. But problem was there! Developer just made mistake and disable not that trigger. And when I open second tool I notice that straight away.

So, don't believe people saying tools are unimportant, sometimes they wrong - and that times can give you a lot of headache, do you need that?

Sunday, December 2, 2007

Windows Live Writer install failed

Live Writer is definitely best windows tool for blogging. But as wellWriter_OverviewBeach_Graphic as many other ms products it has some bugs which with non-transparent behavior annoy some people.

So I have 2 problems with Live Writer.

 

Install failed

With release of version 2008 you must install LiveWriter from united Live installer - which downloads needed software during installation. And on my home PC it says failed without any additional information.

Workaround for problem - download installer, that contains writer - from here:

http://get.live.com/wl/all

Just select writer.

(400) Bad Request when upload post with images in blogger

The problem appears when post image through clipboard or in .png for example. So workaround - save images to disk in .jpg before add it to draft.

more about - here