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

Help with writing and running scripts

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

Postby umayxa3 » Tue Nov 02, 2010 9:32 am

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">
umayxa3
Newbie
 
Posts: 6
Joined: Sat Aug 15, 2009 8:33 am

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

Postby Mofi » Tue Nov 02, 2010 12:56 pm

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();
   UltraEdit.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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts