file plugin no deleting files consistently

I have an application where I file 500K JPEG images in the database and then delete them. Filing the photos in the backend database works fine. When I try to delete files in a folder using this:

var contentArray = plugins.file.getFolderContents(selecteddirectory,new Array(‘.JPG’,‘.JPE’,‘.JPEG’,‘.jpg’,‘.jpe’,‘.jpeg’),1)
var count = contentArray.length
for(var i = 0 ; i < count ; i++)
{
var thefilename = contentArray
var success = plugins.file.deleteFile(thefilename)
}
I get strange results. Some files in the directory are delete, others are not. If I run it again some of the ones that didn’t delte the first time around will delete Even when the file is not deleted the var success will evaluate as true.
John McCann
Servoy 2.2.1
MS SQL Server

Hi John,

Could this be due to a timing issue between Servoy and the OS ?

Maybe the OS is struggling to match the speed of the recurring delete commands being sent to it ?

Could you try using an ‘application.sleep(500)’ after the delete command as in :

var contentArray = plugins.file.getFolderContents(selecteddirectory,new Array(‘.JPG’,‘.JPE’,‘.JPEG’,‘.jpg’,‘.jpe’,‘.jpeg’),1)
var count = contentArray.length
for(var i = 0 ; i < count ; i++)
{
var thefilename = contentArray
var success = plugins.file.deleteFile(thefilename)
application.sleep(500);
} [/quote]
You can obviously adjust your sleep time to suit but this may help
Cheers
Harry

It probably is some sort of timing error. I think this to be the case because I can reproduce the error in the developer but when I step through it in the debugger it deletes all of the rfiles. I have tried putting delays both before and after the delete command without success.

John McCann

I looked into this further. The error does not occur when running Servoy on Java 1.4. It only occurs on Java 5.0. I cannot make the transition to 5.0 till this gets fixed. I would like to make the transition as I noted an overall performance improvement when moving from 1.4 to 5.0.

John McCann
Servoy Client
Version R2 2.2.1-build 333
Java version 1.4.2_09-b05 (Windows XP) versus 1.50 release 4
MS SQL Server

John,

I have logged this into our internal system. Are you still seeing it with 2.2.4?

I looked into this further and have a found the an acceptable solution. The issue improved much either with improvements in Servoy and or changes in Jave itself or both. However it did not resolve completely and remained an issue when deleting files on slow drives like USB mounted memory cards.

I also noted that in Java 1.4.2.x the file plugin often accepted the text form of a variable specifying a file name. In Java 5.0 it is very important that the JSFile form of the variable be used. I also test to be sure the file is actually deleted using the .exist() function. If it fails to delete I put the applicaiton to sleep for 500 ms and then delete again. This seems to take care of it.

John McCann

I changed my mind. It is not really an acceptable work around. It can be forced to delete a file if one does this:

plugins.file.deleteFile(filename)
while(filename.exist())
{
application.sleep(500)
plugins.file.deleteFile(filename)
}

However, sometimes it hangs for almost a minute before actually deleting the file. This never happens when using Java 1.4.2.x and continues to be an issue in Java 1.5.0.6

John McCann
Servoy 2.2.4
Windows
MS SQL Server

There are some news about this problem that we have tested?

Servoy Developer
Version R2 2.2.4-build 336
Java version 1.4.2_08-b03 (Windows XP)

we just call directly the java file.delete()
so it has to be some issue that the file is locked somehow and the delete can’t happen.
what does deleteFile return? Always just true?

or maybe an issue with rights?

“we just call directly the java file.delete()
so it has to be some issue that the file is locked somehow and the delete can’t happen.
what does deleteFile return? Always just true?”

When it fails to delete the file it returns false. I do not believe it can be an issue with rights because the file is not locked and if you hammer away and ask it to delete the file a couple hundred times, it eventually does. It works fine when Servoy is run in Java 1.4.x but not 1.5. It continues to be a problem for us. What does the Servoy team need me to do to help adress the problem?

John McCann

Maybe this has something to do with it?

http://forum.java.sun.com/thread.jspa?t … &tstart=45

no that is not it because this code:

var contentArray = plugins.file.getFolderContents(selecteddirectory,new Array('.JPG','.JPE','.JPEG','.jpg','.jpe','.jpeg'),1)
var count = contentArray.length
for(var i = 0 ; i < count ; i++)
{
var thefilename = contentArray[i]
var success = plugins.file.deleteFile(thefilename)
application.sleep(500);
}

is used as an example. And then no streams are made to those files so there is no stream to close.
Or if somebody can tell me how and when those files where created that you are trying to delete now.

The files are created by a digital camera. After I create them [typically about 5] a memory stick is removed from the camera and plugged into the computer. The user tells the software the location of the folder and the software moves each jpeg file to a new record in the database. After all jpegs are moved I delete them. [I have seen the same issue when I copy the jpeg files to a folder on the hardrive so it is not just an issue with using a memory card.]

Java sees the file as existing if I use the .exist funciton. It is just unable to delete the files unless one call the delete many times over. I have changed the code to delete the entire directory but this also takes a very long time and many calls to get it to complete.

John McCann
Windows XP
Servoy 2.2.4
Java 1.5
MS SQL Server

Maybe a stream is opened up when the file is accessed to copy it to the database. I note that the copy operation happens much faster in Java 1.5 than Java 1.4.x so something must be different about how Java accomplishes the copy.

Maybe you should try System.gc() thing to see if it fixes the problem. The issue mentioned on the JAVA forum sounds like it is at least barking up the right tree. However, in the JAVA forum they had problems in 1.4.x as well.

Thanks for looking into this.

John McCann

what is the code how you load those images in the database?

Also when you don’t do this.. So no loading just deleting. Does it then also happen?

This is the entire method. The section in bold does the loading.

if (!globals.GlobalDateVariable1 || !globals.GlobalTextVariable1 || !globals.GlobalTextVariable2 || !globals.GlobalTextVariable3)
{
globals.WarningDialogue(‘Fill out the required fields before proceeding.’);
return;
}

// If not defined start looking for images on the desktop
if (!globals.Directory_Images_Media_Input)
{
globals.Directory_Images_Media_Input = plugins.file.getDesktopFolder();
}

//select directories starting at the specified folder
var selecteddirectory = plugins.file.showFileOpenDialog(2, globals.Directory_Images_Media_Input)
if(!selecteddirectory)
{
globals.WarningDialogue(‘No files were saved.’);
return;
}

//Get files only from the selected directory
var contentArray = plugins.file.getFolderContents(selecteddirectory,new Array(‘.JPG’,‘.JPE’,‘.JPEG’,‘.jpg’,‘.jpe’,‘.jpeg’),1)
var count = contentArray.length
if(count == 0)
{
globals.WarningDialogue(‘Their are no photos in this folder.’);
return;
}

//Remember the directory with the media files
globals.Directory_Images_Media_Input = selecteddirectory
//Prevent acidental data change during photo download
controller.newRecord();
demographicsid = globals.demographicsid;
date_image = globals.GlobalDateVariable1;
digital_asset = globals.GlobalTextVariable1;
picture_type = globals.GlobalTextVariable2;
picture_of = globals.GlobalTextVariable3;
comments = globals.GlobalTextVariable4;
controller.saveData()
controller.readOnly = true
//for every file in the selected directory
globals.MediaVar1 = null
elements.CancelButton.visible = false
elements.FileButton.visible = false
elements.bean_256.visible = true
elements.bean_256.string = “Filing photos…”
elements.bean_256.maximum = count //max value allowed
for(var i = 0 ; i < count ; i++)
{
elements.bean_256.value = i + 1
application.updateUI()
var thefilename = contentArray
* thefilename = plugins.file.convertStringToJSFile(thefilename)*
* var filesize = plugins.file.getFileSize(thefilename)[/b] var thesize = 0*
* var q = 1*
* var thejpeg = null*
* while((thejpeg == null || thejpeg == -1) && q < 10)*
* {*
_ thejpeg = plugins.file.readFile(thefilename);_
* q = q+1*
* }*
* if (filesize < 3000000)*
* {*
images_to_images_assets_thumbnails.newRecord();
** controller.saveData()** var theprimarykey_of_asset = images_to_images_assets_thumbnails.images_assets_thumbnailsid
* while(images_to_images_assets_thumbnails.full_size == null)
__
{__
images_to_images_assets_thumbnails.full_size = thejpeg }
images_to_images_assets_thumbnails.filesize = filesize*
* images_to_thumbnails_filing.newRecord()
controller.saveData() images_to_thumbnails_filing.image_assets_thumbnailsid = theprimarykey_of_asset
while(images_to_thumbnails_filing.thumbnail == null)
__
{__
images_to_thumbnails_filing.thumbnail = application.createThumbnailJPGImage(thejpeg, 160,120)
__
}__
while(images_to_thumbnails_filing.thumbnail_large == null)
__
{__
images_to_thumbnails_filing.thumbnail_large = application.createThumbnailJPGImage(thejpeg, 500,384)
__
}__
__
controller.saveData();__
__
}__
__
else*__
* {*
* globals.WarningDialogue(‘You cannot file images greater than 3MB in size. This image was not stored. Please refer to your camera users manual for information on how to decrease the resolution of the camera or increase the compression of the JPEG images.’);*
* }*
* }*
// Ended up deleting the input folder and then recreating it because in Java 1.50_6 we
// had problems deleting individual files with the file plug in.
if(globals.DeleteInputJPG == ‘Yes’)
* {*
* elements.bean_256.string = “Deleting input folder.”
__
application.updateUI()__
__
var success = plugins.file.deleteFolder(selecteddirectory, false)__
__
while(!success)__
__
{__
__
success = plugins.file.deleteFolder(selecteddirectory, false)__
__
}__
elements.bean_256.string = “Creating empty input folder.”
__
application.updateUI()__
__
var success = plugins.file.createFolder(selecteddirectory)__
__
}*__
elements.bean_256.string = “Finsished”
application.beep()//signal when ended
application.sleep(1000) // bar remains visible 1 more second before disappearing
elements.bean_256.visible = false
elements.CancelButton.visible = true
elements.FileButton.visible = true
controller.readOnly = false
globals.WindowReset();

please test if you also get that not deleting behaviour if you don’t read the file.

By the way, Why are you doing this:

var thefilename = contentArray
thefilename = plugins.file.convertStringToJSFile(thefilename)
var filesize = plugins.file.getFileSize(thefilename)
the contentArray[] already contains JSFile objects..
so this works just as fine:
var thefilename = contentArray
var filesize = plugins.file.getFileSize(thefilename)
i checked this method:
thejpeg = plugins.file.readFile(thefilename);
and we are closing everything just fine.
Also if you test by completely not doing the script you just send and if it then works can you the test if it work by running that script but just not this line:
thejpeg = plugins.file.readFile(thefilename);
So don’t read the actual file.

Johan,

I think [at one time] the code where you get the size of a file did not work with the JSFile object so I used the string. It is probably fixed now and I should change the code, but doesn’t have anything to do with the current problem.

Jim Olsen will get you a sample solution within an hour. Preliminary testing indicates that we only see the problem deleting files if we copy the files to the database prior to attempting to delete them. So it seems that Java is still hanging onto them after the copy for some reason.

JDM

Here is a sample solution that simply reads the files, then tries to delete them. If I comment out the code that reads the files, it will delete all of the files just fine. If I first read the file, then try to delete it (even while retrying and sleeping), the delete fails. Simply run this solution and push the button. Select a directory with a bunch of jpegs in it, and it will go through and try to delete them all.

The solution includes sample data, but doesn’t rely on any data to do its thing.

Jim

FileDeleteTest.servoy (4.74 KB)