3 out of 8 rated this helpful - Rate this topic

Files Property (FileSystemObject)

Returns a Files collection consisting of all File objects contained in the specified folder, including those with hidden and system file attributes set.


                      object.Files 

The object is always a Folder object.

The following code illustrates the use of the Files property:

function ShowFolderFileList(folderspec)
{
   var fso, f, fc, s;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f = fso.GetFolder(folderspec);
   fc = new Enumerator(f.files);
   s = "";
   for (; !fc.atEnd(); fc.moveNext())
   {
      s += fc.item();
      s += "<br>";
   }
   return(s);
}

Function ShowFileList(folderspec)
   Dim fso, f, f1, fc, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.Files
   For Each f1 in fc
      s = s & f1.name 
      s = s &   "<BR>"
   Next
   ShowFileList = s
End Function

Applies To:

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
DIR command and read-only files
Be aware that the DIR command excludes read-only files by default (WinXP SP3, 2 nov 2011)

To includes read-only files, use :
DIR /B /A-D
/A-D is used to exclude folder, but for no apparent reason it also includes read-only files (!?)

Note that you could use DIR /B /AR, but then only read-only files are listed.
DIR /B /AR /AH /AS /AA could do the job but it excludes all files not having any attribute.
So /A-D seems to be the way to go.

Thanks to Ashot for the DIR trick
Avoid regex altogether
Excellent tip by Ashot.  Use /B param on dir command (cmd /C dir /B MyfileSpec) and there is no need to use regular expressions.
RE: A much faster method...
Wow, but this is far faster Ashot, thanks much.

For the regex challenged out there, here's an example with regex using Ashot's code.

This is only going to grab filenames without spaces consisting of "word" characters [a-zA-Z_0-9] from the c:\test folder.

var wshShell = new ActiveXObject("WScript.Shell");
var oExec = wshShell.Exec("cmd /C dir C:\\test\\*.png");

var directoryContentLine;
var fileNameRegex = /[^\s]\w*\.png/i;
var fileNameMatch;
var fileNames = new Array();

while (!oExec.StdOut.AtEndOfStream)
{
directoryContentLine = oExec.StdOut.ReadLine();
fileNameMatch = fileNameRegex.exec(directoryContentLine);
if (fileNameMatch != null)
{
fileNames[fileNames.length] = fileNameMatch[0];
}
}

A much faster method to enumerate files that also supports wildcards

Here's what works for me when I use CScript.exe and need a very quick file enumerator. This works fast for both local and network directories and can save literally hours compared to FileSystemObject methods when there are thousands of files. This method uses the OS dir command and parses its output.

var wshShell = new ActiveXObject("WScript.Shell");
var oExec = wshShell.Exec("cmd /C dir c:\\MyDirectory\\MyFilePattern*.*");

var directoryContentLine;
var fileNameRegex = /MyFilePatternRegEx/i;
var fileNameMatch;
var fileNames = new Array();

while (!oExec.StdOut.AtEndOfStream)
{
directoryContentLine = oExec.StdOut.ReadLine();
fileNameMatch = fileNameRegex.exec(directoryContentLine);
if (fileNameMatch != null)
{
fileNames[fileNames.length] = fileNameMatch[0];
}
}