Hey, Scripting Guy! How Can I Retrieve Information About All the Shortcut Files in a Folder?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a folder with about 1800 shortcuts in it. I need to retrieve the file name and the target for each of these shortcuts in order to import that information into a database. Do you have any idea how I can retrieve this information for each and every shortcut file in a folder?

— JK

SpacerHey, Scripting Guy! AnswerScript Center

HeYy, JK. WwEELL, PRobbaly THe bESS – sorry; it’s HArdd to tyBE wiiith middEns ON.

Sorry; like we said (or tried to say) it’s hard to type with middens – uh, mittens – on. Anyway, probably the best way to –

What’s that? Why is the Scripting Guy who writes this column typing with mittens on in the first place? Well, truth be told, this column is being written on a Thursday morning, and the Scripting Guy who’s trying to write this column is still trying to warm up after sitting outside watching high school baseball on Wednesday night. Officially, the game-time temperature last night was 39 degrees Fahrenheit; however, thanks to the wind and the rain it felt a bit colder than that. How cold? Off the top of our heads, we’d say around 116 degrees below zero.

OK, so that is a bit of an exaggeration. In reality, it felt no colder than 103 or 104 degrees below zero. That leaves us a bit short of the coldest temperature ever registered on Earth: 128.6 degrees below zero, recorded at Vostok Station, Antarctica. The coldest temperature ever recorded in the state of Washington is minus 48 degrees Fahrenheit; fortunately that occurred in December of 1968, when there were no high school baseball games scheduled. Interestingly enough, the coldest temperature ever recorded in Hawaii is 12 degrees above zero. And even that was at the top of Mt. Mauna Kea, elevation 13,770 feet.

Yes, we wish we were in Hawaii right about now, too.

Note. Did we mention the fact that there were a few flakes of snow mixed in with the rain? Well, we should have, because there were. And that was fun; after all, you hardly ever get to see snow when you’re at a baseball game.

Of course, some of you may be thinking, “Sitting in the rain, wind, and cold and watching a high school baseball game? That doesn’t sound so bad.” To those of you who feel that way all we can say is this: the customer is always right.

Except in this case, when you’re 100% wrong: sitting in the rain, wind, and cold and watching a high school baseball game is absolutely no fun at all. No matter how much you like baseball. And regardless of whether you’re a Microsoft customer or not.

So just how cold was it last night? Well, you might say that it was colder than a witch’s – um, let’s just say that it was cold. Really cold.

Fortunately, though, things are a little better today; that’s because the Scripting Guy who writes this column is safely-cocooned inside his climate-controlled office.

Note. Well, pseudo-climate controlled. A year or so ago Microsoft installed individual thermostats in each office, but they appear to be placebos rather than actual thermostats. As near as we can tell, it doesn’t matter whether you set the thermostats to 40 degrees or 140 degrees; the temperature in your office remains exactly the same either way.

In fact, the Scripting Guy who writes this column has even thawed out enough to hammer out a script that can retrieve the file name and target for all the shortcut files in a specified folder:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colShortcuts = objWMIService.ExecQuery("Select * From Win32_ShortcutFile Where " & _
    "Drive = 'c:' AND Path = '\\documents and settings\\all users\\shortcut_files\\'")

For Each objShortcut in colShortcuts
    Wscript.Echo objShortcut.FileName
    Wscript.Echo objShortcut.Name
    Wscript.Echo objShortcut.Target
    Wscript.Echo
Next

Adkab dsgjdsa fli;

Sorry; the stocking cap dropped down over our eyes again. As you can see (assuming you don’t have a stocking cap covering your eyes) we start out by connecting to the WMI service on the local computer. Could we run this script against a remote computer? Of course we could; to do that we’d simply need to assign the name of the remote computer to the variable strComputer:

strComputer = "atl-fs-001"

After connecting to the WMI service we then use the following query to return a collection of all the shortcut files found in the folder C:\Documents and Settings\All Users\Shortcut_Files:

Set colShortcuts = objWMIService.ExecQuery("Select * From Win32_ShortcutFile Where " & _
    "Drive = 'c:' AND Path = '\\documents and settings\\all users\\shortcut_files\\'")

We should probably take a moment to examine this query in a little more depth. To begin with, note that we’re selecting items from the Win32_ShortcutFile class; as the name implies, this class contains instances of all the shortcut files found on the computer. In addition, we’ve added a Where clause with the following two conditions: 1) any returned files must be found on drive C (Drive = ‘c:’); and, 2) any returned files must be found in the folder Documents and Settings\All Users\Shortcut_Files.

For better or worse, this Where clause isn’t as straightforward as it could be. For one thing, we need to split the clause into two pieces: the drive must be referenced separately from the folder path. WMI won’t let you request files from the folder C:\Documents and Settings\All Users\Shortcut_Files; instead, you have to request files from drive C and the folder \Documents and Settings\All Users\Shortcut_Files (the complete path minus the drive letter).

Strange, but true.

Second, note that we have to double up each and every \ in the folder path:

\\documents and settings\\all users\\shortcut_files\\

Why? Well, as it turns out, the \ is a reserved character in WMI; in order to use a \ in a WMI query each such character must be “escaped.” Because the \ is also the escape character in WMI, that means we need to preface each \ with a second \. Thus a path like \Scripts\My Scripts\WMI\ has to look like this when used in a WMI query:

\\Scripts\\My Scripts\\WMI\\

Like we said: strange, but true.

Once we’ve nailed down the query the rest of the script is a piece of cake. After we get back a collection of all the shortcut files found in C:\Documents and Settings\All Users\Shortcut_Files we set up a For Each loop to loop through each item in that collection. Inside the loop we use the following block of code to echo back the shortcut FileName (the name of the file, minus the file extension), Name (complete file path, including the drive, folder, and file name and extension), and Target (the application/script/whatever that gets called any time the shortcut is clicked):

Wscript.Echo objShortcut.FileName
Wscript.Echo objShortcut.Name
Wscript.Echo objShortcut.Target
Wscript.Echo

From there it’s back to the top of the loop, where we repeat the process with the next shortcut in the collection. When we’re all done we should have an onscreen report that looks something like this:

InterVideo WinDVD
c:\documents and settings\all users\shortcut_files\intervideo windvd.lnk
C:\Program Files\InterVideo\WinDVD\WinDVD.exe

IT Connection Manager
c:\documents and settings\all users\shortcut_files\connection manager.lnk
C:\Program Files\InterVideo\WinDVD\WinDVD.exe

Zune
c:\documents and settings\all users\shortcut_files\zune.lnk
C:\Program Files\Zune\Zune.exe

Etc., etc.

That should do it, JK. As for the Scripting Guy who writes this column, he’s finally beginning to warm up. Which is good: by the time this column is published (Friday, March 28) he’ll be getting ready to head for yet another baseball game, an afternoon game to be played in Seattle. Here’s the current forecast for Friday afternoon:

Periods of rain. Becoming windy in the afternoon. High 43F. SSE winds at 5 to 10 mph, increasing to 20 to 30 mph. Chance of rain 90%. Rainfall near a half an inch.

A high of 43, winds up to 30 miles an hour, and a 90% chance of rain. Watch out, Vostok Station, Antarctica. Your days as a world record holder just might be numbered.

Speaking of which, where did we put those middens?

Sorry; mittens.

0 comments

Discussion is closed.

Feedback usabilla icon