Where is my Desktop?

DesktopProperties

 

Some people tend to save their documents and files on a separate drive.  This allows the flexibility of flattening the Windows/OS partition at any time, without losing any data.  This is one reason I have never been a fan of using the %USERPROFILE% folders (eg. C:Users<UserName> on Vista or C:Documents and Settings<UserName> on XP).

It turns out, though, that you can redirect these ‘special’ shell folders easily on Vista, or with the Tweak UI PowerToy for XP (or hacking up the registry).  These folders include things like “My Pictures,” “My Music,” “Favorites,” or even your “Desktop” folder.  On Vista, for instance, when you right-click the Desktop folder and select Properties, you get the dialog on the right.

If you’ve done this, obviously it’s not good enough to assume that your desktop is in %USERPROFILE%Desktop.  You’ve got to open up the registry to find the location of these “User Shell Folders”:

set OUTPUTDIR=%USERPROFILE%Desktop

set SHFOLDER_REGISTRY_KEY = "HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders"
for /f "tokens=2*" %%i in (
    ‘REG QUERY %SHFOLDER_REGISTRY_KEY% /v Desktop’
) do (
    call set OUTPUTDIR=%%~j
)

Notes:

  • We’re querying HKCU, so no elevation on Vista is required.
  • Most of values underneath this key are of type REG_EXPAND_SZ.  This is why we need to ‘call’ to expand the value.
  • Bonus points if you can tell me why I start with token #2.

In some cases, you may also find that there is a “DesktopDirectory” value as well as a “Desktop” value underneath that key.  I cannot eloquently explain the difference, but perhaps you may be able to interpret Wikipedia’s explanation:

The "Desktop" virtual folder is not the same thing as the "Desktop" special folder. The Desktop virtual folder is the root of the Windows Shell namespace, which contains other virtual folders.

I believe that DesktopDirectory is the correct value to use, but sometimes it is not available.  Most of the time the two values are equivalent.

I bet you someone on the shell team could explain this properly, if he hasn’t already.  I’m sure there’s an interesting story behind it.

Extension Methods and LINQ

I know very little about the new features of C# 3.0 and LINQ.  I’ve been playing around with it a little bit and was puzzled by how it all works.  This is probably old news for a lot of people, but it’s all new to me.  For instance, let’s take the simplest possible query:

int[] numbers = { 4, 8, 15, 16, 23, 42 };
var query = from n in numbers where IsPrime(n) select n;

Trying to compile, I got this compiler error:

Error: Could not find an implementation of the query pattern for source type ‘int[]’.  ‘Where’ not found.  Are you missing a reference to ‘System.Core.dll’ or a using directive for ‘System.Linq’?

You might get this error if you’re missing some references (as the error message says).  I was getting it due to some other external issue.  But in trying to fix/investigate the issue, I first tried to figure out what exactly the compiler was doing behind the scenes.  The method ‘Where’ isn’t available on IEnumerable or Array, so it seemed like a valid error.  What was I missing?

I couldn’t find any good information online as to how the compiler actually takes the LINQ syntax and generates code that resembles something more IL-ish.  There are plenty of sample queries online, but I couldn’t find any good site that really explains how it all works.  So after a bunch of digging I managed to piece together that the compiler actually translates the above query to something like this:

var query = numbers.AsQueryable().Where(n => n.IsPrime());

Interesting.  That kind of explains where the error message is coming from.  But still, I couldn’t for the life of me figure out how the heck this worked.  There’s no ‘AsQueryable’ method on Array or IEnumerable, or ‘Where’ on IQueryable either.  Where are these methods coming from?

It turns out that the compiler further translates the query to something like this:

IQueryable<int> query = Queryable.Where<int>(Queryable.AsQueryable(numbers), delegate(int n) { return IsPrime(n); });

How the heck?!  Where did the Queryable class come from?

Apparently there is this new concept of “extension methods” in C#.  You can add methods to existing types without creating a new derived type, or recompiling, or modifying the original type.  Wow!!  Far out!

System.Linq has a static class ‘Queryable’ that does exactly this; it extends IEnumerable and IQueryable<T>.

public static IQueryable AsQueryable(this IEnumerable source);
public static
IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate);

All you have to do is add the directive “using System.Linq;” to take advantage of this functionality.  Check out the rules on how to implement a custom extension method.

And now it all makes sense.  Very cool!

Entertain the Cat

 

