SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem using javascript to check for a file

    I have tried extensively to find an answer to this question. Hopefully someone will be able to put my mind at ease.

    Is it possible to use client-side javascript to get a list of files from a folder?

    This computer is not connected to the internet or any other computer. The browser just needs to be able to see in a folder and make an array from its contents at certain intervals. This is w XP/MCE.

    I have been trying to use the getfolder function with a newActiveXObject but have not gotten it to work and I am not even certain it works client-side.

    Any help is appreciated. Thanks in advance.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple example:
    Code:
    <html>
    <head>
    <title>Filelist to array using FSO</title>
    <script type="text/javascript">
    function getFileList(folderSpec) {
    	var fs = new ActiveXObject("Scripting.FileSystemObject");
    	var folder = fs.GetFolder(folderSpec);
    	var en = new Enumerator(folder.Files);
    	var list = [];
    	
    	for (;!en.atEnd();en.moveNext()) {
    		list.push(en.item().Name);
    	}
    	return list;
    }
    function showFiles() {
    	var myList = getFileList("c:\\");
    	document.getElementById("files").innerHTML = myList.join("<br>");
    }
    </script>
    </head>
    <body>
    <div id="files"></div>
    <button onclick="showFiles();">Show Files</button>
    </body>
    </html>
    This will work only in IE/Windows

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much. Tested and works perfectly. I have been looking for this for at least a month with so many different sites/forums to no avail. You are a damn saint. Thanks again.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, glad to help.

    Shortly after I posted this I made a change to show folders as well, in case you need it here it is:
    Code:
    function getFileList(folderSpec) {
    	var fs = new ActiveXObject("Scripting.FileSystemObject");
    	var folder = fs.GetFolder(folderSpec);
    	var list = [];
    	
    	// subfolders
    	var en = new Enumerator(folder.Subfolders);
    	for (;!en.atEnd();en.moveNext()) {
    		list.push("FOLDER > " + en.item().Name);
    	}
    
    	// files	
    	var en = new Enumerator(folder.Files);
    	for (;!en.atEnd();en.moveNext()) {
    		list.push(en.item().Name);
    	}
    	return list;
    }
    PS - welcome to the forums

  5. #5
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jim I can't thank you enough for helping me get fso to work in my script...unfortunately this has left me with another problem.

    I can't seem to figure out how to separate the items in the list into an array.

    The goal would be to identify each item in the list by itself. Is this possible?

  6. #6
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally figured one out on my own. myList is the array which you can easily call with myList[0], myList[1], etc...

    Thanks again though. Start the timer until I'm back here looking for more answers after my next roadblock!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •