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

Since he does not randomize well, why not bring the names of the files already randomized?

I decided to take no time 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 as 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 file renamed * @ param subdir <code> true </ code> if subdirectories' files have to be renamed * <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 as 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 extracted the path * @ return The pathname of a file * / public static String getPath (File f) (String absolutePath f.getAbsolutePath = (); absolutePath.substring return (0, absolutePath.lastIndexOf (file. separator) + 1);) / ** * Returns the extension of a file. * * @ param The file that will have the extension extracted * @ return <code> 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, an example follows 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 ...
>>> 210904720.html renamed 668609286.html
>>> 538981388.PDF renamed 573146098.PDF
>>> 65861820.doc renamed 417987190.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 garbage, right?) Here in the PC in the house of my parents.

Holiday now! : ~

import java.io.File;
import java.util.Random; public class RandomFile (private static Random randomizer = new Random (); 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);
)
)
)

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: