Hey, Scripting Guy! The Scripting Wife Works with Special Folders
Microsoft Scripting Guy Ed Wilson here. It is a quiet day; the calm before the storm so to speak. The 2010 Scripting Games begin tomorrow. The PoshCode.org site is ready for the games. The 2010 Scripting Games forum is ready, and all the judges and commentators have been contacted, and all of the events are ready to go. All that needs to occur now is for the clock to strike midnight, and the Games will be underway. It is an exciting time to see so many months of work finally come to fruition. I am sitting in the kitchen with my new copy of The Three Musketeers and a freshly brewed pot of English Breakfast tea as I try to take my mind off of the Scripting Games for a little while and mellow out. Unfortunately, it does not appear that this brief repose is to last—I hear footsteps.
“Here, little Script Monkey. Where’s my little Script Monkey?” the Scripting Wife sang out.
I resisted the urge to reprove her for calling me Script Monkey—I think that was her plan. Instead, I hid in the kitchen pantry, hoping she would pass by and I would escape with my day of rest still intact. Unfortunately, she must have seen my still steaming cup of hot tea, and she knew I had not gone too far.
“There’s my little Script Monkey! What are you doing in the pantry? Silly little Script Monkey,” she chastised mockingly.
I decided not to play along with her, deciding instead to take the direct approach and perhaps still escape with my dignity intact. “Good morning, my dear. Fancy seeing you up so early. Would you like me to fix breakfast for you? I could perhaps make you an omelet,” I offered.
“Nope. Nope. Nope,” she exclaimed in rapid staccato. “I have plans for you,” she said as she grabbed my wrist and led me off in the direction of her computer.
“I need you to tell me about special folders,” she said as she sat down at the computer.
“Well, special folders are special,” I began.
“Don’t be silly,” she chastised, “Obviously they are special. What makes them so?”
“Well, they are special because they can be referenced by name instead of path. Examples of special folders include favorites, mymusic, or mypictures. Because the exact location of these special folders varies depending on the version of the operating system or the person who is logged in, we need an easy way to access the data commonly stored in those folders. This is where special folders come in. Make sense?” I asked.
“Yeah, it makes sense. The folders move around, and you need an easy way to find them,” she summarized. “But how do I know if I have a special folder or not?”
“To get a list of all the special folders, you can use the GetValues static method from the System.Enum .NET Framework class,” I began.
“Oh no! There you go again,” she exclaimed in mock anguish. “Don’t get all geeky on me. It is too early in the morning.”
“Okay. It is easy. Open the Windows PowerShell ISE, and type this: [enum]::GetValues([environment+SpecialFolder]),” I said.
“That is better. Kind of an ugly command. Now can I click the green triangle and run it?” she asked.
“It didn’t work. It gives me a bogus error,” she said. “It says I am missing a closing parenthesis but I have one.”
“Two things. You left out the square brackets around environment+SpecialFolder; and it is SpecialFolder not SpecialFolders,” I said.
“Okay. I’ve got it now,” she said.
“Awesome. Good job. Now if you want to retrieve the path to a specific folder, such as the desktop, you use the GetFolderPath static method from the [environment] class. You do this by putting it in square brackets and using two colons. Just like the previous command. You need to type [environment]::GetFolderPath(“Desktop”),” I said.
“If you want to see the path to the mymusic folder, you type [environment]::GetFolderPath(“mymusic”),” I said.
“Ok,” I have it.
You can follow us on Twitter or Facebook. If you have any questions, send e-mail to us at firstname.lastname@example.org or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys