newbie question: loop through directory and change file names

Discussion in 'Java' started by lawrence, Nov 20, 2004.

  1. lawrence

    lawrence Guest

    This is my first attempt to write a java program. I want to write a
    litttle macro that will show up as an icon. When I put in a directory
    and double click it, I want it to launch and loop through every file
    in the directory and change the names of the files. Specifically, I
    want it to find open ' ' white spaces and change them to an
    underscore: _

    I'm doing this because I've several hundred images I'd like to upload
    to the web, but they are on a Windows machine and they have spaces in
    their name.

    Looking around online I found examples of how to open a directory, get
    a list of the files, and replace the white spaces with underscores,
    but I don't know how to actually replace the old name with the new
    name.

    Any advice?




    /*
    * changeFileNames.java
    *
    * Created on November 20, 2004
    */

    /**
    *
    * @author Lawrence
    */
    public class changeFileNames {


    public changeFileNames {

    }



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    File dir = new File("directoryName");

    String[] children = dir.list();
    if (children == null) {
    // Either dir does not exist or is not a directory
    } else {
    for (int i=0; i<children.length; i++) {
    // Get filename of file or directory
    String filename = children;
    // Convert to lower case
    String newFileName = filename.toLowerCase();
    newFinalName = newFileName.replace(' ', '_');
    }
    }

    }

    }
     
    lawrence, Nov 20, 2004
    #1
    1. Advertising

  2. lawrence wrote:

    > This is my first attempt to write a java program. I want to write a
    > litttle macro that will show up as an icon. When I put in a directory
    > and double click it, I want it to launch and loop through every file
    > in the directory and change the names of the files. Specifically, I
    > want it to find open ' ' white spaces and change them to an
    > underscore: _
    >
    > I'm doing this because I've several hundred images I'd like to upload
    > to the web, but they are on a Windows machine and they have spaces in
    > their name.
    >
    > Looking around online I found examples of how to open a directory, get
    > a list of the files, and replace the white spaces with underscores,
    > but I don't know how to actually replace the old name with the new
    > name.
    >
    > Any advice?


    You probably want to look at the File.renameTo(File) function. You can
    create a File object for the file you want to rename, create a new file
    object with the new name, and call renameTo to rename the file.

    Hope this helps.

    --
    Neil Campbell
    batneil[AT]thebatcave[DOT]org[DOT]uk
    http://www.thebatcave.org.uk
     
    Neil Campbell, Nov 20, 2004
    #2
    1. Advertising

  3. Andrew Thompson, Nov 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmFzIEhlbmRyaWtz?=

    Directory and files names in temporary asp.net directory

    =?Utf-8?B?QmFzIEhlbmRyaWtz?=, Jan 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    872
    Juan T. Llibre
    Jan 31, 2005
  2. fBechmann
    Replies:
    0
    Views:
    414
    fBechmann
    Jun 10, 2004
  3. Julian Yap
    Replies:
    4
    Views:
    491
  4. Ajay Garg
    Replies:
    5
    Views:
    221
  5. Isaac Won
    Replies:
    9
    Views:
    420
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page