Facility Control

 View Only
  • 1.  Check if file exists - getFile() only in full file path?

    Posted 09-01-2020 09:08

    So, I'm simply trying to see if a file exists... Would be great to simply check if an image file exists or not. And then send either a placeholder or the correct file to DataLinq/Xpression...

    Now, I found the help files and the development guide online.
    (https://www.rossvideo.com/products-services/management-systems/automated-production-control/dashboard/ - in the download section).

    Looked up the function "getFile()", and according to the documentation, it's supposed to return the following:

    Returns the File object found at the specified path if it was found, null otherwise.

    Now, if I put in a full filepath, that works just fine. It returns as expected.
    And can do a simple if null do this, else do that.
    However, seeing as I cant 100% know that the filepath will always stay the same, or the OS for that matter, I want to use the relative path compared to where the dashboard is located. That's how the file picker can work etc...

    So, instead of the full path, I try a file name in the same folder as the DashBoard...
    However, this always returns true, regardless of whatever filename you put in, existing or not.
    However it will return the full filepath to your folder, and add on the file you specified in the end... Telling me that this should work! But for some reason it does not.

    See example DashBoard here:

    <abs contexttype="opengear" gridsize="20" id="_top" keepalive="false" style="">
    <button buttontype="push" height="100" left="60" name="&lt;html&gt;&lt;center&gt;Checkfile&lt;br/&gt;Full Filepath&lt;/center&gt;&lt;/html&gt;" top="60" width="220">
    <task tasktype="ogscript">var file = ogscript.getFile('C://DashBoard/artifacts.xml');

    if(file == null) {
    ogscript.debug('No such file...');
    } else {
    ogscript.debug(file);
    }</task>
    </button>
    <button buttontype="push" height="100" left="300" name="&lt;html&gt;&lt;center&gt;Checkfile&lt;br/&gt;Relative Filepath&lt;/center&gt;&lt;/html&gt;" top="60" width="220">
    <task tasktype="ogscript">var file = ogscript.getFile('THIS FILE DOES NOT EXIST.txt');

    if(file == null) {
    ogscript.debug('No such file...');
    } else {
    ogscript.debug(file);
    }</task>
    </button>
    </abs>

    Working as intended? Or should the getFile have returned null with a relative path as well?
    DB version: 9.1.2



  • 2.  RE: Check if file exists - getFile() only in full file path?

    Posted 09-01-2020 12:09

    So, after I keep digging, and keep reading, I finally find the getPanelRelativeURL() function (yeah guess I'm blind).
    However, documentation states that this will return the following:

    Returns a String representing the full path of the relative path with respect to the panel's path.

    Example
    // If we have a panel stored at C:\Users\Test\Panels\ and we store images in a // directory \Images\
    located in the same \Panels\ folder that the panel itself is located in, we can // use the line
    ogscript.getPanelRelativeURL('\Images\');
    // to get the String "C:\Users\Test\Panels\Images\".

    However, no, this does not work.
    In the example above, for some reasons they add escape strings in front of the ' for the string sub folder we're looking for.
    What you should be using is:

    var relPath = ogscript.getPanelRelativeURL('Images/');

    However, this will also not give you back the expected string mentioned in the example above. You'll get something similar, but you will also get a "file:/" before the file path itself, like so:

    file:/C:\Users\Test\Panels\Images\

    This again cannot be used in the getFile() function before. And not to mention if you had a space in the folder names or similar, you will get this returned as URI encoded characters, with %20 for space etc... These cannot be used for the getFile() function either.
    So, first thing you do is just to get rid of the file:/ by using the JS slice function. Then follow it up by using a decodeURI on the string as well for good meassure.

    var path = file:/C:\Users\Test\Panels\Images\;
    path = path.slice(6);
    path = decodeURI(path);

    Then you'll finally end up with a clean string, formatted in such a way that it can be used for the getFile() function.
    Now with the get file function, you can add whatever file name you're looking for, either through a parameter or just a simple string value, concatenate the two, and you can check for whatever file you want.

    var file = 'BLANK.png'; // The filename we're looking for.

    path = ogscript.getPanelRelativeURL('Images/'); //DB path + subfolder "Images"
    path = path.slice(6);
    path = decodeURI(path);
    path = path + file;

    var res = ogscript.getFile(path);
    if (res == null) {
    ogscript.debug('RESULT: No such file...');
    // Do whatever you need if there is no such file...
    } else {
    ogscript.debug('RESULT: ' + res);
    // Do what you want to do when you confirm there is a file there...
    }

    Hope someone might find this usefull!


    #DashBoard


  • 3.  RE: Check if file exists - getFile() only in full file path?

    Posted 09-01-2020 14:21

    Hello Aleksander,

    I saved your final panel as 'If file exists check.grid'. Thank you for sharing!
    By the way, if we talking about file managing, i found that res.delete(); will delete the file. Copy file is also possible, example can be found if you search 'copy file' on forum. Now the missing part is how to rename / cut the files.


    #DashBoard