0

I am trying to copy folders and files which is working fine but I need help on how to filter a single folder and copy the rest of the folders. For example, I have directories like carsfolder and truckfolder in(C:\vehicle\carsfolder and C:\vehicle\truckfolder). When I use the below code it copies both carsfolder and truckfolder but I wanted to copy only carsfolder. How can I do that. Your help is highly appreciated.(Using Swing and Java 1.6)

 class CopyTask extends SwingWorker<Void, Integer>
                 {
                   private File source;
                   private File target;
                   private long totalBytes = 0;
                   private long copiedBytes = 0;
                   public CopyTask(File src, File dest)
                 {
                    this.source = src;
                    this.target = dest;
                    progressAll.setValue(0);

                  }
         @Override
         public Void doInBackground() throws Exception
                 {

                    ta.append("Retrieving info ... ");  //append to TextArea
                    retrieveTotalBytes(source);
                    ta.append("Done!\n");
                    copyFiles(source, target);
                    return null;
                 }
         @Override
       public void process(List<Integer> chunks)
              {
                for(int i : chunks)
              {
                         }
        }
         @Override
         public void done()
                    {
                      setProgress(100);
                      }
        private void retrieveTotalBytes(File sourceFile)
                    {
                    try
                    {
                      File[] files = sourceFile.listFiles();
                      for(File file : files)
                    {
                     if(file.isDirectory()) retrieveTotalBytes(file);
                     else totalBytes += file.length();
                     }
                  }
                 catch(Exception ee)
                   {
                                            }
               }
    private void copyFiles(File sourceFile, File targetFile) throws IOException
                   {
                     if(sourceFile.isDirectory())
                   {
         try{
                if(!targetFile.exists()) targetFile.mkdirs();
                String[] filePaths = sourceFile.list();
                for(String filePath : filePaths)
               {
                File srcFile = new File(sourceFile, filePath);
                File destFile = new File(targetFile, filePath);
                copyFiles(srcFile, destFile);
               }
          }
        catch(Exception ie)
              {
                                   }
         }
    else
             {
                try
                     {
                        ta.append("Copying " + sourceFile.getAbsolutePath() + " to " + targetFile.getAbsolutePath() );
                        bis = new BufferedInputStream(new FileInputStream(sourceFile));
                        bos = new BufferedOutputStream(new FileOutputStream(targetFile));
                        long fileBytes = sourceFile.length();
                        long soFar = 0;
                        int theByte;
             while((theByte = bis.read()) != -1)
                       {
                         bos.write(theByte);
                         setProgress((int) (copiedBytes++ * 100 / totalBytes));
                         publish((int) (soFar++ * 100 / fileBytes));
                       }
                          bis.close();
                          bos.close();
                          publish(100);
                          ta.append(" Done!\n");
                      }
           catch(Exception excep)
                            {
                              setProgress(0);
                              bos.flush();
                              bis.close();
                              bos.close();

                            }
          finally{
                    try {
                          bos.flush();
                        } 
                    catch (Exception e) {
                         }
                    try {
                            bis.close();
                        }
                    catch (Exception e) {
                        }
                   try {
                       bos.close();
                       } 
                   catch (Exception e) {
                    }
                }
}
}
}
user1815823
  • 617
  • 2
  • 8
  • 14
  • You can make use of [`FilenameFilter`](http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html). See this post - http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter – Rohit Jain Jan 30 '13 at 20:25

1 Answers1

1

Maybe you can introduce a regex or list of regexes that specify which files and dirs to exclude?

For example, to exclude truckfolder, use a "exclusion" regex like "C:\\vehicle\\truckfolder.*".

Then, in your code, before you copy anything, check to make sure the absolute path of the sourcefile doesn't match the exclusion regex(s).

Upgradingdave
  • 12,916
  • 10
  • 62
  • 72