Wednesday, August 20, 2014

PS: Remove folder recursively

Recently I have to clean up my .Net solution with a long list of projects. One of cleanup is to remove all bin and obj folders. It is very tedious doing it manually.

I turn to Powershell Script to find a quick way. Soon I got my solution, only two lines of codes and I could do in PS console interactively:

Here is the function:

$a = Get-ChildItem -Path H:MyRepository\Solution\ -Recurse | Where-Object {$_Name -Match '^obj$' }
$a | { rm -r $_.FullName }

Repeat the same steps to remove bin folders.


Friday, May 09, 2014

TnDao: Chinese Version of TED

TED is my favorite web resources I watch for talks about new ideas and views, technical or none-technical topics. Actually, I was involved in its translation program as volunteer and I did some video subtitle translations when I found some excellent talks.

I knew there is Chinese TED, which is called as TEDToChina. This one has been very active. Most contents there are from English TED talks with Chinese translation. Many Chinese like this web resource to open to new ideas, concepts,  and views. I have couple friends there based on strong interest in TED.

To my surprise, two days ago, I found another similar version of TED, totally in Chinese, from China. It is TnDao. It initially started in 2011 from Shanghai. Now it makes its great impact wave to Beijing. The most interesting thing of TnDao is that all the talks are concentrated on issues in China.

I watched some vedio presentations in past two days. I could not stop watching after starting one. They are really eye-open views about current China. Some make me excited, but a lots of them make my heart sad, and heavy. There are so many social issues like poor children, drags, and others. It is good to know that there are so many intellectual, talent and influential people from all levels in China realize those issues and they invest their time, efforts and money to deal with.

I just by-chance know about TnDao from Weibo, Chinese Twitter and Ximalaya, a voice based social network like internet radio. Web, internet and mobile apps are in big wave in China. There are so many people already using those technologies and services. I often talk to my English speaking friends and co-workers that now days Chinese is a very important language in social life with technology.


  • TnDao, Chinese version of TED with truly Chinese topics.
  • Ximalaya, Listen to what you want, anywhere and anytime, Internet Radio with huge contents of voices in Chinese.


Thursday, May 08, 2014

.Net var (C#) Almost Everywhere, One Exception

Starting from .Net 3.0, Microsoft introduced type var in C#, as a simply way to binding strongly type. This is a very convenient way to define variables. Basically, .Net will be smart enough to infer correctly data type. It makes code maintenance and update much easy and no need to change data type in case variable types changed. I like it very much and use var almost everywhere.

However, recently I found one case this var is not able to infer explicit data type. In the case of dynamic delegate, you have to use explicit delegate type:

var myDelegate = delegate(int valueA, int valueB) { return valueA + valueB; };

.Net cannot infer what delegate type is for myDelegate. You have to use the explicit delegate type in this case:

delegate int SumOfTwoValues(int v1, int v2);
SumOfTwoValues myDelegate = delegate(int valueA, int valueB) { return valueA + valueB; };

I found this interesting exception when I was sharing my knowledge about delegate with my co-worker.


Tuesday, April 08, 2014

iOS 7 Training Course by Stanford University

iOS 7 training course, Developing iOS Apps for iPhone and iPad, offered by Paul Hegarty, University of Stanford was published on Oct 28, 2013 at iTunesU. I did not realized its availability until today. Actually WWDC 2014 will be on June 2, 2014. New iOS features will be announced in about two months time.

Here is the description of the course:

Updated for iOS 7. Tools and APIs required to build application for the iPhone platform using the iOS SDK. Users interface designs for mobile devices and unique user interactions using multi-touch technologies. Objective-oreiented design using model-controller paradigm, memory management, Objective topics includes: objective-oriented database API, animation, multi-threading and performance considerations.

Prerequisites: C language and objective-orianted programming experience

Recommended: Programming Abstractions.

Offered by Stanford School of Engineering and released with Creative Commons BY-NC-SA licenses.  If you have disability-related accessibility question, please email

Couse Outline

I. Couse Introduction, Overview of iOS, MVC Objective-C
II. Xcode 5
III. Objetive-C
IV. Foundation, Attributed Strings
V. View Controller Lifecycle

This course is on the top of Cources in iTunesU.

Today I finished the first course. I am going to update this blog with my progress.

NameFinished DateNotes
1. Class Logistics, Overview of iOS, Objective-C2014-04-08MVC is explained in 1st lesson!
2. Xcode 52014-04-09Created card matching game (Matchismo) project in Xcode 5
3. Objective-C2014-04-09Updated the project with model class CardGame class. Demonstrated communication between Controller and Model & View in Xcode.
4. Foundation and Attributed Strings2014-04-09More on Objetive-C, id, introspection(query id class type and method implementation; Foundation: NSObject, NSArry, NSNumber, NSData, NSDIrectionary, NSUserDefaults, NSRange, UIFont UIColor, NSAttributedString(new in iOS 7).
5. View Controller Lifecycle2014-04-10Continue on attributed string with UITextView (new attributed string for the view). View controller lifecycle and Notification (radio station communication mechanism) with new Attributor demo project.
6. Polymorphism with Controllers, UINavigation, UITableBar2014-04-10Polymorphism: Subclass and abstract class; MVCs controlled by navigation or tab controllers.
7. View and Guestures2014-04-11Views are most often constructed in Xcode graphically. Override drawRect:for customized UIView class, to draw your own staff.  Use UIBezierPath to draw.  Drawing text: UILabel, NSAttributedString.

UIGuestureRecgonizer  for accept inputs: pan, pinch,  rotation, swipe, and tap.

Demo: SuperCard: draw card in customized view class, and handle swipe gesture to turn card, pinch gesture to scale card image.
8. Protocols, Blocks and Animation2014-04-12First two are very important coding practices in Objective-C. Block of codes can be attached to methods as a way for customized implementation. iOS 7 adds two ways to do animation, view animation methods and animator with behaviors. Demo DropIt project shows the power of animations.
9. Animation and Autolayout2014-04-13Continued the previous demo on animation: less tippy, add trap to dropping view with line.
Autolayout is powerful in iOS 7. Paul explained three ways to do it in Xcode: 1. use guidelines, 2. tools in Xcode bottom, and 3. control-click.
Demo: update previous Attibutor project with auto layout, simple and nicely done!

10. Multithreading, Scroll View2014-04-13iOS 7 utilizes blocks to make multithreading much easy to manage and control. Demo project imaginarium shows downloading image with multithreading technique and scroll view to host image with zooming feature.
11. Table View and iPad2014-04-14Table view has been in iOS since iPhone first version. It is a very popular way to show data. Paul showed activity indicator for table view. This may be something new in iOS 7. The second part is about iPad. Demo: universal app Shutterbug displaying photos from Flikr.
12. Documents and Core Data2014-04-14Document is used for storage, here Core data is using it as storage. Core data has been in iOS for long time, but it has been updated extensively. Xcode provides working nice UI and classes for data creation, update, deletion and query. Paul explains basics in a clear layout logic.
13. Core Data and Table View2014-04-15This lecture contains a lot of information about core data, radio notification pub and sub, application delegate, and background execution. All are explained quickly in Photomanian project.
14. UIApplication, Network Activity2014-04-16Briefly on UIApplication: sharedApplication and delegate. One property networkActivityVisible, maybe confusing when you have a lots of activities and turning it on or off. Demo Photomanian, iPad, show photos, and popover. Maps: core classes for Maps. Tip on working on iPad or iPhone, copy and paste storyboard!
15. MapKit and Embed Segue2014-04-16Quickly through MapKit classes. Continued to Photomanian demo, iPhone: replaced PhotoByPhotographer's tableview with mapvuew; iPad: two ways to show maps and photos, first one similar to iPhone's flow out, and second embed view by embed segue, very cool!
16. Modal Segue and Text Field2014-04-17Last segue in this lecture. The only difference is unwise segue, set action to storybord green icon in view. Text field is for text input in one line. Demo: Photomanian, add photos from camera (no camera information in this class) by modal segue, alert view, cancel or add a photo to photographer.

Also showed how to start CLocation manager and to get location of latitude and longitude. 
17. Camera, Core Motion, Application Lifecycle2014-04-18Camera: continued to demo Photomaian to add "Take photo" to new photo view.

In second half, Paul explained Motion APIs first, then he demoed a project Bouncer with no storyboard, all in codes, redBlock and blackBlock to bounce based on animation and motion manager. 
18. Localization, Adding UI to Settings2014-04-18Paul showed tick to localize strings: search for @" in .m, #define for localization, and then gesturing in finder to generate strings. Xcode provides UI for stroryboard localization. Use . for localized strings. Demo: Photomanian localization

For settings, Paul uses Bouncer as demo to as slider for alacrity setting.


Monday, April 07, 2014

PS Function: Convert Collection of PSObjects

PS Table-view is a very useful view to present collection of PSObjects. This works well with PSObejects with just simple and single value properties, such as name, age, telephone number etc. However, if there is any properties with collections, the layout may be too for for those long list of collection objects.

I find out a great way to convert collection of PSObjects with collections as its properties. It works very well to break collection of values as multiple lines in column so that the layout of table view is much tight and clean.

Here is the function:

#FileName: UtilCollection.ps1
Function ConvertPSObjectCollection {
       Gets a converted collection of PSObjects so that the converted
   collection can be nicely displayed in table-view.

       This function will convert a collection of PSObjects with
   one or more array properties to a collection of PSObjects so
   that the colleciton can be output to a nice and tight table view.
   The input collection contains PSObjects with one or more array
   property element. Normally array elements are too wide to be displayed
   in a table-view. By converting collection, the returned colection
   can be viewed nicely in table-view.
   This function is based on SO solution

   .PARAMETER CollectionPSObjects
       Mandatory. Collection of PSObjects, PSObject have one or more
   property of array data.

       Parameters above

       A collection of converted PSObjects with same prorperties
   but array elements as rows in the collection so that the
   collection can be nicely formated output to a table view.

       Version:        1.0
       Authors:        David Chu
       Creation Date:  10/04/2014
       Purpose/Change: Initial function development

       $myCol = @(
       (New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@(1,2,3);'others'=@('SampleA1','SampleA2')}),
       (New-Object –TypeName PSObject –Prop @{'id'=@('02a','02b');'name'='b';'items'=@(1,2,3);'others'=@('SampleB1','SampleB2','SampleB3','SampleB4','SampleB5')}),
       (New-Object –TypeName PSObject –Prop @{'id'='03';'name'=@('c1','c2');'items'=@(1,2,3);'others'='SampleC'})

 $myCol1 = ConvertPSObjectCollection $myCol
 $myCol1 | FT ID,Name,Items,Others -AutoSize
Param (
[Parameter(Mandatory=$true)] $CollectionPSObjects
Process {
 $m_result = $CollectionPSObjects | %{
     $Current = $_
     $Members = $_|GM|?{$_.MemberType -match "Property"}| `
     Select -ExpandProperty Name
     $Rows = ($Members|%{$current.$_.count}| `
     sort -Descending|Select -First 1)-1
     For($i=0; $i -le $Rows;$i++){
         $LoopObject = New-Object PSObject -Property `
       @{$($Members[0]) = if($Current.$($Members[0]).count -gt 1) { `
           $Current.$($Members[0])[$i] `
         } else{ `
           if(!($i -gt 0)){ `
             $Current.$($Members[0]) `
           }else{ `
             $Null `
           } `
         } `
         If($Members.Count -gt 1){
                 Add-Member -InputObject $LoopObject `
           -MemberType NoteProperty `
           -Name $_ `
           -Value $(if($Current.$_.count -gt 1) { `
             $Current.$_[$i] `
             }else { `
               if(!($i -gt 0)) { `
                 $Current.$_ `
               }else{ `
                 $Null `
               } `
             } `

 return $m_result



Wednesday, April 02, 2014

Three Shortcuts of PowerShell Script

Recently I have been writing PS codes at work. PS is a really good and powerful script language in Windows environment. I have never learned it systematically or from basic concept to advanced level.  Based on my other programming skills, I just pick it and use it at work. In most cases, I just do the internet search for solutions if I don't have clue how to resolve my problems. Therefore, I have been on and off depending on my work requirements.

Still I don't know some very basic concepts. Here are three most commonalty used code cases:


I just use them based on other people's codes. For these three shortcuts, I have trouble to google explanations. Finally, last week, I got an answer from SO.

%{...}: ForEach{}
@{...}: Constructor for hash table
?{...}: shortcut for Where {....}


  • SO comment discussion on Francis Padron's quetsion.