unit B2FMain;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, Registry;<br><br>type<br> // Record for use as list element<br> PUrlRec = ^TUrlRec;<br> TUrlRec = record<br> Rep: String; // Folder name (folders and links)<br> UrlName: String; // Logical URL name (links only)<br> UrlPath: String; // www URL name (links only)<br> Level: Integer; // Folder level (position in folder tree)<br> end;<br><br>type<br> TMainForm = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Label1: TLabel;<br> Edit1: TEdit;<br> Label2: TLabel;<br> Edit2: TEdit;<br> OpenDialog1: TOpenDialog;<br> Button4: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> procedure Button4Click(Sender: TObject);<br> private<br> BookmarkList: TStringList;<br> L: TList;<br> LastFolder: String;<br> TargetFolder: String;<br> LvlList: TStringList;<br> procedure GetFavoritesFolder(var Folder: String);<br> procedure Convert(Folder: String);<br> procedure ScanBmkLine(I: Integer; Lvl: Integer);<br> function FindFolder(HtmlStr: String; var Lvl: Integer): Boolean;<br> function FindUrl(HtmlStr: String; Lvl: Integer): Boolean;<br> function FindSectionEnd(HtmlStr: String; var Lvl: Integer): Boolean;<br> procedure MakeFavorites;<br> function MakeFolder(Folder: String): Boolean;<br> function MakeUrlFile(UrlName, UrlPath: String): Boolean;<br> procedure ReplaceIllChars(var S: String);<br> end;<br><br>var<br> MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses<br> IniFiles, B2FAbout;<br><br>{---------------------- TMainForm ---------------------}<br><br>procedure TMainForm.GetFavoritesFolder(var Folder: String);<br>// Find the directory of the Internet Explorer favorites<br>var<br> Registry: TRegistry;<br>begin<br> Registry := TRegistry.Create;<br> Registry.RootKey := HKEY_CURRENT_USER;<br> Registry.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);<br> Folder := Registry.ReadString('Favorites');<br> Registry.Free;<br>end;<br><br><br>function SortFunction(Item1, Item2: Pointer): Integer;<br>// Function to sort the list of bookmarks alphabetically<br>var<br> Rec1, Rec2: PUrlRec;<br>begin<br> Result := 0;<br> Rec1 := Item1;<br> Rec2 := Item2;<br> if Rec1.Rep < Rec2.Rep then Result := -1;<br> if Rec1.Rep > Rec2.Rep then Result := 1;<br> if Rec1.Rep = Rec2.Rep then<br> begin<br> if Rec1.UrlName < Rec2.UrlName then Result := -1;<br> if Rec1.UrlName > Rec2.UrlName then Result := 1;<br> if Rec1.UrlName = Rec2.UrlName then Result := 0;<br> end;<br>end;<br><br><br>procedure TMainForm.Convert(Folder: String);<br>var<br> I: Integer;<br>begin<br> L := TList.Create;<br> LvlList := TStringList.Create;<br> LvlList.Add('');<br> try<br> ScanBmkLine(0, 0); // Find all bookmarks recursively<br> finally<br> L.Sort(SortFunction); // Sort the resulting list<br> MakeFavorites; // Make favorites from the list<br> // Clean up<br> for I := 0 to L.Count -1 do<br> Dispose(PUrlRec(L));<br> L.Free;<br> LvlList.Free;<br> end;<br>end;<br><br><br>procedure TMainForm.ScanBmkLine(I: Integer; Lvl: Integer);<br>// Recursive method to scan all lines in the bookmark file<br>begin<br> if I < BookmarkList.Count -1 then<br> begin<br> if not FindSectionEnd(BookmarkList, Lvl) then<br> if not FindUrl(BookmarkList, Lvl) then<br> FindFolder(BookmarkList, Lvl);<br> ScanBmkLine(I+1, Lvl);<br> end;<br>end;<br><br><br>function TMainForm.FindFolder(HtmlStr: String; var Lvl: Integer): Boolean;<br>// Retrive folder name (if any) from HTML string<br>const<br> FolderSubStr: String = '<H3';<br>var<br> J, Idx: Integer;<br> S: array[0..255] of Char;<br> UrlRec: PUrlRec;<br> I: Integer;<br> Folder: String;<br> FName: String;<br>begin<br> J := Pos(FolderSubStr, HtmlStr);<br> Result := (J <> 0);<br> if J <> 0 then // Substring found, retrieve folder name<br> begin<br> Inc(Lvl); // 1 level up<br><br> FillChar(S, SizeOf(S), 0);<br> Idx := 1;<br> for J := 1 to Length(HtmlStr)-1 do<br> if HtmlStr[J] = '>' then<br> Idx := J+1;<br> for J := Idx to Length(HtmlStr)-5 do<br> S[J-Idx] := HtmlStr[J];<br><br> Folder := S;<br> ReplaceIllChars(Folder);<br><br> if LvlList.Count > Lvl then<br> LvlList[Lvl] := Folder // Replace folder hierarchy<br> else<br> LvlList.Add(Folder);<br><br> FName := LvlList[Lvl];<br> for I := Lvl-1 downto 1 do<br> FName := LvlList + '/' + FName;<br>// FName := FName + '/' + Folder;<br><br> // Finally store a record with the data<br> UrlRec := New(PUrlRec);<br> UrlRec.Rep := FName;<br> UrlRec.UrlName := '';<br> UrlRec.UrlPath := '';<br> UrlRec.Level := Lvl;<br> L.Add(UrlRec);<br> end;<br>end;<br><br><br>function TMainForm.FindUrl(HtmlStr: String; Lvl: Integer): Boolean;<br>// Retrive URL name and path (if any) from HTML string<br>const<br> UrlSubStr: String = 'http://';<br>var<br> J, K: Integer;<br> S1, S2, S3: array[0..255] of Char;<br> Apo1, Apo2: PChar;<br> I: Integer;<br> SPath, SName: String;<br> FName: String;<br> UrlRec: PUrlRec;<br>begin<br> J := Pos(UrlSubStr, HtmlStr);<br> Result := (J <> 0);<br> if J <> 0 then // Substring found, retrieve URL path<br> begin<br> FillChar(S1, SizeOf(S1), 0);<br> FillChar(S2, SizeOf(S2), 0);<br> FillChar(S3, SizeOf(S3), 0);<br> SPath := HtmlStr;<br> Apo1 := StrScan(PChar(SPath), '"'); // Requires " !!!!!<br> for K := 1 to StrLen(Apo1) do<br> S1[K-1] := Apo1[K];<br> for K := 0 to Pos('"', S1) -2 do<br> S2[K] := S1[K];<br><br> SPath := S2;<br> // SPath should now hold the proper URL path<br><br> // Now retrieve URL name<br> Apo2 := StrScan(Apo1, '>');<br> for K := 1 to StrLen(Apo2) do<br> S1[K-1] := Apo2[K];<br> for K := 0 to Pos('<', S1) -2 do<br> S3[K] := S1[K];<br><br> // Convert illegal characters<br> SName := S3;<br> ReplaceIllChars(SName);<br><br> FName := LvlList[Lvl];<br> for I := Lvl-1 downto 1 do<br> FName := LvlList + '/' + FName;<br><br> // Finally store a record with the data<br> UrlRec := New(PUrlRec);<br> UrlRec.Rep := FName;<br> UrlRec.UrlName:= SName;<br> UrlRec.UrlPath := SPath;<br> UrlRec.Level := Lvl;<br> L.Add(UrlRec);<br> end;<br>end;<br><br><br>function TMainForm.FindSectionEnd(HtmlStr: String; var Lvl: Integer): Boolean;<br>// Return whether the current section ends<br>const<br> EndSubStr: String = '</DL>';<br>var<br> J, Idx: Integer;<br> UrlRec: PUrlRec;<br>begin<br> J := Pos(EndSubStr, HtmlStr);<br> Result := (J <> 0);<br> if J <> 0 then // Substring found, end of section<br> begin<br> Dec(Lvl); // 1 level down<br> end;<br>end;<br><br><br>procedure TMainForm.MakeFavorites;<br>// Create the favorites and their folders<br>var<br> I: Integer;<br> UrlRec: PUrlRec;<br>begin<br> for I := 0 to L.Count -1 do<br> begin<br> UrlRec := L;<br> if UrlRec.UrlName = '' then<br> MakeFolder(TargetFolder + '/' + UrlRec.Rep)<br> else<br> MakeUrlFile(TargetFolder + '/' + UrlRec.Rep + '/' {+IntToStr(UrlRec.Level)+' - '} + UrlRec.UrlName + '.url', UrlRec.UrlPath);<br> end;<br>end;<br><br><br>function TMainForm.MakeFolder(Folder: String): Boolean;<br>begin<br> CreateDirectory(PChar(Folder), nil);<br>end;<br><br><br>function TMainForm.MakeUrlFile(UrlName, UrlPath: String): Boolean;<br>var<br> UrlFile: TIniFile;<br>begin<br> { A URL-file has the structure of an INI-file with just one section.<br> Create the file as an INI-file and save the URL path. }<br> UrlFile := TIniFile.Create(UrlName);<br> UrlFile.WriteString('InternetShortcut', 'URL', UrlPath);<br> UrlFile.Free;<br>end;<br><br><br>procedure TMainForm.ReplaceIllChars(var S: String);<br>{ Replace illegal chars in filenames. This is necessary since<br> Internet Explorer stores the names as files and folders. }<br>const<br> ReplacedChar: Char = '-';<br>var<br> I: Integer;<br>begin<br> for I := 1 to Length(S) do<br> if S in ['/', '/', ':', '*', '?', '"', '<', '>', '|'] then<br> S := ReplacedChar;<br>end;<br><br><br>procedure TMainForm.Button1Click(Sender: TObject);<br>var<br> R: TSearchRec;<br> FavFolder: String; // Path of the favorites folder<br>begin<br> if not FileExists(Edit2.Text) then<br> begin<br> MessageDlg('The bookmark file you specified does not exist.',<br> mtError, [MbOk], 0);<br> Edit2.SetFocus;<br> end<br> else<br> begin<br> BookmarkList := TStringList.Create;<br> // Load the bookmarks into the stringlist<br> BookmarkList.LoadFromFile(Edit2.Text);<br> GetFavoritesFolder(FavFolder);<br> if FavFolder <> '' then<br> begin<br> // First make the appropriate favorites subfolder<br> CreateDirectory(PChar(FavFolder+'/'+Edit1.Text), nil);<br> // Make sure the folder actually exists<br> if FindFirst(FavFolder+'/'+Edit1.Text, faDirectory, R) = 0 then<br> begin<br> // Now convert all the bookmark links<br> TargetFolder := FavFolder+'/'+Edit1.Text;<br> Convert(FavFolder+'/'+Edit1.Text);<br> MessageDlg('The bookmarks have been copied to your favorites.',<br> mtInformation, [MbOk], 0);<br> end<br> else<br> begin<br> MessageDlg('The subfolder name you specified is invalid.',<br> mtError, [MbOk], 0);<br> Edit1.SetFocus;<br> end;<br> FindClose(R);<br> end;<br> BookmarkList.Free;<br> end;<br>end;<br><br><br>procedure TMainForm.Button2Click(Sender: TObject);<br>begin<br> Close;<br>end;<br><br><br>procedure TMainForm.Button3Click(Sender: TObject);<br>begin<br> AboutBox.ShowModal;<br>end;<br><br><br>procedure TMainForm.Button4Click(Sender: TObject);<br>begin<br> if OpenDialog1.Execute then<br> Edit2.Text := OpenDialog1.FileName;<br>end;<br><br>end.