最近在做收藏夹的软件,由于微软提供的ImportExportFavorites函数必须确认,所以我自己做了个,希望大家指正其中的错误(15分)

  • 主题发起人 主题发起人 myqq
  • 开始时间 开始时间
M

myqq

Unregistered / Unconfirmed
GUEST, unregistred user!
use &nbsp; SHLObj;<br>procedure MyImportExportFavorites(t:boolean;dir:string);<br>var<br>bar:string;<br>OutPut: Text;<br>pidl: PItemIDList;<br>FavPath: array[0..MAX_PATH] of char ;<br>procedure GetIEFavourites(const favpath: string; var f: Text);<br>var<br>&nbsp; &nbsp;searchrec:TSearchrec;<br>&nbsp; &nbsp;path,dir,filename:String;<br>&nbsp; &nbsp;Buffer: array[0..2047] of Char;<br>&nbsp; &nbsp;found:Integer;<br>begin<br>if bar ='' then begin<br>filename:='&lt;!DOCTYPE NETSCAPE-Bookmark-file-1&gt;'+#13#10+<br>'&lt;!-- This is an automatically generated file.'+#13#10+<br>'It will be read and overwritten.'+#13#10+<br>'Do Not Edit! --&gt;'+#13#10+<br>'&lt;TITLE&gt;Bookmarks&lt;/TITLE&gt;'+#13#10+<br>'&lt;H1&gt;Bookmarks&lt;/H1&gt;'+#13#10+<br>'&lt;DL&gt;&lt;p&gt;';<br>writeln(f,filename);<br>end;<br>bar:=bar+' &nbsp; &nbsp;';<br>&nbsp; &nbsp;path:=FavPath+'/*.url';<br>&nbsp; &nbsp;dir:=ExtractFilepath(path) ;<br>&nbsp; &nbsp;found:=FindFirst(path,faAnyFile,searchrec) ;<br>&nbsp; &nbsp;while found=0 do begin<br>&nbsp; &nbsp; SetString(filename, Buffer,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetPrivateProfileString('InternetShortcut',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PChar('URL'), NIL, Buffer, SizeOf(Buffer),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PChar(dir+searchrec.Name))) ;<br>&nbsp; &nbsp; filename:=bar+'&lt;DT&gt;&lt;A HREF="'+filename+'" ADD_DATE="1073605689" LAST_VISIT="1073605695" LAST_MODIFIED="1076514206"&gt;'+changefileext(searchrec.Name,'')+'&lt;/A&gt;';<br>&nbsp; &nbsp; writeln(f,filename);<br>&nbsp; &nbsp; found:=FindNext(searchrec) ;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;found:=FindFirst(dir+'/*.*',faAnyFile,searchrec) ;<br>&nbsp; &nbsp;while found=0 do begin<br>&nbsp; &nbsp; if ((searchrec.Attr and faDirectory) &gt; 0)<br>&nbsp; &nbsp; &nbsp; and (searchrec.Name[1]&lt;&gt;'.') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; filename:=bar+'&lt;DT&gt;&lt;H3 FOLDED ADD_DATE="1073605693"&gt;'+searchrec.Name+'&lt;/H3&gt;&lt;DL&gt;&lt;p&gt;';<br>&nbsp; &nbsp; writeln(f,filename);<br>&nbsp; &nbsp; GetIEFavourites (dir+'/'+searchrec.name,f) ;<br>&nbsp; &nbsp; filename:='&lt;/DL&gt;&lt;p&gt;';<br>&nbsp; &nbsp; writeln(f,filename);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; found:=FindNext(searchrec) ;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;SysUtils.FindClose(searchrec) ;<br>end;<br>begin<br>if t=true then<br>begin<br>&nbsp; SHGetSpecialFolderLocation(0, CSIDL_FAVORITES, pidl) ;<br>&nbsp; SHGetPathFromIDList(pidl, favpath) ;<br>&nbsp; Assign(Output,dir);<br>&nbsp; Rewrite(output);<br>&nbsp; try<br>&nbsp; GetIEFavourites(StrPas(FavPath),OutPut) ;<br>&nbsp; finally<br>&nbsp; Close(Output);<br>&nbsp; end;<br>&nbsp; end;<br>end;<br>//函数只做好了导出部分,谁能帮忙做下导入部分
 
unit F2BMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Registry;<br><br>type<br>&nbsp; // Record for use as list element<br>&nbsp; PUrlRec = ^TUrlRec;<br>&nbsp; TUrlRec = record<br>&nbsp; &nbsp; Rep: &nbsp; &nbsp; String; &nbsp; &nbsp; &nbsp; &nbsp; // Folder name (folders and links)<br>&nbsp; &nbsp; UrlName: String; &nbsp; &nbsp; &nbsp; &nbsp; // Logical URL name (links only)<br>&nbsp; &nbsp; UrlPath: String; &nbsp; &nbsp; &nbsp; &nbsp; // www URL name (links only)<br>&nbsp; &nbsp; Level: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Folder level (position in folder tree)<br>&nbsp; end;<br><br>type<br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; CheckBox1: TCheckBox;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; FavFolder: String; &nbsp; &nbsp; &nbsp; // Path of the favorites folder<br>&nbsp; &nbsp; FavList: TList; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// List of the favorite links<br>&nbsp; &nbsp; BmkList: TStringList; &nbsp; &nbsp;// Favorites converted into bookmarks<br>&nbsp; &nbsp; procedure GetFavoritesFolder;<br>&nbsp; &nbsp; procedure SearchURL(Folder: String; Level: Integer);<br>&nbsp; &nbsp; procedure MakeBookmarkFile;<br>&nbsp; &nbsp; procedure TraverseFavList(Idx, PrevIdx: Integer);<br>&nbsp; protected<br>&nbsp; &nbsp; procedure MakeDocumentTop;<br>&nbsp; &nbsp; procedure MakeDocumentBottom;<br>&nbsp; &nbsp; procedure MakeHeaderTop(UrlRec: PUrlRec);<br>&nbsp; &nbsp; procedure MakeHeaderBottom(UrlRec: PUrlRec);<br>&nbsp; &nbsp; procedure MakeBookmark(UrlRec: PUrlRec);<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses<br>&nbsp; IniFiles, ShellApi, F2BAbout;<br><br>{---------------------- TMainForm ---------------------}<br><br>procedure TMainForm.GetFavoritesFolder;<br>// Find the directory of the Internet Explorer favorites<br>var<br>&nbsp; Registry: TRegistry;<br>begin<br>&nbsp; Registry := TRegistry.Create;<br>&nbsp; Registry.RootKey := HKEY_CURRENT_USER;<br>&nbsp; Registry.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);<br>&nbsp; FavFolder := Registry.ReadString('Favorites');<br>&nbsp; Registry.Free;<br>end;<br><br><br>procedure TMainForm.SearchURL(Folder: String; Level: Integer);<br>// Research and create the list of the favorites files found.<br>// This method uses recursion to get all the subfolders.<br>var<br>&nbsp; Found: Integer;<br>&nbsp; SearchRec: TSearchRec;<br>&nbsp; UrlFile: TIniFile;<br>&nbsp; UrlRec: PUrlRec;<br>begin<br>&nbsp; // First File<br>&nbsp; Found := FindFirst(Folder+'/*.*', faAnyFile, SearchRec);<br>&nbsp; // Research other file<br>&nbsp; while Found = 0 do<br>&nbsp; begin<br>&nbsp; &nbsp; // Don't get the special directories ('.' and '..')<br>&nbsp; &nbsp; if (SearchRec.Name &lt;&gt; '.') and (SearchRec.Name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; // Change directory<br>&nbsp; &nbsp; &nbsp; if (SearchRec.Attr and faDirectory &gt; 0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not (SearchRec.Attr and faSysFile &gt; 0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec := New(PUrlRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.Rep := Copy(Folder+'/'+SearchRec.Name, Length(FavFolder)+2, Length(Folder+'/'+SearchRec.Name));<br>/// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UrlRec.Rep := SearchRec.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.UrlName := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.UrlPath := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.Level := Level;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FavList.Add(UrlRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SearchURL(Folder+'/'+SearchRec.Name, Level+1); &nbsp; // Recursion<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the file extension<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if UpperCase(ExtractFileExt(SearchRec.Name)) = '.URL' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec := New(PUrlRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.Rep := Copy(Folder, Length(FavFolder)+2, Length(Folder));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.UrlName:= Copy(SearchRec.Name, 0, Length(SearchRec.Name)-3);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { A URL-file has the structure of an INI-file with just one section.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Open the file as an INI-file to read the URL path. }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlFile := TIniFile.Create(Folder+'/'+SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.UrlPath := UrlFile.ReadString('InternetShortcut', 'URL', 'no path');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.Level := Level;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlFile.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FavList.Add(UrlRec); &nbsp; &nbsp; &nbsp; // Add to list of favorites<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; // other file<br>&nbsp; &nbsp; &nbsp; Found := FindNext(SearchRec);<br>&nbsp; end;<br>{<br>&nbsp; // Add a dummy record<br>&nbsp; &nbsp; &nbsp; &nbsp;UrlRec := New(PUrlRec);<br>&nbsp; &nbsp; &nbsp; &nbsp;UrlRec.Rep := '';<br>&nbsp; &nbsp; &nbsp; &nbsp;UrlRec.UrlName := '';<br>&nbsp; &nbsp; &nbsp; &nbsp;UrlRec.UrlPath := '';<br>&nbsp; &nbsp; &nbsp; &nbsp;UrlRec.Level := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;FavList.Add(UrlRec);<br>}<br>end;<br><br><br>procedure TMainForm.MakeBookmarkFile;<br>// Create the bookmark file with the internet favorites files found<br>begin<br>&nbsp; BmkList := TStringList.Create;<br><br>&nbsp; MakeDocumentTop;<br>&nbsp; TraverseFavList(0, -1); &nbsp; &nbsp;// Called recursively<br>/// &nbsp;MakeHeaderBottom(FavList[FavList.Count-1]);<br>&nbsp; MakeDocumentBottom;<br><br>&nbsp; try<br>&nbsp; &nbsp; BmkList.SaveToFile(Edit1.Text);<br>&nbsp; except on EFCreateError do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageDlg('The target file name you specified is invalid.', mtError, [MbOk], 0);<br>&nbsp; &nbsp; &nbsp; Edit1.SetFocus;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp; BmkList.Free;<br>&nbsp; MessageDlg('Your favorites have been copied to the bookmark file you specified.',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mtInformation, [MbOk], 0);<br>end;<br><br><br>procedure TMainForm.TraverseFavList(Idx, PrevIdx: Integer);<br>// Recursive method to obtain all URLs in their respective folders<br>var<br>&nbsp; UrlRec, PrevUrlRec: PUrlRec;<br>&nbsp; X: Integer;<br>begin<br>&nbsp; if Idx &lt; FavList.Count then<br>&nbsp; begin<br><br>&nbsp; &nbsp; UrlRec := FavList[Idx];<br>&nbsp; &nbsp; if PrevIdx = -1 then<br>&nbsp; &nbsp; &nbsp; PrevUrlRec := nil<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; PrevUrlRec := FavList[PrevIdx];<br><br>&nbsp; &nbsp; if UrlRec.UrlName = '' then &nbsp; // Folder found (not a URL)<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if PrevUrlRec &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; X := PrevUrlRec.Level;<br>&nbsp; &nbsp; &nbsp; &nbsp; while UrlRec.Level &lt;= X do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MakeHeaderBottom(PrevUrlRec); &nbsp; &nbsp; &nbsp;// End section<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(PrevUrlRec.Level); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // dirty!<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(X);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; MakeHeaderTop(UrlRec); &nbsp; &nbsp; &nbsp;// Make a new header<br><br>&nbsp; &nbsp; &nbsp; PrevIdx := Idx;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; MakeBookmark(UrlRec); &nbsp; &nbsp; &nbsp; // Insert bookmark<br><br>&nbsp; &nbsp; TraverseFavList(Idx+1, PrevIdx);<br><br>&nbsp; end;<br>end;<br><br><br>procedure TMainForm.MakeDocumentTop;<br>begin<br>&nbsp; BmkList.Add('&lt;!-- Made with ' + Application.Title + ' --&gt;');<br>&nbsp; BmkList.Add('');<br>&nbsp; BmkList.Add('&lt;TITLE&gt;' + 'Converted Bookmarks' + '&lt;/TITLE&gt;');<br>&nbsp; BmkList.Add('&lt;H1&gt;' + 'Converted Bookmarks' + '&lt;/H1&gt;');<br>&nbsp; BmkList.Add('');<br>&nbsp; BmkList.Add('&lt;DL&gt;&lt;P&gt;');<br>end;<br><br><br>procedure TMainForm.MakeDocumentBottom;<br>begin<br>/// &nbsp;BmkList.Add('');<br>// &nbsp;BmkList.Add(' &nbsp; &nbsp;&lt;/DL&gt;&lt;P&gt;');<br>&nbsp; BmkList.Add('');<br>&nbsp; BmkList.Add('&lt;/DL&gt;&lt;P&gt;');<br>end;<br><br><br>procedure TMainForm.MakeHeaderTop(UrlRec: PUrlRec);<br>// Add header name to the start of the section<br>var<br>&nbsp; I, Idx: Integer;<br>&nbsp; S: String;<br>&nbsp; A: array[0..255] of Char;<br>begin<br>&nbsp; // Change names of subfolders<br>&nbsp; FillChar(A, SizeOf(A), 0);<br>&nbsp; Idx := 1;<br>&nbsp; for I := 1 to Length(UrlRec.Rep) do<br>&nbsp; &nbsp; if UrlRec.Rep = '/' then<br>&nbsp; &nbsp; &nbsp; Idx := I+1;<br>&nbsp; for I := Idx to Length(UrlRec.Rep) do<br>&nbsp; &nbsp; A[I-Idx] := UrlRec.Rep;<br><br>&nbsp; // Add the name to the header<br>&nbsp; BmkList.Add('');<br>/// &nbsp;BmkList.Add('Level: ' + IntToStr(UrlRec.Level));<br>&nbsp; for I := 1 to UrlRec.Level do<br>&nbsp; &nbsp; S := S + ' &nbsp; &nbsp;';<br>&nbsp; BmkList.Add(S + '&lt;DT&gt;&lt;H3&gt;' + A + '&lt;/H3&gt;');<br>&nbsp; BmkList.Add(S + '&lt;DL&gt;&lt;P&gt;');<br>end;<br><br><br>procedure TMainForm.MakeHeaderBottom(UrlRec: PUrlRec);<br>// End section containing bookmarks<br>var<br>&nbsp; I: Integer;<br>&nbsp; S: String;<br>begin<br>&nbsp; for I := 1 to UrlRec.Level do<br>&nbsp; &nbsp; S := S + ' &nbsp; &nbsp;';<br>&nbsp; BmkList.Add(S + '&lt;/DL&gt;&lt;P&gt;');<br>&nbsp; BmkList.Add('');<br>end;<br><br><br>procedure TMainForm.MakeBookmark(UrlRec: PUrlRec);<br>var<br>&nbsp; I: Integer;<br>&nbsp; S: String;<br>begin<br>&nbsp; { For some reason the names of all the favorite links retrieved<br>&nbsp; &nbsp; have an extra dot in the end. Remove this dot. }<br>&nbsp; Delete(UrlRec.UrlName, Length(UrlRec.UrlName), 1);<br>&nbsp; // Now add the path and name<br>&nbsp; for I := 1 to UrlRec.Level do<br>&nbsp; &nbsp; S := S + ' &nbsp; &nbsp;';<br>/// &nbsp;BmkList.Add('Level: ' + IntToStr(UrlRec.Level));<br>&nbsp; BmkList.Add(S + '&lt;DT&gt;&lt;A HREF="' + UrlRec.UrlPath + '"&gt;' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UrlRec.UrlName + '&lt;/A&gt;' + '&lt;BR&gt;');<br>end;<br><br><br>function SortFunction(Item1, Item2: Pointer): Integer;<br>// Function to sort the list of favorites alphabetically<br>var<br>&nbsp; Rec1, Rec2: PUrlRec;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; Rec1 := Item1;<br>&nbsp; Rec2 := Item2;<br>&nbsp; if Rec1.Rep &lt; Rec2.Rep then Result := -1;<br>&nbsp; if Rec1.Rep &gt; Rec2.Rep then Result := 1;<br>&nbsp; if Rec1.Rep = Rec2.Rep then<br>&nbsp; begin<br>&nbsp; &nbsp; if Rec1.UrlName &lt; Rec2.UrlName then Result := -1;<br>&nbsp; &nbsp; if Rec1.UrlName &gt; Rec2.UrlName then Result := 1;<br>&nbsp; &nbsp; if Rec1.UrlName = Rec2.UrlName then Result := 0;<br>&nbsp; end;<br>end;<br><br><br>procedure TMainForm.Button1Click(Sender: TObject);<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; FavList := TList.Create;<br>&nbsp; GetFavoritesFolder;<br>&nbsp; if FavFolder &lt;&gt; '' then<br>&nbsp; begin<br>&nbsp; &nbsp; SearchURL(FavFolder, 1); &nbsp; &nbsp; &nbsp;// Get all links<br>&nbsp; &nbsp; FavList.Sort(SortFunction); &nbsp; // Sort the resulting list<br>&nbsp; &nbsp; MakeBookmarkFile; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make bookmark file from links<br>&nbsp; end;<br>&nbsp; // Clean up<br>&nbsp; for I := 0 to FavList.Count -1 do<br>&nbsp; &nbsp; Dispose(PUrlRec(FavList));<br>&nbsp; FavList.Free;<br>&nbsp; // Open the new bookmark file if user wants it<br>&nbsp; if CheckBox1.Checked then<br>&nbsp; &nbsp; if ShellExecute(Application.MainForm.Handle, nil, PChar(Edit1.Text),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil, nil, SW_SHOWNORMAL) &lt;= 32 then<br>&nbsp; &nbsp; &nbsp; MessageDlg('Error: Could not start your Internet browser.', mtError, [mbOk], 0);<br>// Alternatively you could use ExecuteFile<br>end;<br><br><br>procedure TMainForm.Button2Click(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br><br>procedure TMainForm.Button3Click(Sender: TObject);<br>begin<br>&nbsp; AboutBox.ShowModal;<br>end;<br><br>end.
 
