Facility Control

 View Only
  • 1.  Reading the filenames of every file in a directory

    Posted 01-24-2024 10:50

    I am looking to use Dashboard code to look at a directory and read all the filenames in the directory. The idea is to have a folder of images that can be dropped in, and Dashboard would be able to read each filename to then send back to XPression as a material for a rotating slideshow. 

    I know I can verify a specific file using "ogscript.getFile(filepath)" but I am looking for a way to not just verify a specific file, but to look at the folder itself and give me back what files are in there.

    Any help would be appreciated.

    Thanks!



    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------


  • 2.  RE: Reading the filenames of every file in a directory

    Ross Staff
    Posted 01-24-2024 11:02

    Hi Jake

    Here is an example panel that does this.  You're on the right track with getFile - argument passed-in can be either a file or a directory.  To get an array of the Files inside of a directory, call getFile with the directory path and use list() on the returned object.  Here is an example panel that exercises the feature:

    <abs contexttype="opengear">
       <meta>
          <params>
             <param access="1" maxlength="0" name="Starting Directory" oid="Starting_Directory" type="STRING" value="C:\" widget="text"/>
             <param access="1" constrainttype="STRING_CHOICE" maxlength="0" name="File List" oid="File_List" type="STRING" value=" " widget="combo">
                <constraint/>
             </param>
             <param access="1" maxlength="0" name="File Extension" oid="extension" type="STRING" value="*" widget="default"/>
          </params>
       </meta>
       <label height="40" left="20" name="Starting Directory: " style="txt-align:east" top="20" width="160"/>
       <param expand="true" height="40" left="180" oid="Starting_Directory" top="20" width="380"/>
       <label height="40" left="20" name="File Extension: " style="txt-align:east;" top="70" width="160"/>
       <param expand="true" height="40" left="180" oid="extension" showlabel="false" top="70" width="160"/>
       <label height="40" left="20" name="File List: " style="txt-align:east" top="120" width="160"/>
       <param expand="true" height="40" left="180" oid="File_List" showlabel="false" top="120" width="380"/>
       <button buttontype="push" height="40" left="580" name="Load" top="20" width="100">
          <task tasktype="ogscript">var file = ogscript.getFile(params.getValue('Starting_Directory', 0));
    if (file != null &amp;&amp; file.exists() &amp;&amp; file.isDirectory())
    {
       var filter = params.getValue('extension', 0).trim();    //this would be optional
       var list = file.list();
       var filteredList = [];
    
       for (var i = 0; i &lt; list.length; i++)
       {
          if (filter == '*' || list[i].toLowerCase().endsWith(filter))  //Check the filter
          {
             filteredList.push(list[i]);
          }
       }
    
       if (filteredList.length == 0)  //Put a blank if we have no files that match the filter
       {
          filteredList.push("");
       }
    
       var constraint = params.createStringChoiceConstraint(filteredList);
       if (constraint != null)
       {
          params.replaceConstraint('File_List', constraint);
       }
    }</task>
       </button>
    </abs>
    

    The relevant snippet is this:

    var file = ogscript.getFile(params.getValue('Starting_Directory', 0));
    if (file != null && file.exists() && file.isDirectory())
    {
       var filter = params.getValue('extension', 0).trim();    //this would be optional
       var list = file.list();
    ...


    ------------------------------
    James Peltzer
    Ross Video
    ------------------------------



  • 3.  RE: Reading the filenames of every file in a directory

    Posted 01-24-2024 11:26

    Works like a charm! Thanks for the speedy response on this! 



    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------