Announcement

Collapse

Mirth Connect 3.12.0 Released!

Mirth Connect 3.12.0 is now available as an appliance update and on our GitHub page. This release includes database performance improvements, improves visual HL7 representation, message pruning, keystore handling, PDF generation, community contributions, and fixes several security vulnerabilities. This release also contains many improvements to commercial extensions. See the release notes for the list of fixes and updates.

Download | See What's New | Upgrade Guide | Release Notes

For discussion on this release, see this thread.
See more
See less

Delete old files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    Reelete 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 ..

    Comment


    • #3
      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());
      }

      Comment


      • #4
        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, 06:16 AM.

        Comment


        • #5
          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/

          Comment


          • #6
            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);

            Comment


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

              http://theunofficialmirthadmin.blogspot.com/

              Comment


              • #8
                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

                Comment

                Working...
                X