Is anyone else having trouble with getFolderContents? I am trying to get a listing of filenames in a directory, but it appears that the files are put in the array without quotes, so when I try to access the individual elements in the array, it thinks they are index objects.
Here’s the code I’m working with (it does work in 3.5.7):
vDirFiles = plugins.file.getFolderContents(dir, new Array(‘jpg’));
var aFiles = databaseManager.createEmptyDataSet(vDirFiles.length,1)
for(var x=1; x <= vDirFiles.length; x++)
{
var vPhotoFile = vDirFiles(x-1).toString() //aFiles.setValue(x,1) = vDirFiles[x-1]
aFiles.setValue(x,1) = vPhotoFile
}
I replaced the toString with getAbsolutePath, but I still get a TypeError - I think it’s because the getFolderContents returns the fully qualified path name, but I don’t think it’s a string in the array.
For example, I have two jpg files in a directory. The vDirFiles array gets both filenames from getFolderContents:
index 0 = c:\pictures\file1.jpg
index 1 = c:\pictures\file2.jpg
Notice that there are no quotes around the pathnames. So when I try to reference vDirFiles(1) it doesn’t know what to do. Am I missing something here? Or is this a bug? If it’s a bug, is there a workaround?
Hi Marcel, again I get the error. Here’s the error text:
TypeError: org.mozilla.javascript.NativeJavaArray@120ff77 is not a function, it is org.mozilla.javascript.NativeJavaArray.
Seems to me that when the getFolderContents method returns the path, and the path is NOT in quotes, Servoy then views the individual path values as an array, and therefore you can not access the array elements as you normally. This is the reason I tried to convert the array elements to strings, thinking that I could force the values to strings. If I could access the path as a string, then I could make this work, but since there are no quotes around the pathname, when I make a call to the element (vDirFiles(c:\pictures\file1.jpg) I get the TypeError. Again, IF the path had quotes in it, this would not be a problem.
cyarbrough:
For example, I have two jpg files in a directory. The vDirFiles array gets both filenames from getFolderContents:
index 0 = c:\pictures\file1.jpg
index 1 = c:\pictures\file2.jpg
Notice that there are no quotes around the pathnames. So when I try to reference vDirFiles(1) it doesn’t know what to do. Am I missing something here? Or is this a bug? If it’s a bug, is there a workaround?
The reason you had the error was because you were using round brackets (means call function) to access vDirFiles in stead of square brackets (which means access by index).
There was also the error mentioned in erdiones post.
Try his code:
Rob
vDirFiles = plugins.file.getFolderContents(dir, new Array('jpg));
var aFiles = databaseManager.createEmptyDataSet(vDirFiles.length,["photo"])
for(var x=1; x <= vDirFiles.length; x++)
{
var vPhotoFile = vDirFiles[x-1].getAbsolutePath()
aFiles.setValue(x,1, vPhotoFile)
}
var uri = aFiles.createDataSource('mydata', [ DM_COLUMNTYPE.TEXT]);
var jsform = solutionModel.newForm("myform", uri, null, true, 300, 300);
jsform.newTextField("photo",100,130,100,20);
forms.myform.controller.show()