It’s pretty cool to see what people can do (or dream of doing) with the things you’ve built.  I’ve been trolling the Live Mesh forums on and off, and so far, my absolute favourite thread has been one entitled "Entertain the cat" (reproduced here).


 Web Guy on Thursday, May 15, 2008 8:55:45 AM

very cool we can have 2 remote connections going at the same time!

My mesh has 3 PCs – while at work I have found great fun watching my cat race from room to room via webcam while I switch between desktops to play a cat meow wave file.  Great way to check in on the house to see how he spends his day.

I love the mesh! 


Proposed AnswerProposed Answer

 Bleak Morn on Thursday, May 15, 2008 9:45:55 AM

Yeah, I do the same thing with my wife.


 WilliamStacey MVP on Thursday, May 15, 2008 5:10:50 PM

Web Guy said:

very cool we can have 2 remote connections going at the same time!

My mesh has 3 PCs – while at work I have found great fun watching my cat race from room to room via webcam while I switch between desktops to play a cat meow wave file.  Great way to check in on the house to see how he spends his day.

I love the mesh! 

Now get some of the USB Nurf missile launchers and really drive that cat nuts (and/or the wife).  I have to get a pc and cam in the family room now.


 Stephen Boots MVP on Thursday, May 15, 2008 6:01:22 PM

Bleak Morn said:

Yeah, I do the same thing with my wife.

LOL! That’s just *not* nice! 🙂
-steve


I especially love how Bleak Morn‘s post is the Proposed Answer.

Hello, World!

Mike Zintel’s got an excellent post on the history of the computing world, entitled "Why Sync?"  It’s an engaging read.  I’m struggling to properly excerpt.  Go read the whole thing.

yang on yahoo!

I’ve never followed Yahoo! much, though I just read Jerry Yang’s memo to Yahoo! staff.  Apparently he’s never heard of the ‘Shift’ key.  Does anybody else feel that it’s incredibly hard to read?  It turns out this is old news, but I just noticed.  Yuck!  At least Ballmer can write intelligibly, even if he does prance around on stage like a monkey.

‘Live Mesh’ Unveiled!

Finally!

In April 2006, I decided to leave my job on Messenger Server to join a completely new start-up team within Microsoft. It was a pretty big change for me, as I went from a well established team with millions of users, to being one of the first three developers on a team with nothing but a dream of what was to be. I couldn’t talk about what the team’s charter was, as it was all under wraps, even internally within the company. My business card had the somewhat cryptic team name.

Exactly two years and three weeks later, we are finally launching a Tech Preview and I can finally talk about (some of) the stuff I’ve been working on for the past two years. What a relief! I can’t believe it’s been such a long time.

I won’t talk about what I think Live Mesh is yet. You’ll find a great deal of information on the Team Blog, as well as videos on Channel 9 this week (Ray’s interview is a good one). I can’t do justice to what Mesh is by excerpting, so please go read our GM Amit’s post. It gives a good idea as to what exactly Mesh is.

We’re only launching a Tech Preview at the moment, so there’s a limited number of invites. Hopefully you’re well connected enough to get someone to share with you. If you’re already provisioned in the system, feel free to share stuff with me by inviting me to a “LiveFolder.”  I’d love to see what people are using Mesh for!

Mesh it up!!

Revamped

So 2⅔ years after creating this blog on a whim, I have decided that I should finally update the colour theme and layout.  Though I liked the red and black contrast with the flame background, I realize that it made for horrible reading on the eyes.  (If you missed it, it kind of looked like this, except the fonts were smaller.)

It was pretty fugly.

I can’t say that I really cared, since I could count my audience on one hand, and most of them were reading in a feed reader anyway.  I’ve picked another one of the default Spaces themes that’s a little easier on the eyes, and tweaked it ever so slightly.  Of course, I’ve kept the flames as my profile image too.  What do you think?  I’ll probably continue tweaking over the next several weeks (like adding a module or two).

I hear rolling thunder.

Date Time

I do many more things than batch scripts all day, and someday (soon) I’ll blog about some of that stuff, but for now, this will have to suffice.

I had several lines of batch code I created several years ago that created a unique filename from the current date/time with the help of two environment variables:

C:> echo %DATE% %TIME%
Tue 04/08/2008 22:48:43.84

So the code I had:

set NOW=%DATE% %TIME: =0%
set NOW=%NOW::=-%
set NOW=%NOW:.=-%

