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]

One Response

  1. human body June 19, 2015

Leave a Reply