Skip to content

Instantly share code, notes, and snippets.

@gunungloli666
Created May 28, 2012 04:23
Show Gist options
  • Save gunungloli666/2817212 to your computer and use it in GitHub Desktop.
Save gunungloli666/2817212 to your computer and use it in GitHub Desktop.
Untuk me-rename file di Windows pake java
package file;
import java.io.File;
public class GetBigFile extends RenameFile{
double maxSize;
public GetBigFile(String source, double maxsize){
super(source);
this.maxSize = maxsize;
}
public void rename(String m){
for(int i=0; i< filelist.length; i++){
File f = filelist[i];
if(!f.isDirectory()){
double size = (double) f.length()/(1024*1024); //size in mb
if(size >= maxSize){
renameFile(f, m);
}
}else{
String path = f.getAbsolutePath().replace("\\", "/");
new GetBigFile(path, maxSize).rename(m);
}
}
}
public static void main(String[] args){
String s = "D:/mamat";
GetBigFile big = new GetBigFile(s, 2);
big.rename("lebih besar dari 2 mb___");
System.out.println("sukses...");
}
}
package file;
import java.io.File;
public class RenameFile{
File file;
File filelist[];
public RenameFile(String folder) {
try {
this.file = new File(folder);
filelist = file.listFiles();
} catch (Exception e) {
System.out.println("salah memasukkan nama folder.. !!");
}
}
public File getFile(){
return this.file;
}
public void rename(String myString) {
for (int i = 0; i < filelist.length; i++) {
File file = filelist[i];
if(!file.isDirectory() ){
this.renameFile(file, myString);
}else{
String path = file.getAbsolutePath().replace("\\", "/");
new RenameFile(path).rename(myString);
}
}
}
protected void renameFile(File f, String myString){
String b = f.getAbsolutePath();
int c = b.lastIndexOf("\\");
String temp = b.substring(0, c);
String temp1 = b.substring(c+1, b.length());
temp = temp.replace("\\", "/").concat("/");
String dest = temp.concat(myString).concat(temp1);
boolean sukses = f.renameTo(new File(dest));
if(!sukses){
System.out.println("gagal: "+ f.getAbsolutePath());
}
}
protected String getRenameString(File f , String myString){
String b = f.getAbsolutePath();
int c = b.lastIndexOf("\\");
String temp = b.substring(0, c);
String temp1 = b.substring(c+1, b.length());
temp = temp.replace("\\", "/").concat("/");
String dest = temp.concat(myString).concat(temp1);
return dest;
}
public static void main(String args[]) {
String source= "D:/mamat";
RenameFile rename = new RenameFile(source);
rename.rename("equal__");
System.out.println("\n FINISH");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment