Page 1 of 1

Script to empty a folder on a daily basis

PostPosted:Mon Jul 20, 2020 2:43 pm
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

Re: Script to empty a folder on a daily basis

PostPosted:Fri Jul 24, 2020 5:31 pm
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
}

Re: Script to empty a folder on a daily basis

PostPosted:Thu Jul 30, 2020 1:34 pm
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();

Re: Script to empty a folder on a daily basis

PostPosted:Sun Aug 02, 2020 7:57 am
by jllort
Code: Select all
for (Document doc : OKMDocument.getInstance().getChildren(token, "/okm:root/FromScanner/")) {
    OKMDocument.getInstance().purge(token, doc.getUuid());  // was missing parentesis
}