This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to Copy All files with certain extensions from a directory and all its subs

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to copy a file from one directory to another, what i would like help on is copy a file with .jpg or .doc extension.

So for example.

Copy all files from D:/Pictures/Holidays

Scanning all folders in the above path and transfer all jpg's to a destination provided.

Any help would be appreciated.



Thanks
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out java.io.FilenameFilter Both java.io.File.list() and java.io.File.listFiles() are overloaded to take a FilenameFilter to filter file listings. You should also be careful to handle name collisions. That is, if you have picture.jpg and something with that name already exists in the destination directory, what do you want to do? Overwrite it? Skip it? Rename one or both of them? You'll want to test that carefully before letting it loose on your real image files.
 
Ranch Hand
Posts: 54
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope the below code will be usefull,

class AcceptFile implements FilenameFilter{

String pattern;

AcceptFile(String pattern){
this.pattern=pattern;
}

public boolean accept(File file,String name ){
if(){
name.endsWith(pattern);
return true;
}
else return false;
}

}

class FetchFiles{

String path = "D:/Pictures/Holidays";
String[] files;
File search = File(path);

files = search.list(AcceptFile(".jpg"));

}


This will fetch all the files with extension .jpg from D:/Pictures/Holidays. Here i have user FilenameFilter interface and have implemeted its accept method so that it accepts only those file of provided extension.
 
Yasin Kothia
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response guys, it really helped.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even simpler
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic