locked
VBS: Convert html file recursively (based on content) into Shortcuts and Folders RRS feed

  • Question

  • Hello,

    First: How to Exactly do the Following via GUI in Windows 7:
    1) Have the bookmarks.html File (see below), Open Internet Explorer 11 - go to "File"->Export/Import->Import from File-> Favorites->Import.

    This generates the File/Folder Structure that I want - via GUI

    Now, the problem: How to do this via Script/Without GUI?


    I want to convert the contents of a bookmarks-file (Documentation:  https://msdn.microsoft.com/en-us/library/aa753582(v=vs.85).aspx) into .url files. 
    I already finished the script to convert the bookmarks.html file into .url Files, but I do not know how to create folders, recursively, , based on the content of the html file.

    The example  HTML file:

    <!DOCTYPE NETSCAPE-Bookmark-file-1>
    <!-- This is an automatically generated file.
         It will be read and overwritten.
         DO NOT EDIT! -->
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
    <TITLE>Bookmarks</TITLE>
    <H1>Lesezeichen-Menü</H1>
    
    <DL><p>
        <DT><H3 ADD_DATE="1434634285" LAST_MODIFIED="1434634373">Folder 1</H3>
        <DL><p>
            <DT><A HREF="https://www.facebook.com/" ADD_DATE="1434634363" LAST_MODIFIED="1434634363">(1) Facebook</A>
            <DT><H3 ADD_DATE="1434634295" LAST_MODIFIED="1434634371">Sub-Folder 1</H3>
            <DL><p>
                <DT><A HREF="https://www.facebook.com/" ADD_DATE="1434634363" LAST_MODIFIED="1434634363">(1) Facebook</A>
                <DT><H3 ADD_DATE="1434634335" LAST_MODIFIED="1434634363">Sub-Sub Folder n</H3>
                <DL><p>
                    <DT><A HREF="https://www.facebook.com/" ADD_DATE="1434634363" LAST_MODIFIED="1434634363">(1) Facebook</A>
                </DL><p>
            </DL><p>
            <DT><H3 ADD_DATE="1434634310" LAST_MODIFIED="1434634376">Sub-Folder 2</H3>
            <DL><p>
                <DT><A HREF="https://www.facebook.com/" ADD_DATE="1434634363" LAST_MODIFIED="1434634363">(1) Facebook</A>
            </DL><p>
        </DL><p>
        <HR>    <DT><H3 ADD_DATE="1378212910" LAST_MODIFIED="1434634405" UNFILED_BOOKMARKS_FOLDER="true">Unsortierte Lesezeichen</H3>
        <DL><p>
            <DT><A HREF="https://www.facebook.com/" ADD_DATE="1434634405" LAST_MODIFIED="1434634405">Facebook</A>
        </DL><p>
    </DL>
    

    The folder should be created in the same directory, as the script now generated the .url files, but it should preserve the folder Structure, as when you import it into Internet Explorer via GUI (above).

    Now, the Script I have, at the Moment:

    'Set Locale
    SetLocale(1031)
    'Expand Environment
    Dim WshS
    Set WshS = WScript.CreateObject("WScript.Shell")
    Set fso1 = CreateObject("Scripting.FileSystemObject")
    usrProfile = WshS.ExpandEnvironmentStrings("%UserProfile%")
    'Read File
    Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = true
    outpath = usrProfile & "\Favorites\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    bookmarkfile = "bookmarks.html"
    'Declare var
    Set bookmarks = objFSO.OpenTextFile(bookmarkfile, ForReading)
    Set regEx = New RegExp 
    regEx.Global = True
    Set regEx2 = New RegExp
    regEx2.Global = True
    'Pattern -- Generate ONLY valid Title/Filenames
    regEx2.Pattern = "[\\/:?*^""<>|]" 
    'Pattern: Set Link and adddate inside File
    regEx.Pattern = "<DT><A HREF=""(.*)"" ADD_DATE.*>(.*)</A>"
    'Do until File finished
    do until bookmarks.AtEndOfStream
      line = bookmarks.readline()
      if regEx.test(line) then
        shortcut = regEx.Replace(line,"$1")
        filename = trim(regEx.Replace(line,"$2"))
    	'cut
        filename = Regex2.Replace(filename, " ") 
    	'Insert Space @ Undefined / Illegal Character
        filename = outpath & left(filename, 80) & ".url"
        wscript.echo filename
        on error resume next 
        Set objFile = objFSO.OpenTextFile(filename, ForWriting, CreateIfNeeded)
        if err.number <> 0 then
          wscript.echo err.description
        end if
        objFile.write "[InternetShortcut]" & vbcrlf & "URL=" & shortcut
        objFile.close
    	Application.DisplayAlerts = False
      end if
    loop 'loop for do

    I hope you can help me with this, as the Execution via GUI / AutoIT is no choice on my Environment.

    Thanks in Advance!


    Thursday, June 18, 2015 1:52 PM

Answers

  • Are you asking others to redesign your script for you according to your specification? That's not the purpose of this forum.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by MKR88 Friday, June 19, 2015 9:41 AM
    Thursday, June 18, 2015 2:22 PM

All replies

  • Are you asking others to redesign your script for you according to your specification? That's not the purpose of this forum.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by MKR88 Friday, June 19, 2015 9:41 AM
    Thursday, June 18, 2015 2:22 PM
  • This will get you all of the links in the file:

    $links=Get-Content  bookmarks.htm | ?{$_ -match 'href=' } |%{[void]($_ -match 'href=(?<href>.*)"');$matches['href']}


    \_(ツ)_/

    Thursday, June 18, 2015 2:53 PM
  • Hello Bill,

    no, I want to show what i've got, so maybe someone can help me.
    Thursday, June 18, 2015 6:55 PM
  • Define "help". What's your specific question? (Also, see jrv's suggestion.)

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 18, 2015 6:59 PM
  • You should also note that all of you bookmarks are already set up as shortcuts that you can copy anywhere you need:

    By default all Internet Shrtcuts are stored here: C:\Users\<userName>\Favorites

    They only become URLs when you export them.  The shortcuts in that folder can be copied anywhere.


    \_(ツ)_/

    Thursday, June 18, 2015 7:15 PM