Transfer files containing specific extension in bulk – the easy way

Transfer files containing specific extension!

In this tutorial I will show you how to create your own simple file transferring software and how to transfer files containing specific extension from one folder to another using JAVA.

For this purpose, we don’t need any external libraries – just JAVA 7 or above.

Ok… are you excited? Let’s get started creating our simple console application that will transfer files containing specific extension in bulk from one folder to another.

First of all we need to create our main class. Let’s name it FileMO

[code language=”java”]
public class FileMO {

}
[/code]

Looks good, but are we missing something here?!
Of course we are, Sparky! We need to create our main method inside of the FileMO class! Here is how to do it

[code language=”java”]
public static void main(String[] args) {

}
[/code]

Seems that we are ready for action!
The first thing we want to do is to choose the directory containing the files we would like to grab and move, the target directory and the file extension – since we would like to move only files with specific extension.
Since this is a console application we need to interact with the user and ask him about the directories and the file extension. This can be achieved using a Scanner and here is how to implement it(Don’t forget that this code goes into our main method):

[code language=”java”]
String folder = "";
String target = "";
String extension = "";

Scanner in = new Scanner(System.in); // Declaring the scanner

System.out.print("Please enter the parent directory path: ");
folder = in.nextLine();
Path folderPath = FileSystems.getDefault().getPath(folder);

System.out.println("Please enter target directory path: ");
target = in.nextLine();

System.out.println("Please enter extension(including the dot): ");
extension = in.nextLine();
[/code]

What just happened in the above code? We asked the user to provide us some information and saved it in 3 String variables.

Alright! We collected the needed information and now is time for action! We have to open the parent directory, loop through the files, spot the ones with the specified extension and transfer files containing specific extension. Don’t be scared! We will do it the easy way and here is how:

[code language=”java”]
File dir = new File(folder);
File[] directoryListing = dir.listFiles();
try {
if (directoryListing != null) {
/*Looping trough the files of the directory*/
for (File child : directoryListing) {
/* Checking if the file contains the desired extension – if yes then we proceed with the moving from the one dir to the other*/
if (child.toString().contains(".") && child.toString().substring(child.toString().lastIndexOf(‘.’)).equals(extension)) {
/*Constructing the correct target path*/
Path targetPath = FileSystems.getDefault().getPath(target + "/" + child.getName());
/* Moving the file to the new directory*/
Files.move(child.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("From: " + folderPath.toString() + " To: " + targetPath.toString());
}
}
} else {
/* The path of the directory is invalid*/
System.out.println("No such directory!");
}
} catch (Exception e) {}
[/code]

This is the whole procedure – as you can see, just few lines of code, so CONGRATULATIONS! you have your own “transfer files containing specific extension in bulk from one folder to another” software!

Mind that it’s not the perfect solution, because the files may contain dots in the names, so you just have to place one more check, but I’m leaving it to you!

Here is the complete FileMO class:

[code language=”java”]
package filemo;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;

public class FileMO {

public static void main(String[] args) {

String folder = "";
String target = "";
String extension = "";

Scanner in = new Scanner(System.in);

System.out.print("Please enter the folder path: ");
folder = in.nextLine();
Path folderPath = FileSystems.getDefault().getPath(folder);

System.out.println("Please enter target directory: ");
target = in.nextLine();

System.out.println("Please enter extension(including the dot): ");
extension = in.nextLine();

File dir = new File(folder);
File[] directoryListing = dir.listFiles();

try {
if (directoryListing != null) {
for (File child : directoryListing) {
if (child.toString().contains(".") && child.toString().substring(child.toString().lastIndexOf(‘.’)).equals(extension)) {
Path targetPath = FileSystems.getDefault().getPath(target + "/" + child.getName());
Files.move(child.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("From: " + folderPath.toString() + " To: " + targetPath.toString());
}
}
} else {
System.out.println("No such directory!");
}
} catch (Exception e) {}
}
}
[/code]

No Responses

  1. Charlesnug June 27, 2024
    Your comment is awaiting moderation.
  2. Charlesnug July 3, 2024
    Your comment is awaiting moderation.
  3. Charlesnug July 11, 2024
    Your comment is awaiting moderation.
  4. Charlesnug July 14, 2024
    Your comment is awaiting moderation.
  5. Charlesnug July 22, 2024
    Your comment is awaiting moderation.
  6. Charlesnug July 26, 2024
    Your comment is awaiting moderation.
  7. Charlesnug July 30, 2024
    Your comment is awaiting moderation.
  8. Charlesnug August 2, 2024
    Your comment is awaiting moderation.
  9. Charlesnug August 10, 2024
    Your comment is awaiting moderation.
  10. Charlesnug August 25, 2024
    Your comment is awaiting moderation.
  11. Charlesnug August 28, 2024
    Your comment is awaiting moderation.
  12. Charlesnug August 28, 2024
    Your comment is awaiting moderation.
  13. Charlesnug August 29, 2024
    Your comment is awaiting moderation.
  14. Charlesnug September 4, 2024
    Your comment is awaiting moderation.
  15. Charlesnug September 4, 2024
    Your comment is awaiting moderation.
  16. Charlesnug September 15, 2024
    Your comment is awaiting moderation.
  17. Charlesnug October 1, 2024
    Your comment is awaiting moderation.
  18. Charlesnug October 4, 2024
    Your comment is awaiting moderation.
  19. Charlesnug October 15, 2024
    Your comment is awaiting moderation.
  20. Charlesnug October 18, 2024
    Your comment is awaiting moderation.
  21. Charlesnug November 18, 2024
    Your comment is awaiting moderation.

Leave a Reply