How to link up all files under a folder?

How to link up all files under a folder?

3
NewbieNewbie
3

    Jan 05, 2007#1

    Hi All:

    I have a folder which has many html files, and their file names are ordered(start from 0001.htm to 0099.htm). Now i want to add navigation link to these files, that is, if I'm reading page 0002.htm, I can goto page 0003.htm by click Next button, and goto page 0001.htm by clicking prev button. However, currently there's no such button(link) in these files.

    More clrearly, I want to replace every </body> with this string:

    <a href=%previous file path> Prev </a> <a href=%next file path> Next </a> </body>

    If the file we're handling is 0003.htm, then %previous file path should be replaced by 0002.htm, the %next file path should be replaced by 0004.htm.

    I'm new to macro, can anybody help me? Thanks a lot!

    6,606548
    Grand MasterGrand Master
    6,606548

      Jan 05, 2007#2

      I have an idea how to code the macro which would do the job. But I must know which version of UltraEdit or UEStudio you currently use because the macro requires the command FindInFiles and the usage of this command depends on the version of UE/UES.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Jan 05, 2007#3

        The version of UltraEdit which I'm using is 11.20a. Thanks in advance!

        6,606548
        Grand MasterGrand Master
        6,606548

          Jan 06, 2007#4

          Okay, here is the macro which should to the job.

          The macro uses the command FindInFiles with an empty search string to get a list of the HTML files in a results edit window. You must adapt the red highlighted path to your local path where you have the HTML files.

          The problem with FindInFiles with results to an edit window is that the macro continues before all results are written to the new edit window, the edit window has the focus and the cursor is at bottom of the edit window.

          A workaround for this problem is to use a loop which searches in all open files for the last search result line which is immediately deleted, when found.

          But this loop could also result in an endless loop, if the macro was started without any file open in UltraEdit, because then the search in the loop is executed in no-man's-land, although you can already see the result edit window.

          To avoid all these problems the macro first creates a new file with an identifier string and sets the cursor at top of this new file. Now the macro does not depend anymore if at least 1 file is open or not.

          Next the FindInFiles with your path, the file specification needed for your HTML files and an empty search string is executed. The command will write its results (= file list) to a new edit window because optional parameter OutputWin is not used.

          The next loop was already explained above which is a workaround for the internal timing problem of UltraEdit. All versions of UltraEdit prior v12.00 creates an ASCII result edit window. So there is nothing to do anymore because you use v11.20a. All users reading this and using v12.00 of UE or higher should insert the command UnicodeToASCII above the SortAsc command.

          The sort command sorts now your files to get the correct file order 0001 - 0099.

          The PREV link in the first file should link to the last file. So the macro now goes to the last file in the list and copies its file name without path to user clipboard 9.

          Next the cursor is moved to the second file and its file name is appended to clipboard 9.

          Back at top of the file now the main loop starts.

          First the full file name of the current file is selected and the file is opened.

          In the HTML file the correct insert position is searched. For security the file is closed without modification if </body> is not found. I hope, you have never anything before </body> in the same line because the macro now inserts the 2 file names in clipboard 9 at start of the current line with </body>.

          The 2 file names are reselected and with a regular expression replace the required string for the 2 links is produced. The ^p at the end of the replace string inserts also a DOS line termination to move </body> into the next line. Use ^n instead of ^p if your HTML files are Unix files opened in Unix mode without temporary conversion to DOS for editing only. Then the modified file is saved and closed.

          After closing the HTML file, the results edit window has again the focus where the selection mode is still active. So the macro disables the selection mode and moves the cursor from end of the current file name to start.

          This now already modified file is the PREV file for the next file. So the macro selects now only the file name without path and copies it to clipboard 9.

          Then the cursor is moved down to the next file in the list. If there is no further file (= end of result edit window reached), all files have been adapted and the main loop must be exited.

          But before the macro can open the next file, the file name for NEXT must be appended to the clipboard 9. So the cursor is moved down by 1 line again.

          If now the end of the file is already reached, the current file where to insert the 2 links, is the last one in the list. The NEXT file for the last file should be the first file in the list. So for the last file append the file name of the first file to clipboard 9. For all other files append the file name at the current line to clipboard 9. In both cases the cursor is then moved back to the current file to be edited next.

          After all files from the list have been modified with PREV and NEXT links, the results edit window can be closed and also the new file created to avoid an endless loop after FindInFiles command.

          Last content of clipboard 9 is cleared and the Windows clipboard is selected as active clipboard before the macro exits.

          That's surely not the best macro solution for this job, but it should work.

          The macro property Continue if a Find with Replace not found must be checked for this macro.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          NewFile
          "DeleteThisFileWhichAvoidsEndlessLoop"
          Top
          FindInFiles "C:\Temp\" "????.htm" ""
          Loop
          Find MatchCase Up "Search complete, found"
          IfFound
          DeleteLine
          ExitLoop
          Else
          NextWindow
          EndIf
          EndLoop
          Top
          SortAsc IgnoreCase RemoveDup 1 -1 0 0 0 0 0 0
          Bottom
          Key UP ARROW
          Clipboard 9
          Find RegExp "????.htm"
          Copy
          Top
          Key DOWN ARROW
          Find RegExp "????.htm"
          CopyAppend
          Top
          Loop
          StartSelect
          Key END
          EndSelect
          Open "^s"
          Find "</body>"
          IfFound
          Key HOME
          Paste
          StartSelect
          Key HOME
          Find RegExp "^(????.htm^)^(????.htm^)"
          Replace All SelectText "<p><a href="^1">Prev</a> <a href="^2">Next</a></p>^p"
          EndSelect
          CloseFile Save
          Else
          CloseFile NoSave
          EndIf
          EndSelect
          Key HOME
          Find RegExp "????.htm"
          Copy
          Key HOME
          Key DOWN ARROW
          IfEof
          ExitLoop
          EndIf
          Key DOWN ARROW
          IfEof
          Top
          Find RegExp "????.htm"
          CopyAppend
          Bottom
          Key UP ARROW
          Else
          Find RegExp "????.htm"
          CopyAppend
          Key HOME
          Key UP ARROW
          EndIf
          EndLoop
          CloseFile NoSave
          Loop
          Find MatchCase "DeleteThisFileWhichAvoidsEndlessLoop"
          IfFound
          CloseFile NoSave
          ExitLoop
          Else
          NextWindow
          EndIf
          EndLoop
          ClearClipboard
          Clipboard 0

          Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Jan 07, 2007#5

            This is wonderful! Thanks Mofi, very usefull & tricky skills.