Thursday, February 17, 2011

Linq with XML literals

Every once in awhile I write something in VB that feels like the days-long-gone when I did crazy-kung-fu Perl/Ruby.  It's a strange mix of being proud, shocked, and slightly embarassed, but thrilled that the problem is fully solved.  Is it ugly - like a pitbull with lipstick, youbetcha!  But still, for those who really dig Linq and think that XML literals in VB are pretty slick, check this out - it converts a System.Data.DataTable to an XHTML table that I could then use in a quick-and-dirty e-mail alert I needed to send out based on the results of some ad-hoc queries.

Public Shared Function ToHtml(ByVal dt As DataTable) As System.Xml.Linq.XDocument
 Dim resultHtml = _
  <?xml version="1.0"?>
  <table name=<%= dt.TableName %>>
   <tr class="row0"><%= dt.Columns.Cast(Of DataColumn)().Select(Function(cl, idx) <th class=<%= "col" + (idx + 1).ToString() %>><%= cl.ColumnName %></th>) %></tr>
   <%= dt.AsEnumerable().Select(Function(rw, rowidx) <tr class=<%= "row" + (rowidx + 1).ToString() %>>
   <%= dt.Columns.Cast(Of DataColumn)().Select(Function(cl, idx) <td class=<%= "col" + (idx + 1).ToString() %>><%= rw(cl.ColumnName) %></td>) %>
   </tr>) %>

 Return resultHtml
End Function

Monday, February 14, 2011

Birthday kitty

Happy birthday Caméra.  You have undeniably made me into a cat person.

Sunday, February 13, 2011

My battle with iTunes continues

My battle with iTunes as the official worst-music-management-software-I've-ever-used continues.  I'm moving music from my old computer to my new computer and I'm trying to figure out how to keep my metadata.  Since I don't let iTunes manage my music as much as possible, I've just copied .mp3 files over.  Well, of course that means that I lose my album art work, which means I have to ask iTunes to get it for me again - and of course it never gets it right.  I wound up using Dropbox to save the correct artwork from my old machine for each one that iTunes got wrong and then album by album tediously corrected it on the new machine.  and Point to Me, but I still wind up the one who loses :(

However, now comes time to figure out my star ratings.  This one was a real head scratcher, until my Bing searches brought me to an answer - here's the process:

On the old computer, make 5 playlists, one for each star rating and add your music to those lists.  Then, export the playlist from the old computer and import into the new one.  It doesn't matter where the files reside, just that iTunes knows about them.  Then, select all the songs in each of those playlists and assign the star rating for all the songs in each playlist.  Genius!  The whole process took less than 10 minutes and made me much less frustrated than I thought I would be, and way easier than the album artwork issue.  Point to iTunes.  The stalemate continues...

Saturday, February 12, 2011

Dropbox and Junctions

If you haven't tried Dropbox, it's quite the nice little utility. It's 2GB of free online storage for whatever. Just sign up, download the app, and drop files in your Dropbox folder and they get synced so that you can access them from anywhere. There's even mobile apps so you can get at your files on your iPhone or Andriod device. Nice.

The main trouble people seem to have with it is that they don't want to store their files in their dropbox folder, so they have to copy their files from the original place and try to keep them synced.  Here's a little trick I've been using and it works great.  Windows Vista and above has a built-in feature called junctions.  Junctions, for those initiated in the Unix world, are essentially hard links.  Basically the concept is you have a folder for a few photos here: "C:\Users\MattMc3\My Photos\Vacation Pix 2011".  You'd like those to by synced via dropbox, which is located here: "C:\Users\MattMc3\Dropbox".  You can create a junction from your Dropbox folder into your Vacation Pix folder and when you delete from one, you'll delete from both.  When you change one, you'll change both.  When you add to one, you'll add to both.  The folders are the same... you don't have a copy, you have a mirror.

The magic is in a little DOS command called mklink.  On your Windows 7 or Vista computer with dropbox already installed, do the following:

  1. Start Menu -> type "cmd" to get to a command line
  2. Change your directory to your dropbox folder by typing cd "C:\Users\MattMc3\Dropbox"
  3. Type: mklink /J Vacation Pix "C:\Users\MattMc3\My Photos\Vacation Pix 2011"
  4. Watch in awe as Dropbox picks up your files and syncs them.  It doesn't matter which folder you drop a file, it'll sync both ways because this isn't a copy - it's a mirror. 
 Now, 2GB really isn't enough to store a lot of photos, but I have been running VisualSVN as my source code repository for awhile and I wanted to get it synced to Dropbox.  This is source code all the way back to Pascal code from High School, C++ from college, and personal projects from 10+ years of .NET.  I don't really need a 2-way mirror backup, I just wanted to keep C:\SVN as my repository location and still get backups.  Thanks to junctions, I can.