Categories
Code Snippets

Internet Shortcuts to HTML file

While trawling around the web, I often save shortcuts to websites that I’ve visited .

Each shortcut is saved as a special filetype with the suffix .url and is recognised by Windows as an Internet Shortcut. The name of the file is a description of the link (usually) taken from the contents of the webpage title html tag and the contents of the file include the actual url.

For example,

openEyes – Eye tracking for the masses.url

has the following contents

[InternetShortcut]
URL=http://thirtysixthspan.com/openEyes/

I came across a technique for combining one or more .url files into a single .html file originally described on the mozillaZine website (http://mozillazine.org) in the forum post

Convert links: desktop shortcut to bookmark?

This describes a method using a DOS batch/cmd file to automate the process (I’d forgotten how powerful DOS batch/cmd files can be). Here is my modified version, saved into a file called url2html.cmd:


@echo off
setlocal enabledelayedexpansion

cd /d "%~dp1"
set output="LINKS_%RANDOM%.html"
echo ^<ol^> >> %output%
for /f "tokens=* delims=" %%t in ('dir /b "%~dp1*.url"') do (
   set strLine2=%%t
   type "%%t" | find "URL=" > u2htemp
   set /p strLine1= < u2htemp
   echo ^<li^>^<a href="!strLine1:~4!"^>!strLine2:~0,-4!^</a^>^</li^> >> %output%
)
echo ^<^/ol^> >> %output%
pause

del u2htemp

This code can be run as follows from a command line prompt:

C:\mutils\url2html.cmd "c:\urls\openEyes - Eye tracking for the masses.url" 

and will add all url files in the same directory of the specified url to a single html file.

It is set up in this way so that it can be easily invoked from Windows File Explorer from right clicking on a .url file and selecting the Send to option. This can be enabled as follows:

Open the Send to special folder ( e.g. by using WindowsKey-R and typing shell:SendTo ) and dragging a shortcut to your newly created .cmd file there. The command url2html will then be added to the Send to option.

There are some interesting points to note regarding some of the commands used in this script.

setlocal enabledelayedexpansion

I found a description of this, with an example, at EnableDelayedExpansion

Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.

Variable expansion means replacing a variable (e.g. %windir%) with its value C:\WINDOWS

By default expansion will happen just once, before each line is executed. The delayedexpansion is performed each time the line is executed, or for each loop in a FOR looping command. For simple commands this will make no noticable difference, but with loop commands like FOR, compound or bracketed expressions delayed expansion will allow you to always see the current value of the variable.

When delayed expansion is in effect, variables can be immediately read using !variable_name! you can still read and use %variable_name% but that will continue to show the initial value (expanded at the beginning of the line).

Another interesting concept is the handling/interpretation of arguments passed to the cmd file.

cd /d "%~dp1"

This is used to attach to the directory containing the url file passed as an argument when executing the cmd file.
The modifier %~dp1 expands the first argument – the url filename and extracts the drive letter and path.

Details can be found from Microsoft at
Using batch parameters