0

Ok, so there are a lot of files that I need to rename and they are all in the same directory. There are multiple folders in the directory with files in them than need to be renamed too.

What I need to do Is replace any space in the file name with an underscore.

I am a beginner at java, so I might be able to do this by myself if there was just one file and I could specify the file in Java, but in this situation I can't.

For example, if the file name is "file number one" I need to rename it to "file_number_one". I would also like to keep the extension unchanged. There are a bunch of files all with different extensions.

Thank you so much for the help and if there are any questions I will try to answer them to the best of my ability.

EDIT:

Ok, I figured it out, but not in Java. Using these websites here and here I figured it out using windown PowerShell.

Basically open up PowerShell, cd into the directory, and type:

dir -Recurse | Rename-Item –NewName { $_.name –replace “ “,”_” }

This replaces all spaces with an underscore in all files and folders in the directory. Sorry I could not figure it out in Java but this is an alternative that seems to work.

Community
  • 1
  • 1
Kyle2595
  • 193
  • 1
  • 6
  • 15
  • 1
    Why not use a scripting language to do so instead? Much quicker I would think. – Jesus Ramos Jun 25 '13 at 21:29
  • How so? Like I said, I'm a beginner and am not all to familiar with coding and scripting. – Kyle2595 Jun 25 '13 at 21:37
  • If you were working on Windows, AutoIt would do this in a snap. But most any scripting language would do. Do do this you would need to: 1) download the language, 2) go through the tutorial, 3) try to code what you need. I don't know of any substitute. It's pretty much the same for Java. You would want to download the utility library that tieTYT recommends, go through the documentation, and try to figure this out. – Hovercraft Full Of Eels Jun 25 '13 at 21:44
  • Ok, not in Java but this is _almost_ what I am looking for: [HERE](http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/). Scroll down to the part where they talk about PowerShell. The problem is that it will rename everything in the top level of the directory and not the in the rest of the folders. – Kyle2595 Jun 25 '13 at 22:43

2 Answers2

3
  1. If I were you, I'd use FilenameUtils for this. It has a way to get the file name without the extension with a method named getBaseName.
  2. Once you have the base name, just use String.replaceAll to replace spaces with underscores.
  3. Rename the file with the result of that.
  4. To get the list of files to operate on, you can use FileUtils. It has a few methods named listFiles that can be used for that.
Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

http://people.apache.org/~jochen/commons-io/site/apidocs/org/apache/commons/io/DirectoryWalker.html

Implement the rename into String normalizeName( String ) and use:

public class FileRenamer extends DirectoryWalker {

  public List rename(File startDirectory) {
      List results = new LinkedList();
      walk(startDirectory, results);
      return results;
  }

  protected boolean handleDirectory(File directory, int depth, Collection results) {
      return true;
  }

  protected void handleFile(File file, int depth, Collection results) {
    String newName = convert( file.getName() );
    File file2 = new File( file.getParentFile(), newName )
    file.renameTo( file2 )
    results.add(file);
  }
}

new FileRenamer.rename( baseDirectory );
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277