Skip to content

Instantly share code, notes, and snippets.

@dineshr93
Last active January 23, 2018 04:16
Show Gist options
  • Save dineshr93/aca14e903e20db53ef651557e3f784a1 to your computer and use it in GitHub Desktop.
Save dineshr93/aca14e903e20db53ef651557e3f784a1 to your computer and use it in GitHub Desktop.
package com.so.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class Sof {
public static void main(String[] args) throws IOException {
String matchword = "POPCORN.";//PA__OPCON.MES.
List<File> files = getDirs(new File(path), 0); //level 0 is inside parent , level 1, and so on
System.out.println(files);
String[] paths = new String[files.size()];
int i = 0;
for (File file : files) {
paths[i++] = file.getAbsolutePath();
}
//File f = null;
HashMap<String, String > old_new = new HashMap<>();
for (int j = 0; j < paths.length; j++) {
System.out.println(paths[j]);
String old_path = paths[j];
if(old_path.contains(matchword)){
paths[j] =paths[j].replaceAll(matchword, ""); /*replace with desired rename*/
old_new.put(old_path, paths[j]);
}else{
System.out.println("skipping->"+old_path);
}
//f = new File(paths[j]);
//f.mkdirs();
}
for(String key : old_new.keySet()){
//FileUtils.copyDirectory(new File(key), new File(old_new.get(key)));
moveDirectory(new File(key), new File(old_new.get(key)));
}
//FileUtils.copyDirectory(new File(old_new.get), new File(arg0));
}
static List<File> getDirs(File parent, int level){
List<File> dirs = new ArrayList<File>(); //to store
for(File f: parent.listFiles()){
if(f.isDirectory()) {
if (level==0) dirs.add(f);
else
if (level > 0) dirs.addAll(getDirs(f,level-1)); //recursion
}
}
return dirs;
}
public static void moveDirectory(File srcDir, File destDir) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (!srcDir.exists()) {
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
}
if (!srcDir.isDirectory()) {
throw new IOException("Source '" + srcDir + "' is not a directory");
}
boolean rename = srcDir.renameTo(destDir);
if (!rename) {
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
throw new IOException("Cannot move directory: "+srcDir+" to a subdirectory of itself: "+destDir);
}
FileUtils.copyDirectory( srcDir, destDir );
FileUtils.deleteDirectory( srcDir );
if (srcDir.exists()) {
throw new IOException("Failed to delete original directory '" + srcDir +
"' after copy to '" + destDir + "'");
}
}
}
}
package com.so.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class moveDirectoryinproj {
public static void main(String[] args) throws IOException {
File src = new File("D:\\root\\choc");
File dest = new File("D:\\root\\candy");
moveDirectory(src, dest);
}
public static void moveDirectory(File srcDir, File destDir) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (!srcDir.exists()) {
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
}
if (!srcDir.isDirectory()) {
throw new IOException("Source '" + srcDir + "' is not a directory");
}
boolean rename = srcDir.renameTo(destDir);
if (!rename) {
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
throw new IOException("Cannot move directory: "+srcDir+" to a subdirectory of itself: "+destDir);
}
FileUtils.copyDirectory( srcDir, destDir );
FileUtils.deleteDirectory( srcDir );
if (srcDir.exists()) {
throw new IOException("Failed to delete original directory '" + srcDir +
"' after copy to '" + destDir + "'");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment