How Can I Return the URLs Associated with My Internet Explorer Favorites?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I return the URLs associated with my Internet Explorer Favorites?

— DW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DW. It’s time for annual performance reviews here at Microsoft, which means we Scripting Guys are supposed to be looking back and detailing all the many things we’ve done in the past year. Unfortunately, instead of finding things we did do it’s much easier for us to find things that we didn’t do. Take this question, for example. You’d think we would have already written a script that returns the URLs for all of your Internet Explorer Favorites, wouldn’t you? And yet, for some reason, we never have. We must have been too … busy ….

Or something.

On the bright side, if we hurry we can get this column in before this year’s review deadline. That way we’ll at least have one accomplishment for the year. That might not sound like much, but it’s one more accomplishment than we usually have.

With that in mind, let’s take a look at a script that returns the name and the associated URL for all the links in the Internet Explorer Favorites folder:

Const FAVORITES = &H6&

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.Namespace(FAVORITES)

For Each objItem in objFolder.Items If objItem.IsLink Then Set objLink = objItem.GetLink Wscript.Echo objItem.Name Wscript.Echo objLink.Target Wscript.Echo End If Next

Gee; if we had known this script was going to be that easy we would have written it a long time ago. As you can see, there isn’t much to the code. We start out by creating a constant named FAVORITES and setting the value to &H6&; we’ll use this constant to tell the script which special folder (Internet Explorer Favorites) we want to connect to. Next we create an instance of the Shell.Application object, then use the Namespace method to bind to the Favorites folder. Notice that, by using this approach, we don’t have to know the actual path to the Favorites folder; the Shell object will go out and find the folder for us.

Our next step is to check all the items in the Favorites folder and then return the URL for any links found there. To accomplish that task we first set up a For Each loop to loop through all the items in the folder. (How do we get at that collection of items? They’re all part of the Items property for the folder object.) That’s what we do here:

For Each objItem in objFolder.Items

Inside the For Each loop we use the IsLink property to determine whether or not the item in question is a shortcut file. If IsLink is True we then use the GetLink method to retrieve the shortcut properties for the file. Complicated? No; that requires just two lines of code:

If objItem.IsLink Then
    Set objLink = objItem.GetLink

After retrieving the shortcut properties we then echo the shortcut Name and Target, with Target representing the URL associated with the shortcut. We then loop around and return the name and URL for the next link in the Favorites folder. That’s all there is to it.

Well, that’s a good point: as handy as this might be we probably still need something besides this script to put on our performance review. On top of that, the script shows only “top-level” links; that is, links found within the Favorites folder itself. Suppose you have subfolders within the Favorites folder. Will this script be able to open up those subfolders and retrieve information about the links found there? Well ….

But that’s OK; we can fix that. Here’s a revised script that uses a recursive subroutine to retrieve information from any subfolders found in the Favorites folder. We won’t discuss this script – or the idea of recursion – in any detail today; for more information on writing recursive functions or subroutines take a look at the Recursion section in the Microsoft Windows 2000 Scripting Guide. For now, we’ll simply point out that the script starts out exactly the way our previous script does: it binds to the Favorites folder and walks through the collection of items, checking to see if each item is a link.

In this revised script, however, after we check to see if each item in the folder is a link we then use the IsFolder property to determine whether or not that item happens to be a folder. If the item is a folder, we call a recursive subroutine named GetSubFolderLinks, passing the folder object as the sole parameter to the subroutine. Inside the subroutine we then use the GetFolder method to bind us to that subfolder.

Once we’ve made a connection to the subfolder we can then walk through the items found in there, retrieving the name and URL for any links, and even calling the GetSubFolderLinks subroutine should our subfolder contain sub-subfolders. We realize that’s a bit hard to grasp: how can we call the GetSubFolderLinks subroutine when we’re already in the GetSubFolderLinks subroutine? All we can say is this: that’s the whole idea behind recursive functions and subroutines. Take a look at the Scripting Guide for more information.

Or, just run the following script and don’t worry too much about it:

Const FAVORITES = &H6&

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.Namespace(FAVORITES)

For Each objItem in objFolder.Items If objItem.IsLink Then Set objLink = objItem.GetLink Wscript.Echo objItem.Name Wscript.Echo objLink.Target Wscript.Echo End If If objItem.IsFolder Then GetSubFolderLinks(objItem) End If Next

Sub GetSubFolderLinks(objItem) Set objSubFolder = objItem.GetFolder For Each objSubItem in objSubFolder.Items If objSubItem.IsLink Then Set objSubLink = objSubItem.GetLink Wscript.Echo objSubItem.Name Wscript.Echo objSubLink.Target Wscript.Echo End If If objSubItem.IsFolder Then GetSubFolderLinks(objSubItem) End If Next End Sub

And hey, no need to thank us, DW: for the Scripting Guys, it’s all in a year’s work.

Um, all in day’s work ….

0 comments

Discussion is closed.

Feedback usabilla icon