File Helper - Create Array of File Names

Hi @emre

It seems Jscript is missing the ability to read from a PATH using WILDCARDS to create a Array of File Names. It would be complimentary to the File Helpers you have now.

Some like:
filearray = file.ReadFromPath(filePath,wildcards|name);

Wildcards|Name values could be “.CVS"or “MyFile.txt” or "My.*”

Can you please consider.

1 Like

You could do this using FileSystemObject …

function getFileList(p,f)
  var objFSO = new ActiveXObject("Scripting.FileSystemObject");

  var fldr = objFSO.GetFolder("D:/Programs/POS/");
  var files = new Enumerator(fldr.Files);

  var fileList = new Array();
  var fltext = "";

  for (; !files.atEnd(); files.moveNext()) {
    fltext += files.item() + "\r\n";
  //return fileList.length;
 return "Count:"+fileList.length + "\r\n" + fltext;
  //return fileList;

You would need to build a routine to parse the wildcards and sort things out to return an array, but it could become a useful utility/function to have on-hand.

Yea read up on “ActiveXObject” and saw a few issues with compatibility going forward. I elected to do something simple with “ACTION: Start Process” and DIR C:\SPOS\*.clb /b > C:\SPOS\check_clb.out - directing output to a text file.

Reading the file is easy using file.ReadFromFile(filePath);.

1 Like

I agree. The FSO is ok but it does not have near the number of features of .NET file/directory handling.

I think it would be much simpler if @emre could give us this type of facility in the form of a helper.

This seems to work ok though …

function getFileList(dirName,pattern)
  var dirName = "D:\\Programs\\POS";
  var pattern = "*.csv";

  var wsh = new ActiveXObject("WScript.Shell");
  var fso = new ActiveXObject("Scripting.FileSystemObject");

  var outputFile = dirName + "\\fileList.flo";

  var oExec = wsh.Run('%comspec% /c dir /on /b "' + dirName + '\\' + pattern + '"' + " > " + '"' + outputFile + '"', 0);

  var fileContent = file.ReadFromFile(outputFile);
  var lines = fileContent.split("\r\n");
  var lineCount = lines.length-1;

  var delSuccess = fso.DeleteFile(outputFile);
  var fileList = new Array();
  var to = "";

  for (var f=0; f<lineCount; f++)
    to += lines[f] + "\r\n";
  //return fileList;
  return "Files matching [" + pattern + "] : " + lineCount + "\r\n" + to;

Aye commander!

PS: ToArr() converts .net array to a JS array.

1 Like

Somehow I figured we might have access to such things. Have you mentioned host.type() previously?

EDIT: yup, right here …

The following already works, right now! :stuck_out_tongue_winking_eye:

function getFileList(path,pattern)
  var path = "D:/Programs/POS";
  var pattern = "*.txt";
  var folder = host.type("System.IO.Directory");
  var fileList = folder.GetFiles(path,pattern).ToArr();//.join();
  var fileCount = fileList.length;
  var fileArray = new Array();
  var to = "";
  for (var f=0; f<fileCount; f++)
    var fileNameOnly = fileList[f].substr(path.length+1,fileList[f].length-path.length);
    to += fileNameOnly + "\r\n";
  // return path and filename
  //return fileList;
  // return filename only
  //return fileArray;
  // return some test output
  return "Files matching [" + pattern + "] : " + fileCount + "\r\n" + to;


Sorry for bumping an old thread but during implementation of this function @emre I realised we do not have a “File.Exists” feature to check for existence of a File or Directory?

If we use a path parameter that does not exists Samba crashes out.

I currently have implemented:

  var fso = new ActiveXObject("Scripting.FileSystemObject") ;

  if (!fso.FolderExists(path)) {
    dlg.ShowMessage("Folder does not exist!:" + path) ;
    return '' ;

I think we want to discourage using ActiveX in favor of C# Types, like my last example.

Search for System.IO.File … there should be an exists method in there.

1 Like


 var folder = host.type("System.IO.Directory");
 var exists = folder.Exists("...");

Thanks @emre this works well:

  var folder = host.type("System.IO.Directory");
  if (!folder.Exists("...")) {
    dlg.ShowMessage("Folder does not exist!") ;
    return '' ;