unit B2FMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Registry;<br><br>type<br>&nbsp; // Record for use as list element<br>&nbsp; PUrlRec = ^TUrlRec;<br>&nbsp; TUrlRec = record<br>&nbsp; &nbsp; Rep: &nbsp; &nbsp; String; &nbsp; &nbsp; &nbsp; &nbsp; // Folder name (folders and links)<br>&nbsp; &nbsp; UrlName: String; &nbsp; &nbsp; &nbsp; &nbsp; // Logical URL name (links only)<br>&nbsp; &nbsp; UrlPath: String; &nbsp; &nbsp; &nbsp; &nbsp; // www URL name (links only)<br>&nbsp; &nbsp; Level: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Folder level (position in folder tree)<br>&nbsp; end;<br><br>type<br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; OpenDialog1: TOpenDialog;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; BookmarkList: TStringList;<br>&nbsp; &nbsp; L: TList;<br>&nbsp; &nbsp; LastFolder: String;<br>&nbsp; &nbsp; TargetFolder: String;<br>&nbsp; &nbsp; LvlList: TStringList;<br>&nbsp; &nbsp; procedure GetFavoritesFolder(var Folder: String);<br>&nbsp; &nbsp; procedure Convert(Folder: String);<br>&nbsp; &nbsp; procedure ScanBmkLine(I: Integer; Lvl: Integer);<br>&nbsp; &nbsp; function FindFolder(HtmlStr: String; var Lvl: Integer): Boolean;<br>&nbsp; &nbsp; function FindUrl(HtmlStr: String; Lvl: Integer): Boolean;<br>&nbsp; &nbsp; function FindSectionEnd(HtmlStr: String; var Lvl: Integer): Boolean;<br>&nbsp; &nbsp; procedure MakeFavorites;<br>&nbsp; &nbsp; function MakeFolder(Folder: String): Boolean;<br>&nbsp; &nbsp; function MakeUrlFile(UrlName, UrlPath: String): Boolean;<br>&nbsp; &nbsp; procedure ReplaceIllChars(var S: String);<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses<br>&nbsp; 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>&nbsp; Registry: TRegistry;<br>begin<br>&nbsp; Registry := TRegistry.Create;<br>&nbsp; Registry.RootKey := HKEY_CURRENT_USER;<br>&nbsp; Registry.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);<br>&nbsp; Folder := Registry.ReadString('Favorites');<br>&nbsp; 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>&nbsp; Rec1, Rec2: PUrlRec;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; Rec1 := Item1;<br>&nbsp; Rec2 := Item2;<br>&nbsp; if Rec1.Rep &lt; Rec2.Rep then Result := -1;<br>&nbsp; if Rec1.Rep &gt; Rec2.Rep then Result := 1;<br>&nbsp; if Rec1.Rep = Rec2.Rep then<br>&nbsp; begin<br>&nbsp; &nbsp; if Rec1.UrlName &lt; Rec2.UrlName then Result := -1;<br>&nbsp; &nbsp; if Rec1.UrlName &gt; Rec2.UrlName then Result := 1;<br>&nbsp; &nbsp; if Rec1.UrlName = Rec2.UrlName then Result := 0;<br>&nbsp; end;<br>end;<br><br><br>procedure TMainForm.Convert(Folder: String);<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; L := TList.Create;<br>&nbsp; LvlList := TStringList.Create;<br>&nbsp; LvlList.Add('');<br>&nbsp; try<br>&nbsp; &nbsp; ScanBmkLine(0, 0); &nbsp; &nbsp; &nbsp; // Find all bookmarks recursively<br>&nbsp; finally<br>&nbsp; &nbsp; L.Sort(SortFunction); &nbsp; &nbsp;// Sort the resulting list<br>&nbsp; &nbsp; MakeFavorites; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make favorites from the list<br>&nbsp; &nbsp; // Clean up<br>&nbsp; &nbsp; for I := 0 to L.Count -1 do<br>&nbsp; &nbsp; &nbsp; Dispose(PUrlRec(L));<br>&nbsp; &nbsp; L.Free;<br>&nbsp; &nbsp; LvlList.Free;<br>&nbsp; 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>&nbsp; if I &lt; BookmarkList.Count -1 then<br>&nbsp; begin<br>&nbsp; &nbsp; if not FindSectionEnd(BookmarkList, Lvl) then<br>&nbsp; &nbsp; &nbsp; if not FindUrl(BookmarkList, Lvl) then<br>&nbsp; &nbsp; &nbsp; &nbsp; FindFolder(BookmarkList, Lvl);<br>&nbsp; &nbsp; ScanBmkLine(I+1, Lvl);<br>&nbsp; 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>&nbsp; FolderSubStr: String = '&lt;H3';<br>var<br>&nbsp; J, Idx: Integer;<br>&nbsp; S: array[0..255] of Char;<br>&nbsp; UrlRec: PUrlRec;<br>&nbsp; I: Integer;<br>&nbsp; Folder: String;<br>&nbsp; FName: String;<br>begin<br>&nbsp; J := Pos(FolderSubStr, HtmlStr);<br>&nbsp; Result := (J &lt;&gt; 0);<br>&nbsp; if J &lt;&gt; 0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Substring found, retrieve folder name<br>&nbsp; begin<br>&nbsp; &nbsp; Inc(Lvl); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 1 level up<br><br>&nbsp; &nbsp; FillChar(S, SizeOf(S), 0);<br>&nbsp; &nbsp; Idx := 1;<br>&nbsp; &nbsp; for J := 1 to Length(HtmlStr)-1 do<br>&nbsp; &nbsp; &nbsp; if HtmlStr[J] = '&gt;' then<br>&nbsp; &nbsp; &nbsp; &nbsp; Idx := J+1;<br>&nbsp; &nbsp; for J := Idx to Length(HtmlStr)-5 do<br>&nbsp; &nbsp; &nbsp; S[J-Idx] := HtmlStr[J];<br><br>&nbsp; &nbsp; Folder := S;<br>&nbsp; &nbsp; ReplaceIllChars(Folder);<br><br>&nbsp; &nbsp; if LvlList.Count &gt; Lvl then<br>&nbsp; &nbsp; &nbsp; LvlList[Lvl] := Folder &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Replace folder hierarchy<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; LvlList.Add(Folder);<br><br>&nbsp; &nbsp; FName := LvlList[Lvl];<br>&nbsp; &nbsp; for I := Lvl-1 downto 1 do<br>&nbsp; &nbsp; &nbsp; FName := LvlList + '/' + FName;<br>// &nbsp; &nbsp;FName := FName + '/' + Folder;<br><br>&nbsp; &nbsp; // Finally store a record with the data<br>&nbsp; &nbsp; UrlRec := New(PUrlRec);<br>&nbsp; &nbsp; UrlRec.Rep := FName;<br>&nbsp; &nbsp; UrlRec.UrlName := '';<br>&nbsp; &nbsp; UrlRec.UrlPath := '';<br>&nbsp; &nbsp; UrlRec.Level := Lvl;<br>&nbsp; &nbsp; L.Add(UrlRec);<br>&nbsp; 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>&nbsp; UrlSubStr: String = 'http://';<br>var<br>&nbsp; J, K: Integer;<br>&nbsp; S1, S2, S3: array[0..255] of Char;<br>&nbsp; Apo1, Apo2: PChar;<br>&nbsp; I: Integer;<br>&nbsp; SPath, SName: String;<br>&nbsp; FName: String;<br>&nbsp; UrlRec: PUrlRec;<br>begin<br>&nbsp; J := Pos(UrlSubStr, HtmlStr);<br>&nbsp; Result := (J &lt;&gt; 0);<br>&nbsp; if J &lt;&gt; 0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Substring found, retrieve URL path<br>&nbsp; begin<br>&nbsp; &nbsp; FillChar(S1, SizeOf(S1), 0);<br>&nbsp; &nbsp; FillChar(S2, SizeOf(S2), 0);<br>&nbsp; &nbsp; FillChar(S3, SizeOf(S3), 0);<br>&nbsp; &nbsp; SPath := HtmlStr;<br>&nbsp; &nbsp; Apo1 := StrScan(PChar(SPath), '"'); &nbsp; &nbsp; // Requires " !!!!!<br>&nbsp; &nbsp; for K := 1 to StrLen(Apo1) do<br>&nbsp; &nbsp; &nbsp; S1[K-1] := Apo1[K];<br>&nbsp; &nbsp; for K := 0 to Pos('"', S1) -2 do<br>&nbsp; &nbsp; &nbsp; S2[K] := S1[K];<br><br>&nbsp; &nbsp; SPath := S2;<br>&nbsp; &nbsp; // SPath should now hold the proper URL path<br><br>&nbsp; &nbsp; // Now retrieve URL name<br>&nbsp; &nbsp; Apo2 := StrScan(Apo1, '&gt;');<br>&nbsp; &nbsp; for K := 1 to StrLen(Apo2) do<br>&nbsp; &nbsp; &nbsp; S1[K-1] := Apo2[K];<br>&nbsp; &nbsp; for K := 0 to Pos('&lt;', S1) -2 do<br>&nbsp; &nbsp; &nbsp; S3[K] := S1[K];<br><br>&nbsp; &nbsp; // Convert illegal characters<br>&nbsp; &nbsp; SName := S3;<br>&nbsp; &nbsp; ReplaceIllChars(SName);<br><br>&nbsp; &nbsp; FName := LvlList[Lvl];<br>&nbsp; &nbsp; for I := Lvl-1 downto 1 do<br>&nbsp; &nbsp; &nbsp; FName := LvlList + '/' + FName;<br><br>&nbsp; &nbsp; // Finally store a record with the data<br>&nbsp; &nbsp; UrlRec := New(PUrlRec);<br>&nbsp; &nbsp; UrlRec.Rep := FName;<br>&nbsp; &nbsp; UrlRec.UrlName:= SName;<br>&nbsp; &nbsp; UrlRec.UrlPath := SPath;<br>&nbsp; &nbsp; UrlRec.Level := Lvl;<br>&nbsp; &nbsp; L.Add(UrlRec);<br>&nbsp; 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>&nbsp; EndSubStr: String = '&lt;/DL&gt;';<br>var<br>&nbsp; J, Idx: Integer;<br>&nbsp; UrlRec: PUrlRec;<br>begin<br>&nbsp; J := Pos(EndSubStr, HtmlStr);<br>&nbsp; Result := (J &lt;&gt; 0);<br>&nbsp; if J &lt;&gt; 0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Substring found, end of section<br>&nbsp; begin<br>&nbsp; &nbsp; Dec(Lvl); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 1 level down<br>&nbsp; end;<br>end;<br><br><br>procedure TMainForm.MakeFavorites;<br>// Create the favorites and their folders<br>var<br>&nbsp; I: Integer;<br>&nbsp; UrlRec: PUrlRec;<br>begin<br>&nbsp; for I := 0 to L.Count -1 do<br>&nbsp; begin<br>&nbsp; &nbsp; UrlRec := L;<br>&nbsp; &nbsp; if UrlRec.UrlName = '' then<br>&nbsp; &nbsp; &nbsp; MakeFolder(TargetFolder + '/' + UrlRec.Rep)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; MakeUrlFile(TargetFolder + '/' + UrlRec.Rep + '/' {+IntToStr(UrlRec.Level)+' - '} + UrlRec.UrlName + '.url', UrlRec.UrlPath);<br>&nbsp; end;<br>end;<br><br><br>function TMainForm.MakeFolder(Folder: String): Boolean;<br>begin<br>&nbsp; CreateDirectory(PChar(Folder), nil);<br>end;<br><br><br>function TMainForm.MakeUrlFile(UrlName, UrlPath: String): Boolean;<br>var<br>&nbsp; UrlFile: TIniFile;<br>begin<br>&nbsp; { A URL-file has the structure of an INI-file with just one section.<br>&nbsp; &nbsp; Create the file as an INI-file and save the URL path. }<br>&nbsp; UrlFile := TIniFile.Create(UrlName);<br>&nbsp; UrlFile.WriteString('InternetShortcut', 'URL', UrlPath);<br>&nbsp; UrlFile.Free;<br>end;<br><br><br>procedure TMainForm.ReplaceIllChars(var S: String);<br>{ Replace illegal chars in filenames. This is necessary since<br>&nbsp; Internet Explorer stores the names as files and folders. }<br>const<br>&nbsp; ReplacedChar: Char = '-';<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; for I := 1 to Length(S) do<br>&nbsp; &nbsp; if S in ['/', '/', ':', '*', '?', '"', '&lt;', '&gt;', '|'] then<br>&nbsp; &nbsp; &nbsp; S := ReplacedChar;<br>end;<br><br><br>procedure TMainForm.Button1Click(Sender: TObject);<br>var<br>&nbsp; R: TSearchRec;<br>&nbsp; FavFolder: String; &nbsp; &nbsp; &nbsp; &nbsp; // Path of the favorites folder<br>begin<br>&nbsp; if not FileExists(Edit2.Text) then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageDlg('The bookmark file you specified does not exist.',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mtError, [MbOk], 0);<br>&nbsp; &nbsp; Edit2.SetFocus;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; BookmarkList := TStringList.Create;<br>&nbsp; &nbsp; // Load the bookmarks into the stringlist<br>&nbsp; &nbsp; BookmarkList.LoadFromFile(Edit2.Text);<br>&nbsp; &nbsp; GetFavoritesFolder(FavFolder);<br>&nbsp; &nbsp; if FavFolder &lt;&gt; '' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; // First make the appropriate favorites subfolder<br>&nbsp; &nbsp; &nbsp; CreateDirectory(PChar(FavFolder+'/'+Edit1.Text), nil);<br>&nbsp; &nbsp; &nbsp; // Make sure the folder actually exists<br>&nbsp; &nbsp; &nbsp; if FindFirst(FavFolder+'/'+Edit1.Text, faDirectory, R) = 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; // Now convert all the bookmark links<br>&nbsp; &nbsp; &nbsp; &nbsp; TargetFolder := FavFolder+'/'+Edit1.Text;<br>&nbsp; &nbsp; &nbsp; &nbsp; Convert(FavFolder+'/'+Edit1.Text);<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageDlg('The bookmarks have been copied to your favorites.',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mtInformation, [MbOk], 0);<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageDlg('The subfolder name you specified is invalid.',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mtError, [MbOk], 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.SetFocus;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; FindClose(R);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; BookmarkList.Free;<br>&nbsp; end;<br>end;<br><br><br>procedure TMainForm.Button2Click(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br><br>procedure TMainForm.Button3Click(Sender: TObject);<br>begin<br>&nbsp; AboutBox.ShowModal;<br>end;<br><br><br>procedure TMainForm.Button4Click(Sender: TObject);<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; &nbsp; Edit2.Text := OpenDialog1.FileName;<br>end;<br><br>end.
 
unit B2FAbout;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; TAboutBox = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; &nbsp; Version: TLabel;<br>&nbsp; &nbsp; Author: TLabel;<br>&nbsp; &nbsp; Freeware: TLabel;<br>&nbsp; &nbsp; Product: TLabel;<br>&nbsp; &nbsp; HtmlLink: TLabel;<br>&nbsp; &nbsp; procedure HtmlLinkClick(Sender: TObject);<br>&nbsp; end;<br><br>var<br>&nbsp; AboutBox: TAboutBox;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses<br>&nbsp; ShellAPI;<br><br>procedure TAboutBox.HtmlLinkClick(Sender: TObject);<br>begin<br>&nbsp; ShellExecute(Application.MainForm.Handle, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PChar('http://'+HtmlLink.Caption), nil, '.', SW_RESTORE);<br>{ Alternatively you could use:<br>&nbsp; ExecuteFile('http://'+HtmlLink.Caption, '.', 'c:/', SW_RESTORE);<br>&nbsp; Remember to get the FMXUtils unit from the Delphi Demo folder. }<br>end;<br><br>end.
 
后退
顶部