Skip to content

Instantly share code, notes, and snippets.

@gpwclark
Created May 24, 2016 22:55
Show Gist options
  • Save gpwclark/78b843bdfa1bff0055dac6f296326a99 to your computer and use it in GitHub Desktop.
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).
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