Awesome! Once again, I won't pretend to understand the code one hundred percent but it seems to work perfect! I should be able to adapt it to my needs! Thanks!
Original Message:
Sent: 09-27-2024 09:54
From: James Peltzer
Subject: Reading the filenames of every file in a directory
Hi Jake
Yes, there is a way to rename the file but it is a little complicated due to:
- the object returned by ogscript.getFile is of type "File" and has a function called "renameTo" but it requires another "File" as an argument (not just a name)
- ogscript.getFile only returns files that already exist
We can work around this by using ogscript.saveToFile to create a new empty "File" that we can retrieve with ogscript.getFile and pass as the argument to file.renameTo using a simple function like this:
function rename(oldPath, newName){ var file = ogscript.getFile(oldPath); var newFilePath = file.getParent() + file.separator + newName; ogscript.saveToFile(newFilePath, '', true); var newFile = ogscript.getFile(newFilePath); newFile.delete(); file.renameTo(newFile);}
The first argument for this function is the full path to the file and the second argument is just the new file name. This is an updated version of the panel with a button to exercise the feature:
<abs contexttype="opengear"> <meta> <api>function rename(oldPath, newName){ var file = ogscript.getFile(oldPath); var newFilePath = file.getParent() + file.separator + newName; ogscript.saveToFile(newFilePath, '', true); var newFile = ogscript.getFile(newFilePath); newFile.delete(); file.renameTo(newFile);}</api> <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"/> <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 && file.exists() && file.isDirectory()){ var filter = params.getValue('extension', 0).trim(); //this would be optional var list = file.list(); var filteredList = []; for (var i = 0; i < 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> <button buttontype="push" height="55" left="119" name="Rename to .procesed" top="190" width="254"> <task tasktype="ogscript">/*! block id=1003,1004,1005,1006,1008,1007 !*/rename((params.getValue('Starting_Directory', 0) + params.getValue('File_List', 0)), (params.getValue('File_List', 0) + ".processed"));/*!! <block id="1003" type="function_rename" x="541" y="261" w="243" oldPath="ID:1004" newName="ID:1008" /><block id="1004" type="math_add" x="272" y="239" VALUE="ID:1005" VAL_1="ID:1006" w="218" VAL_1_1="" /><block id="1005" type="param__top&amp;Starting Directory (Starting_Directory)[0]" x="36" y="204" w="243" SET="" /><block id="1006" type="param__top&amp;File List (File_List)[0]" x="67" y="391" w="243" SET="" /><block id="1008" type="math_add" x="405" y="408" VALUE="ID:1007" VAL_1=".processed" w="218" VAL_1_1="" /><block id="1007" type="param__top&amp;File List (File_List)[0]" x="182" y="522" w="243" SET="" />!!*//*!!<checksum>71c99d91d1ebb47d7c6a14d868f7faac</checksum>!!*/</task> </button></abs>
------------------------------
James Peltzer
Ross Video
Original Message:
Sent: 09-26-2024 15:35
From: Jake Lew
Subject: Reading the filenames of every file in a directory
Follow up question... would there happen to be a way to rename a file from Dashboard? That way I can ensure anything dropped in that folder gets formatted properly.
------------------------------
Jake Lew
Access Communications Co-Operative Limited
Regina Canada
Original Message:
Sent: 01-24-2024 11:01
From: James Peltzer
Subject: Reading the filenames of every file in a directory
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 && file.exists() && file.isDirectory()){ var filter = params.getValue('extension', 0).trim(); //this would be optional var list = file.list(); var filteredList = []; for (var i = 0; i < 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
Original Message:
Sent: 01-24-2024 10:49
From: Jake Lew
Subject: Reading the filenames of every file in a directory
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
------------------------------