Loop in directory file names to create HTML references to all files in a directory (tree)

Loop in directory file names to create HTML references to all files in a directory (tree)

6
NewbieNewbie
6

    Nov 02, 2010#1

    I frequently have to manually build a list of links to files for our IntrAnet.

    Is there an easier way to:
    - Get a file list within a directory
    - Loop through that list and for each file name write out:
    <a href="/RootFolder/SubFolder/Project/<INSERT FILE NAME HERE">

    Example directory list:

    File1.pdf
    File2.pdf
    File3.pdf

    Results of script:

    <a href="/RootFolder/SubFolder/Project/File1.pdf">
    <a href="/RootFolder/SubFolder/Project/File2.pdf">
    <a href="/RootFolder/SubFolder/Project/File3.pdf">

    6,603548
    Grand MasterGrand Master
    6,603548

      Nov 02, 2010#2

      Sure, this is very simple. You need my script function GetListOfFiles and put the code of this function (without the explanation above and the demonstration code below, but read the explanation) together with following script code into a script file:

      Code: Select all

      // Note: Normally the directory for command Find in Files must be with backslash at end.
      //       But function GetListOfFiles appends it automatically and the regular expression
      //       below requires the root directory name without backslash to convert also the
      //       file names found in the root directory correct.
      var sRootDir = "\\\\server\\share"
      if (GetListOfFiles(0, sRootDir, "*", true)) {
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace("%"+sRootDir+"^(?++\\^)^(*^)$", "<a href=\"^1^2\">^2</a>");
         UltraEdit.activeDocument.findReplace.regExp=false;
         UltraEdit.activeDocument.findReplace.replace("\\", "/");
         UltraEdit.activeDocument.findReplace.replace("href=\"/", "href=\"");
      }
      As you can see UltraEdit is used to search for the files names in all directories starting in the specified root directory. I used "*" as file specification, but you can also use "*.htm;*.pdf" to find only HTML and PDF files.

      Next a simple Tagged regular expression using the UltraEdit regexp engine is used to insert the tag arround the file names.

      Two non regular expression replaces convert the backslashes to forward slashes and remove the forward slash at start of every URL which would not be wrong, but also not needed. And that's it.
      Best regards from an UC/UE/UES for Windows user from Austria