set YEAR=%NOW:~10,4%
set MONTH=%NOW:~4,2%
set DAY=%NOW:~7,2%
set NOW=%YEAR%-%MONTH%-%DAY%_%NOW:~15%

gave a nice:

C:> echo %NOW%
2008-04-08_22-48-43-84

which would allow you to sort files by filename in chronological order.  It worked quite well and I had used it for years.  Recently though, I started seeing some weird behaviour from some people.

C:> echo %NOW%
Tue-8/-00_22-48-43-84

This left me scratching my head for a while, until I asked one of these people to run the following on their machine:

C:> echo %DATE% %TIME%
04/08/2008 Tue 22:48:43:84

Gosh darn those locale settings!  It’s not enough to handle just this specific case, as in Canada for instance, dates are written DD/MM/YYYY as opposed to the US’s MM/DD/YYYY.  So I went online and found all sorts of strange ways to figure out the format of the date.  The best way I read of, to get locale was by crawling through the registry.  I wasn’t particularly motivated to do this, so left it at that.  Fortunately this particular script got deprecated in favour of new one I wrote that didn’t require date/time uniqueness.  Saved!

Progressive Dots

I just found out about this (thanks to Live Search!) from a fellow Canadian’s blog entry "batch file snippets" and it’s got me super excited.

Haven’t you ever found it annoying that you can’t print to a line without a newline (carriage return/line feed) in a batch script?  (At least, I didn’t know how to.)  Well, now you can!

For instance:

echo Copying files .

for %%f in (A B C D) do (
    echo .
    xcopy %%f %DEST% /cqy 2>&1>NUL
)

would give you:

Copying files .
.
.
.
.

as opposed to a progressive:

Copying files ….

So what you actually want is:

set /p CRLF=Copying files .<NUL
for %%f in (A B C D) do (
    set /p CRLF=.<NUL
    xcopy %%f %DEST% /cqy 2>&1>NUL
)

Don’t tell me that’s not cool!  How does it work?  The ‘/p’ option given to set is asking for user input:

SET /P variable=[promptString]

The /P switch allows you to set the value of a variable to a line of input entered by the user.  Displays the specified promptString before reading the line of input.  The promptString can be empty.

The key is that it prompts for input *on the same line*!  And by redirecting the NUL device into it, you get an immediate return.  How absolutely clever.

I love it!

To Sync, or not to Synch?

I tend to nitpick.  I have a bit of OCD when it comes to grammar and spelling.  I cringe when people use "it’s" instead of "its" or "your" instead of "you’re".*  I find it distasteful that people allow themselves to butcher a language (though I suppose non-native speakers can be given a bye).  So it’s not surprising that I have fairly strong feelings about the abbreviation for the word "synchronize" (or "synchronise", as you prefer).

It really bothers me when people write "synch".  I realize that both "sync" and "synch" are commonly accepted abbreviations; however the latter irks me to no end.  Why?  I read "synch" as rhyming with "lynch".  I realize that there are words that end with -ch with a hard ‘k’ sound (say, "loch"), but how many words end with -ynch (or -inch) with a hard ‘k’ sound?!  If you’re going to cut a word in half, at least have the decency to do it so that it follows general conventions of pronunciation.

I do also realize that languages are a fluid subject that are influenced by many factors.  (I hate the impact texting has had on the language–‘IMO’.)  Here’s an interesting tidbit from The American Heritage Book of English Usage:

The American Heritage® Book of English Usage.
A Practical and Authoritative Guide to Contemporary English.  1996.

7. Pronunciation Challenges: Confusions and Controversy

§ 45. Ch

In Old English the sounds (k) and (ch) were both represented by the letter c. Later, under the influence of French spelling, Middle English scribes inserted an h after c to indicate the (ch) sound at the beginning of words, as in child. (The sequence tch became the usual way to represent this sound following short vowels, as in catch.) In English words of Greek origin the digraph ch represents a transliteration of Greek X (chi), and so is usually pronounced (k), as in chorus, architect. And in English words borrowed from French, ch is often pronounced (sh), as in charlatan, cachet.

[ Interestingly enough, I don’t really like using the abbreviation in the present participle as in, like "syncing".  I tend to write "sync’ing" to denote the abbreviation, though I recognize that this is definitely my own oddity. ]

* NB.  Some smart aleck will point out that periods and commas go inside the quotation marks.  I, for one, despise this notation, and as this is apparently only a US custom and not generally followed in the UK or Canada, I take the liberty not to do so for my own edification.