Link to home
Create AccountLog in
Avatar of Doug Mewmaw
Doug MewmawFlag for United States of America

asked on

Java Script (List items in a folder) - wildcards



Recently, I was given a neat solution to list items in a folder...as I'm implementing the solution, I discovered I need to change the solution slightly.   I'm betting it’s an easy solution.

Today, I can list items in a folder:

MPGreport1.html
MPGreport2.html
and so on

in the code, this is what made it happen:

  var FilePatern = /MPG.+Rpt\.htm/i;

In my real life situation, the MPG might change to ABC, XYZ, depending on the customer...so the var statement needs to be altered a bit.  Can one use a wild card so that I can get something like this:

MPGreport1.html
ABCreport2.html
DDDreport1.html
JOBreport2.html

I have the flexibility to change the report name where the unique ID is at the end of the file name, but before I do that I wanted to see if a wildcard can be used

thanks!

<script>
var Fo; // = new ActiveXObject("Scripting.FileSystemObject");
var StrOut = "";
var FilePatern = /MPG.+Rpt\.htm/i;

function FindFile(FOo) {
    var FSo = new Enumerator(FOo.Files);
    for (var i=0 ; !FSo.atEnd(); FSo.moveNext()) {
        if(FSo.item().name.match(FilePatern)) {
                StrOut += 
                    "<tr " + ((i%2) ? "" : "bgcolor=\"#FFFFFF\"") + ">" + 
                    "<td width=\"40%\" class=\"Find\"><a href=\"" + FSo.item().name + "\">" + FSo.item().name + "</a></td>" + 
                    "</tr>";
                i++;
        }
    }
}

function Scan() {
    if ((path.value.length > 0) && (Fo.FolderExists(path.value))) {
        StrOut = "<table border=\"0\" width=\"100%\" cellspacing=\"0\">"
        FindFile(Fo.GetFolder(path.value));
        outPut.innerHTML = StrOut + "</table>";
    }
    else alert("Insert Correct Path Address");
}

function page_load() {
    Fo = new ActiveXObject("Scripting.FileSystemObject");
	Scan();
}

window.onload = page_load;
</script>

Open in new window

SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sory, should be:

if (FSo.item().name.substr(3,6) == "report") {
Avatar of Doug Mewmaw

ASKER

What if the prefix varies in size?

For example:

MPG520P_ReportType
DDD77777_ReportType

The prefix is uder defined but it will never be more than 10 character

another thought too

I know how to list all HTM items which works pretty neat for me. However, I need to exclude one item in the list

For example if I do this:

var FilePatern = /\.htm/i;

I get this:

MPG520P_ReportType
DDD77777_ReportType
Output_Item_html

I really need to exclude the Ouput item *Which will always be Output_ in the 1st 7 characters so that I only list these valid items:

MPG520P_ReportType
DDD77777_ReportType





ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You just have to match the pattern that you want, that will exclude all other files.

You can allow any prefix you like, as long as there is some part of the file name that is always identifiable, for example anything from three to ten characters, followed by "report" and a number:

  var FilePattern = /^.{3,10}report\d+\.html$/i;

Note: ^ matches the beginning of the string, and $ matches the end of the string, so that the pattern matches the entire file name and not just anything that is contained inside a file name. That way the pattern matches for example "ABCreport1.html" but not "Output_ABCreport1.html.txt".

All good suggestions...The easiest solution for me is to do file name checking
(07/11/11 08:20 AM, ID: 36166853)

But I just ran into something that made me laugh... that I haven't asked yet

What if my search finds zero items?   Can I throw a message "No Items Found"
Whoa - zero isn't funny, unless it's of the "Mostel" type ;-)
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Great job everyone!   I owe a few folks a drink of your choice some day!
No worries - a glass of Tanquery, 3 ice cubes and 2 olives - the perfect martini ;-)
Hey...You helped with this last year and I need some guidance again...

You helped me list items in a folder...

<script>
var Fo; // = new ActiveXObject("Scripting.FileSystemObject");
var StrOut = "";
var FilePatern = /(MPG|ABC|DDD|JOB).+Rpt\.htm/i;

function FindFile(FOo) {
    var FSo = new Enumerator(FOo.Files);
    for (var fileCount=0 ; !FSo.atEnd(); FSo.moveNext()) {
        if(FSo.item().name.match(FilePatern) && !FSo.item().name.match(/^Output_/i)) {
                  fileCount++;
            StrOut +=
                    "<tr " + ((fileCount%2) ? "" : "bgcolor=\"#DDAA55\"") + ">" +
                    "<td width=\"40%\" class=\"Find\"><a href=\"" + FSo.item().name + "\">" + FSo.item().name + "</a></td>" +
                    "</tr>";
        }
    }
      if(fileCount==0){
            StrOut += "<tr><td>No Reports found in upper directory.</td></tr>";
      }
}

function Scan() {
    if ((path.value.length > 0) && (Fo.FolderExists(path.value))) {
        StrOut = "<table border=\"0\" width=\"100%\" cellspacing=\"0\">"
        FindFile(Fo.GetFolder(path.value));
        outPut.innerHTML = StrOut + "</table>";
    }
    else alert("Insert Correct Path Address");
}

function page_load() {
    Fo = new ActiveXObject("Scripting.FileSystemObject");
      Scan();
}

window.onload = page_load;
</script>

--------------------------------------------------------
How does one point to a specific folder location?  Is there an input field in the HTML?

Where does the folder path name reside?
Not sure - I don't see where path is declared or initialized?
In my editor is that line number 394:

<input size="125" type="text" value="C:\Program Files\Midrange Performance Group\Performance Navigator 15\Factory\Output\Monthly" id="path" name="path" class="Field" ></td>
Yes, but path.value looks like a property of an object - if you want the value of a DOM object, I'd use document.getElementById('path').value.

But more to the point: what are you trying to do now?
"Is there an input field in the HTML?" :
Input field has the name: path

"Where does the folder path name reside?" :
The default folder is placed as String into input field value of field: path
Try this:
function Scan(path) {
    if (path && (Fo.FolderExists(path)) {
        StrOut = "<table border=\"0\" width=\"100%\" cellspacing=\"0\">";
        FindFile(Fo.GetFolder(path));
        outPut.innerHTML = StrOut + "</table>";
    }
    else alert("Insert Correct Path Address");
}

function page_load() {
    Fo = new ActiveXObject("Scripting.FileSystemObject");
    Scan(document.getElementById('path').value);
}

Open in new window