web stats
Delete old files - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-13-2008, 06:58 AM
JasonGriffith JasonGriffith is offline
OBX.1 Kenobi
 
Join Date: Aug 2007
Posts: 37
JasonGriffith
Default Delete old files

I'm trying to delete old files (older than 90 days or so)

I figure if I can list the files in a folder, I can look at how old each file is and delete the file if it is older than 90 days.

I am trying to use the following code

http://www.java-tips.org/java-se-tip...ular-dire.html

to list out the files, but I run into a problem when I try to use javascript syntax as suggested here:

http://www.mirthproject.org/index.ph...8&catid=2#6578


Code:
//use 
//   importClass(java.io.File); 
//instead of 
//   import java.io.File;
importClass(java.io.File);



//use  
//   var inputFile = new java.io.File("farrago.txt");
//instead of 
//   File inputFile = new File("farrago.txt");
    var folder = new java.io.File("c:/");


    java.io.File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
      }
    }
any suggestions on how to do it a better way, or suggestions on how to change this code to use javascript syntax?

Post edited by: JasonGriffith, at: 10/13/2008 07:59
Reply With Quote
  #2  
Old 10-14-2008, 12:36 AM
quimicefa quimicefa is offline
Mirth Guru
 
Join Date: Dec 2007
Location: Barcelona
Posts: 235
quimicefa is on a distinguished road
Default Re:Delete old files

I suggest you to use the java.io.File and java.io.FileFilter classes.

The most elegant way to solve this is making a jar package that can be called from javascript, and hence, from a JS reader or transformer. You can define the main class to accept the root directory and the days-old thresold.

I would package all the file/filefilter logic in a single jar, and then call it from the JS. It's very hard to debug the mixed JS/Java code ..
Reply With Quote
  #3  
Old 10-08-2009, 11:49 PM
ajay_007 ajay_007 is offline
OBX.1 Kenobi
 
Join Date: Apr 2009
Posts: 26
ajay_007 is on a distinguished road
Default

Quote:
Originally Posted by quimicefa View Post
I suggest you to use the java.io.File and java.io.FileFilter classes.

The most elegant way to solve this is making a jar package that can be called from javascript, and hence, from a JS reader or transformer. You can define the main class to accept the root directory and the days-old thresold.

I would package all the file/filefilter logic in a single jar, and then call it from the JS. It's very hard to debug the mixed JS/Java code ..
Hi try this...
but there is something wrong in javascript ....

if you call delete (newFile.delete()) , javascript throw error .... something needs to be written after . operator. any solutions

var file = new Packages.java.io.File("C:\\Dir");
if(file.exists())
{
if(file.isDirectory())
{
var list = file.list();
for(i=0;i<list.length;i++)
{
var newFile = new Packages.java.io.File(file,list[i]);
FileUtil.write("C:\\abc.txt",true,"\r\n File name = " + newFile.getName() + "\r\n Path = " + newFile.getAbsolutePath().toString() + "\r\n modified " + newFile.lastModified());
var getdate = new Packages.java.util.Date(newFile.lastModified());
FileUtil.write("C:\\abc.txt",true,"\r\n date = " + getdate.toString());
FileUtil.delete("C:\\abc.txt");
//var sucess = newFile.delete();
//if(newFile.delete()
// {
// }


}
FileUtil.write("C:\\abc.txt",true,"\r\n Dir Path " + file.getAbsolutePath().toString());
}
Reply With Quote
  #4  
Old 09-20-2016, 06:06 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Is there a solution for this in javascript?

I'm trying to delete subfolders/files older than x days in a parent folder.

Thanks

Last edited by jg12345; 09-20-2016 at 06:16 AM.
Reply With Quote
  #5  
Old 09-20-2016, 06:25 AM
tiskinty tiskinty is offline
OBX.1 Kenobi
 
Join Date: Jun 2016
Posts: 43
tiskinty is on a distinguished road
Default

It should be possible with something like this. (Untested)

Code:
var dateLimit = new Date(/*today minus x days*/);
var foldercontents = new java.io.File("."); // change "." to be your base directory
for each(file in foldercontents.list()){
    var myFile = new java.io.File(file);
    if(myFile.lastModified() < dateLimit){
        myFile.delete();
    }
}
__________________
Matt

http://theunofficialmirthadmin.blogspot.com/
Reply With Quote
  #6  
Old 09-20-2016, 07:43 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Didn't work - nothing deleted. I'm testing in a Javascript Reader channel.

I also added this, but still no go.
importPackage(java.io);
importClass(java.io.File);
Reply With Quote
  #7  
Old 09-20-2016, 08:26 AM
tiskinty tiskinty is offline
OBX.1 Kenobi
 
Join Date: Jun 2016
Posts: 43
tiskinty is on a distinguished road
Default

You will need to format the date result from myFile.lastModified() to match
__________________
Matt

http://theunofficialmirthadmin.blogspot.com/
Reply With Quote
  #8  
Old 09-20-2016, 09:36 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

Here's an example in MC 3.4.1.8057

File reader looking for all files in a directory. The Source Transformer has javascript to grab the file's last modified date and do a compare to whatever date you choose. Then you just delete the file.
Attached Files
File Type: xml File delete example.xml (86.2 KB, 111 views)
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to delete OBX segments I don't need aht75 Support 14 08-15-2012 11:53 AM
Delete 'done' messages after x days magikbanana Support 3 09-22-2008 12:03 PM
How to delete a segment from message...? faisalamjad Development 1 04-28-2008 12:57 PM
simple transformer to delete OBX 16 cgbullock Support 5 12-05-2007 12:15 PM
Please delete this post jlehew Support 0 10-31-2007 01:45 PM


All times are GMT -8. The time now is 01:51 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation