My needs
My primary use of pocketpc is reading articles somewhere in underground on the way from 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 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:
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.
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.
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!") }