5 Replies Latest reply: Apr 26, 2007 12:29 AM by 807599 RSS

    Delete folders and files

    807599
      After the execution of my batch file, I need to delete the folder temp containing some folders and files.
      My code is
      File deleteFiles=new File("C:\\temp");
      String[] dltFiles=deleteFiles.list();
      for(int s=0;s<dltFiles.length;s++)
      {
           File deleteFiles1=new File("C:\\temp",dltFiles[s]);
           deleteFiles1.delete();
      }
      From my above code only the files are getting deleted and that too before the execution gets over. Please tell me what updations to make in my code.
        • 1. Re: Delete folders and files
          800282
          Try this (untested!) snippet:
          public static void recursiveRemove(File file) {
            if(file.isDirectory()) {
              File[] contents = file.listFiles();
              for(File temp : contents) {
                recursiveRemove(temp);
              }
            }
            System.out.println("Removed "+file+"? "+file.delete());
          }
          • 2. Re: Delete folders and files
            807599
            The code given by you deletes folders and files, but it is doing before the completion of my batch file ps.bat.
            Below is my code
            String path1 = (new StringBuilder()).append("cmd /c start /MIN C:\\temp\\ps.bat ").append(checkExistSub).append(" /wait").toString();
            try
            {
                 Process procapplyxsl = Runtime.getRuntime().exec(path1,null,fold); 
                 procapplyxsl.waitFor();
                 wait(2000L);
            }
            catch(FileNotFoundException e)
            {
                 System.out.println(e.getMessage());     
            }
            catch(Exception e1)
            {
                 System.out.println(e1.getMessage());     
            }
            //the code below calls the method containing the code given by you
            DeleteFiles df = new DeleteFiles();
            df.dltFiles();
            Please tell me what changes to make in my code.
            • 3. Re: Delete folders and files
              800282
              ... you're welcome, b.t.w.
              • 4. Re: Delete folders and files
                807599
                But I need the folders and files to be deleted after the execution of my batch file, but it is doing before now.
                • 5. Re: Delete folders and files
                  807599
                  Thanks for the solution.