Created
May 24, 2016 22:55
-
-
Save gpwclark/78b843bdfa1bff0055dac6f296326a99 to your computer and use it in GitHub Desktop.
This is a java program that takes a filepath (to a folder... arg1) and randomly renames the files in the folder with a UUID, each randomly renamed file is given the same extension (arg2).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.io.File; | |
import java.util.UUID; | |
/* | |
* This is a java program that takes a filepath (to a folder... arg1) | |
* and randomly renames the files in the folder with a UUID, each | |
* randomly renamed file is given the same extension (arg2). | |
* | |
* 1st argument is "/the/filepath" | |
* 2nd argument is ".fileextension" | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
String filepath = args[0]; | |
String extension = args[1]; | |
File sourceFolder = new File(filepath); | |
File[] fileHandles = getFileHandlesInFolder(sourceFolder); | |
int numFiles = getNumFilesInFolder(sourceFolder); | |
String[] newFileNames = generateNRandomNames(numFiles); | |
for (int i = 0; i < numFiles; i++){ | |
fileHandles[i].renameTo(new File(filepath + newFileNames[i] + extension)); | |
} | |
} | |
public static File[] getFileHandlesInFolder(File sourceFolder){ | |
return sourceFolder.listFiles(); | |
} | |
public static int getNumFilesInFolder(File sourceFolder){ | |
return sourceFolder.listFiles().length; | |
} | |
public static String[] generateNRandomNames(int numNames){ | |
String[] namesArray = new String[numNames]; | |
for (int i = 0; i < numNames; i++){ | |
namesArray[i] = generateRandomName(); | |
} | |
return namesArray; | |
} | |
public static String generateRandomName(){ | |
return UUID.randomUUID().toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment