0

I’ve written a program that creates some bookmark/favorites shortcuts (.url files). I’ve examined some of the bookmark files it creates and some of them are okay (figure 1), but some of them are displayed with a different icon (figure 2) and give an error message when trying to open them (figure 3).

I’ve checked the files with a text-editor and confirmed that they do indeed contain the appropriate bookmark data. I even created a few test bookmarks by dragging the shortcut directly from a browser to the folder and compared those to the files my program creates and they are bitwise identical.

I swear they used to all be fine, but now they just won’t seem to work anymore. What’s going on? How can I fix it?



Figure 1: Some of the generated bookmark files are good

Screenshot of folder with good generated bookmarks


Figure 2: A bunch of the generated bookmark files are broken (now?)

Screenshot of folders with broken generated bookmarks


Figure 3: The dialog claims that the bookmark has an invalid target even though it doesn’t

Screenshot of Invalid Shortcut Target error message

Synetech
  • 9,643
  • 9
  • 64
  • 96

1 Answers1

0

It turns out that this is happening because of a cache. Windows Explorer caches files (especially small, “system” files like shortcuts which include bookmarks). The problem is that it can cache them prematurely, so if the file is invalid (e.g., during development) or it gets cached before the file has finished being written (‽), then Explorer will continue to use that until the cache is cleared. Obviously one way to fix it is to simply clear the cache, but the only obvious way of doing that is to reboot (maybe also log out and back in). Of course this is not ideal, especially if you are still in the middle of development.

A knowledge-base article says it applies to Windows XP, Vista, and Server 2003 with Internet Explorer 7 and has a hotfix for them, but it happens with Windows 7 with IE9 as well, which has no hotfix (even though the hotfix was issued in October 2011, long after Windows 7 was released).

An easy way to work around this is to rename either the bookmark file, or the containing folder. Either way, Explorer sees it as a “new” file and ignores the cache.

A common concern is that you cannot simply rename the file or folder every time that you run the program (well you can, but it’s not ideal). Therefore you will want to resolve the issues that cause Windows to cache the bad files. Here’s a few tips on doing this:

  • Reduce the amount of time between the creation of the empty .url and writing its contents. For example, instead of opening/creating the file, then building the contents as strings and writing them to the file, consider building the contents (which are simple and small anyway) as a string before opening the file. Then dump the contents and close it. This makes it more likely that by the time that Windows notices the file, it will already have its contents.

  • Check that the file encoding is not Unicode, BOM, etc. Now that Unicode characters are acceptable in URLs, a .url file will not be ASCII-only, it might have some UTF-8 characters, but the whole thing won’t be wide-chars.

  • Check that your line-endings are CR+LF. When writing to the file, it is easy to simply append a \n, but you should use \r\n like Windows and browsers do.

  • Make sure that the file does not contain illegal characters. It shouldn’t normally be possible, but it can happen.

In addition, some bookmarks will naturally have the generic globe icon and throw an error when you try to open them. This is true for any bookmarks that link to different protocols which are not set up in the system to be opened via bookmarks:

  • Chrome internals pages like chrome://version
  • Bookmarklets like javascript:alert('Woot!');
  • P2P protocols like ed2k://… if the protocol is not registered with the system
Synetech
  • 9,643
  • 9
  • 64
  • 96