Script to empty a folder on a daily basis

OpenKM has many interesting features, but requires some configuration process to show its full potential.
Forum rules
Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
Post Reply
rwebb
Fresh Boarder
Fresh Boarder
Posts: 14
Joined: Wed Jun 24, 2020 4:51 pm

Script to empty a folder on a daily basis

Post by rwebb »

Hello,

I don't know if configuration is the proper place for this topic so mods feel free to move it if not.

Just wondering if someone could help me with a bsh script (or another way) to automatically delete all documents in a specific folder every day at a certain time? I'm using this because I have a scans folder that takes documents in from a scanner and then the user is supposed to then move those documents to the final location that they belong in. I want to delete all remaining files out of this scans folder every day so that users don't get the idea that the scan folder is just another convenient storage location.

I don't know if this has to be a bsh script or if something can be done with automation. I've tried a few calls to the API but I don't know what call to use to delete them.

Here is what I have - some of the code is commented as I was testing but this is the general idea:

Code: Select all

import com.openkm.api.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.db.stuff.*;
 
String token = DbSessionManager.getInstance().getSystemToken();
 
OKMFolder trash : OKMFolder.getInstance().getChildren(token, "/okm:root/FromScanner/"));
print ("Scanner path: " + trash.getPath() + "<br/>");

/*for (Folder trash : OKMFolder.getInstance().getChildren(token, "/okm:root/FromScanner/")) {
    print("Trash: " + trash.getPath()+"<br/>");
 
    for (Folder fld : OKMFolder.getInstance().getChildren(token, trash.getPath())) {
        print("About to delete folder: " + fld.getPath() + "<br/>");
        //OKMFolder.getInstance().purge(token, fld.getPath());
    }
 
    for (Document doc : OKMDocument.getInstance().getChildren(token, trash.getPath())) {
        print("About to delete document: " + doc.getPath() + "<br/>");
        
        if (OKMDocument.getInstance().isLocked(token, doc.getPath())) {
			print("Unlocking Document: " + doc.getPath() + "<br/>");
            OKMDocument.getInstance().forceUnlock(token, doc.getPath());
        }
        
		print("Purged Document " + doc.getPath() + "<br/>");
        //OKMDocument.getInstance().purge(token, doc.getPath());
    }
 
    /*for (Mail mail : OKMMail.getInstance().getChildren(token, trash.getPath())) {
        print("About to delete mail: " + mail.getPath() + "<br/>");
        OKMMail.getInstance().purge(token, mail.getPath());
    }*/
}
print ("End of Script! <br />");
Any help would be greatly appreciated!
Rich

jllort
Moderator
Moderator
Posts: 11162
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: Script to empty a folder on a daily basis

Post by jllort »

You want to delete all the documents into the folder /okm:root/FromScanner/ ?

You should do something like:

Code: Select all

for (Document doc : OKMDocument.getInstance().getChildren(token, "/okm:root/FromScanner/")) {
    OKMDocument.getInstance().purge(token, doc.getUuid); // you can use purge directly from the taxonomy, not necessary to send in the trash and later purge, obviously it is a dangerous strategy because you will no longer be able to recover the file, has been definitively removed from the repository
}

rwebb
Fresh Boarder
Fresh Boarder
Posts: 14
Joined: Wed Jun 24, 2020 4:51 pm

Re: Script to empty a folder on a daily basis

Post by rwebb »

I tried this and got this error:

Code: Select all

bsh.EvalError: Sourced file: inline evaluation of: ``import com.openkm.api.*; import com.openkm.core.*; import com.openkm.bean.*; . . . '' : Cannot access field: getUuid, on object: {path=/okm:root/FromScanner/20200728094928371.pdf, 
I'm guessing maybe I don't have all the imports I need maybe? I have this at the top:

Code: Select all

import com.openkm.api.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.db.stuff.*;

String token = DbSessionManager.getInstance().getSystemToken();

jllort
Moderator
Moderator
Posts: 11162
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: Script to empty a folder on a daily basis

Post by jllort »

Code: Select all

for (Document doc : OKMDocument.getInstance().getChildren(token, "/okm:root/FromScanner/")) {
    OKMDocument.getInstance().purge(token, doc.getUuid());  // was missing parentesis
}

Post Reply