The idea arose from the stereo of my father's car is not there an iPod Shuffle. Then I thought:

Since he does not randomises well, why not bring the file names already randomized?

Quickly decided to do in Java and gave it there:

RandomFiles.java

 / * * RandomFiles.java * Copyright (C) 2009 Vítor Avelino * * This program is free software: you can redistribute it and / or modify * it under the terms of the GNU General Public License * those published by the Free Software Foundation, Either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope That It Will Be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You Should Have received a copy of the GNU General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. * / Import java.io.File; import java.util.Random; public class RandomFile (private static Random randomizer = new Random (); / ** * Randomize the files of a specific directory. * * @ param directory The directory That Will Have ITS renamed file * @ param subdir <code> true </ code> if subdirectories' files renamed * Have to Be <code> false < / code> if it's not Necessary rename subdirectories' files * / public static void randomize (String directory, boolean includeSubdirs) (File dir = new File (directory) if (! dir.isDirectory () | |! dir.exists () ) (System.err.println (directory + "is not a directory or does not exist!") System.exit (-1);) System.out.println ("Starting ..."); random renaming navigateInDirectories (dir , includeSubdirs) System.out.println ("Finished!");) private static void navigateInDirectories (File dir, boolean includeSubdirs) (renamed File = null; randomNumber int = 0; for (File f: dir.listFiles ()) (do (randomNumber randomizer.nextInt = ();) while (randomNumber <= 0) if (! f.isDirectory ()) (renamed = new File (FileUtil.getPath (f) + + randomNumber FileUtil.getExtension (f) ); f.renameTo (renamed) System.out.println (">>> "f.getName + () +" renamed to "+ + randomNumber FileUtil.getExtension (f))) else if (includeSubdirs) (System . out.println ("-" + f.getName () + file.separator); navigateInDirectories (f, includeSubdirs);)))) 

FileUtil.java

 / * * FileUtil.java * Copyright (C) 2009 Vítor Avelino * * This program is free software: you can redistribute it and / or modify * it under the terms of the GNU General Public License * those published by the Free Software Foundation, Either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope That It Will Be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You Should Have received a copy of the GNU General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. * / Import java.io.File; public class FileUtil (/ ** * Returns just the pathname of a file as string. * * @ Param f The file That Will Have the extracted path * @ return The pathname of the file * / public static String getPath (File f) (String AbsolutePath f.getAbsolutePath = (); absolutePath.substring return (0, absolutePath.lastIndexOf (file. separator) + 1);) / ** * Returns the extension of the file. * * @ param The file extension That Will Have the <code> extracted * @ return null </ code> if the extension of the file does not exist, <code> Extension </ code> if the file has one * / public static String GetExtension (File f) (String name = f.getName (); return name.substring (name.lastIndexOf ('.')); )) 

The form is simple to use, following an example below:

 RandomFiles.randomize ("D: \ Test \", false);
 RandomFiles.randomize ("D: \ Test \", true);

For the first call the output is:

Initializing random renaming ...
>>> Arquivo1.PDF renamed 538981388.PDF
>>> Arquivo2.doc renamed 65861820.doc
>>> Arquivo3.html renamed 210904720.html
Finished!

For the second call the output is:

Initializing random renaming ...
>>> Renamed to 210904720.html 668609286.html
>>> 538981388.PDF renamed 573146098.PDF
>>> Renamed 417987190.doc 65861820.doc
- Sub \
>>> Arquivo4.PDF renamed 233716363.PDF
>>> Arquivo5.doc renamed 657621617.doc
>>> Arquivo6.html renamed 118165004.html
Finished!

Outgoing messages are used only to monitor the implementation of the program.

I hope it's useful to someone as it was for me. When you have time I will make a version of Python and / or Shell-Script and also install a decent OS (eg saw the trash, right?) Here on the PC in the house of my parents.

Vacations now! : ~

import java.io.File;
import java.util.Random; public class RandomFile (private static Random randomizer = new Random (); public void randomize (String directory, boolean includeSubdirs) (
File dir = new File (directory);
if (! dir.isDirectory () | |! dir.exists ()) (
System.err.println (directory + "is not a directory or does not exist!");
System.exit (-1);
)

System.out.println ("Initializing random renaming ...");
navigateInDirectories (dir, includeSubdirs);
System.out.println ("Finished!");
)

private static void navigateInDirectories (File dir, boolean includeSubdirs) (
Renamed File = null;
randomNumber int = 0;

for (File f: dir.listFiles ()) (
do (
randomNumber randomizer.nextInt = ();
) While (randomNumber <= 0);

if (! f.isDirectory ()) (
renamed = new File (FileUtil.getPath (f) + + randomNumber FileUtil.getExtension (f));
f.renameTo (renamed);
System.out.println (">>> "f.getName + () +" renamed to "+ + randomNumber FileUtil.getExtension (f));
) Else if (includeSubdirs) (
System.out.println ("-" + f.getName () + file.separator);
navigateInDirectories (f, includeSubdirs);
)
)
)

public static void main (String [] args) (
RandomFiles.randomize ("D: / test /", false);
)

)

Share:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Identi.ca
  • LinkedIn
  • Netvibes
  • Rec6
  • Reddit
  • RSS
  • StumbleUpon
  • Twitter
  • Mixx
  • Technorati

Related articles: