2 Replies Latest reply on Mar 1, 2016 3:37 AM by funsekon

    aFile.remove() not working as expected

    funsekon Level 1

      Trying to find out why the code below is not deleting all files from the target folder.

       

      After some test it appears that only graphic files (jpg, png, gif) are being removed, skipping pdf, txt, eps, doc, docx, etc.

       

      Have you seen this happening before? Thanks,

       

      #target framemaker
      
      var aGraphicFiles = [];
      
      //Check for target folder existance, if not, create.
      var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");
      if (!checkFolderExists(myTargetFolder)){ //target folder does not exist
          myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");
          myTargetFolder.create();
      }
      
      //Check if folder DeleteMe is empty
      myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");
      aGraphicFiles = myTargetFolder.getFiles();
      if (aGraphicFiles.length != 0){
          for (i=0; i<aGraphicFiles.length; i++){ //scan array
              var sourceChild = aGraphicFiles[i];
              if (sourceChild instanceof File){
                  sourceChild.remove();
                  }
              if (sourceChild instanceof Folder){
                  alert("I found a folder");
              }
          }
      }
      
      function checkFolderExists(myObj){
          var flagEx = false;
          if (myObj.exists){
              flagEx = true; //alert("myTargetFolder exists");
           } else {
              flagEx = false; //alert("myTargetFolder does NOT exist");
           }
           return flagEx;
      }
      
        • 1. Re: aFile.remove() not working as expected
          Klaus Göbel Level 3

          Hi funsekon,

           

          here an example, that works:

           

          Unfortunately the editor doesn't accept code at the moment, so I have to put it here this way:

          Just some notes to your code:

           

          It's easier to check, if a file exists:

          if (objfile.exists).......

           

          The reason your code doesn't work

           

          you have to define a File Object to delete/remove it. : sourceChild = new File(aGraphicFiles[i])

          sourceChild.remove()

           

          Your Line 3 :

          var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

            should be

          var myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe"); 

           

          BTW: BE CARFUL WITH THIS SCRIPT ---  IT WILL ULTIMATELY DELETE ALL FILES !!!!!!!!!!!!!

           

          var oFolder = new Folder("C:\\test\\deleteme");

           

          var oStartFolder = oFolder.selectDlg ("Select Folder");

           

          if (oStartFolder != null)
          {
          DeleteFilesInFolder(oStartFolder);
          }

          function DeleteFilesInFolder(foFolderName)

          {

          var locError = false;

           

          var DeleteFolder = new Folder(foFolderName)

           

          var foFolderContent = DeleteFolder.getFiles ("");

           

          for (var i = 0; i < foFolderContent.length; i++)
          {
          if (foFolderContent[i]  instanceof File)
          {
          var FileToDelete = new File (foFolderContent[i]);
          FileToDelete.remove();
          if (FileToDelete.error > "")
          {
          locError = true;
          alert("Error deleting File: ") + FileToDelete.fsName;
          }
          }
          }

          }

          • 2. Re: aFile.remove() not working as expected
            funsekon Level 1

            Thanks Klaus. Was not aware of the fact that a new object was required in order to remove the actual files. Your script worked perfectly (thanks for the warning about deleting all files).