|Note:||This batch file is showing its age. It was created for MS-DOS and Windows 9x back when Windows NT 4 was only just beginning its conquest of the corporate PCs.
Don't expect it to work on Windows NT 4, 2000, XP or Server 2003 without modifications.
In fact there is no need to use these ancient techniques in the "modern" Windows versions, as CMD.EXE has much more powerful string parsing features with its FOR /F command.
As an example of the techniques discussed here, we'll set up a batch file to convert MS Internet Explorer "favorites" files to a single Netscape "bookmark" file.
Consider the format of IE's "favorites" files.
Every "favorite" URL is stored in a separate file named after that same URL, and containing two or more lines:
[InternetShortcut] URL=http://www.redhat.com/support/docs/rhl/intel/rh51-hardware-intel.html Modified=C0357FC608BBBD01BB
Now let's have a look at the way Netscape stores its "favorites" or "bookmarks".
They are stored together in one single HTML file called BOOKMARK.HTM:
<!DOCTYPE NETSCAPE-Bookmark-file-1> <!-- This is an automatically generated file. It will be read and overwritten. Do not edit! --> <TITLE>'s Bookmarks</TITLE> <H1>'s Bookmarks</H1> <DL> <p> <DT><A HREF="http://www.redhat.com/index.html" ADD_DATE="907880848" LAST_VISIT="907880843" LAST_MODIFIED="907880843">Red Hat Linux</A> </DL> <p>
First we'll have to isolate the line starting with "URL=" from IE's "favorite".
To that end we'll use the FIND command:
TYPE "Red Hat Linux.URL" | FIND "URL="
This will show the next line on screen:
Our next step is to remove the string "URL=" from the actual URL.
To that end we'll first redirect the "URL=" line to one temporary batch file, named TEMP1.BAT:
TYPE "Red Hat Linux.URL" | FIND "URL=" > TEMP1.BAT
We need a second temporary batch file, named URL.BAT, to split that URL line in two and store the resulting string in a variable:
@ECHO OFF SET URL=%1
How can this batch file remove "URL=" from a line?
It does so because DOS' command interpreter ignores the "=" in the command and replaces it with a space.
So DOS reads the line "URL=http://..." as "URL http://..."
|Note:||DOS only ignores the "=" if it is the last character of the command, not if it is located within the parameters string!|
Now comes a hard part: instead of creating this second temporary batch file once and keeping it in the same directory or in a path directory, we'll let our primary batch file create both temporary batch files the moment it needs them.
ECHO @ECHO OFF>URL.BAT ECHO SET URL=%%1>>URL.BAT
As you can see, both lines of the original batch file are now preceded by "ECHO ", the "%" is replaced by "%%" and ">URL.BAT" was attached to the end of the first line, and ">>URL.BAT" to the second line.
Just echoing the lines and then redirecting them to URL.BAT is not enough. That way, "%1" would not be redirected literally, but it would be interpreted first.
Adding an extra "%" prevents interpretation of "%1", so that a it will be redirected to URL.BAT.
Only when URL.BAT executes will "%1" be interpreted.
Combining the different techniques we found so far, our batch file now looks like this:
@ECHO OFF TYPE "Red Hat Linux.URL" | FIND "URL=" > TEMP1.BAT ECHO @ECHO OFF>URL.BAT ECHO SET URL=%%1>>URL.BAT CALL TEMP1.BAT PROMPT $LA HREF="%URL%"$GRed Hat Linux.URL$L/A$G %COMSPEC% /C >> BOOKMARK.HTM
It will add the next line to BOOKMARK.HTM:
<A HREF="http://www.redhat.com/index.html">Red Hat Linux.URL</A>
Now let's create BOOKMARK.HTM's header first:
PROMPT $L!DOCTYPE NETSCAPE-Bookmark-file-1$G %COMSPEC% /C >> BOOKMARK.HTM PROMPT $L!-- This is an automatically generated file. %COMSPEC% /C >> BOOKMARK.HTM PROMPT It will be read and overwritten.$_Do not edit! --$G %COMSPEC% /C >> BOOKMARK.HTM PROMPT $LTITLE$G's Bookmarks$L/TITLE$G %COMSPEC% /C >> BOOKMARK.HTM PROMPT $LH1$G's Bookmarks$L/H1$G %COMSPEC% /C >> BOOKMARK.HTM PROMPT $_$LDL$G$LP$G %COMSPEC% /C >> BOOKMARK.HTM
Next we need to add the bookmarks:
SET PROMPT= $LDT$G$LA HREF="%URL%"$G%1$L/A$G %COMSPEC% /C >> BOOKMARK.HTM
Note that we use "%1" for the URL's description now. "%1" is the file name of the IE "favorite". This leaves an annoying ".URL" at the end of the URL's description. I am interested in any suggestions to remove it without the need for external utility programs.
Note, too, that the bookmarks we place will not contain any ADD_DATE, LAST_VISIT or LAST_MODIFIED entries in the tag.
It would be fairly easy to add the LAST_MODIFIED entry, using the same techniques we used to retrieve the URL from IE's "favorite" file, but I'll leave that to you for a rainy day.
To close BOOKMARK.HTM after all bookmarks have been added, we need one more line at the end:
PROMPT $L/DL$G$Lp$G %COMSPEC% /C >> BOOKMARK.HTM
To finish our project take a look at the sources of all batch files involved.
CNVRTURL.BAT is the main batch file that should be called from the command line; ADDURL.BAT is a secondary batch file called from within CNVRTURL.BAT.
CVTURLNT.BAT is the same batch file adapted for Windows NT and converted into one single file.
CV2URLNT.BAT is a more recent update to the Windows NT version, using escape characters instead of the PROMPT command to display < and > characters.
If you like to experiment, you could try to integrate ADDURL.BAT into CNVRTURL.BAT, using the same techniques we used to integrate URL.BAT into ADDURL.BAT. Don't forget to check URL.BAT if you do, because it will have to be generated by a batch file that was itself generated by another batch file.
Depending on your operating system and its configuration you may need to adjust the part of CNVRTURL.BAT located before the :Header label, since different operating systems use different default directories and have different ways of handling long file names.
Besides that, I have not yet found a way to display the prompt that would work in every operating system, so you may have to experiment.
But after reading and understanding the current page, and maybe some of the other pages on this and other web sites, that shouldn't pose much of a challenge anymore.
Consider yourself an expert!
I hope you enjoyed this project as much as I did!
page last uploaded: 2018-12-20, 10:51