Monday, 26 December 2016

Moving Files From One Folder to Another Folder

1)create maveen project
2)import lib files from commons-io2.0.1.jar for FileUtils,FilenameUtils
by downloading from following link
 https://commons.apache.org/proper/commons-io/download_io.cgi
3) make sure destination and source folder path

package JavaWorld;

import java.io.File;
//commons-io2.0.1.jar for FileUtils,FilenameUtils
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;


public class MoveFilesBasedOnExtension {
public static void main(String a[])throws Exception
{
File srcFolder = new File("C:/Users/Vishnuyardini/Desktop/fotos/11-12-16 soulmates to banshankari ashram");
File processedDir = new File("C:/Users/Vishnuyardini/Desktop/fotos/soulmate siritual trip");
File shdir = new File("C:/Users/Vishnuyardini/Desktop/fotos/soulmate siritual trip/shdir");
File afile[];
int j = (afile = srcFolder.listFiles()).length;
for(int i = 0; i < j; i++)
{
File fileEntry = afile[i];
String fileName = fileEntry.getName();
System.out.println(fileName);
//moving specific files to other destination directory
if("jpg".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, processedDir, true);
}
if("txt".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, shdir, true);
}



}
}
}

Output:
before:





after:

No comments:

Post a Comment