aspose file tools*
The moose likes I/O and Streams and the fly likes renaming all files in one dir Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "renaming all files in one dir" Watch "renaming all files in one dir" New topic
Author

renaming all files in one dir

maya brown
Ranch Hand

Joined: Feb 18, 2009
Posts: 42

hi,

I have written a little class to rename all files in a dir, but with the added feature that files are "shuffled" before they're renamed...


(entire class code is here, http://mayacove.com/dev/java/renr.txt)


sometimes, if I want to shuffle files again, I run the class again, but what happens is the 2nd time (and any subsequent) times I run it, that files disappear.. every time I run the class, starting w/the 2nd time, it removes files... why is this... how can it remove files when I don't have any code in there to remove files...

thank you..


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

That's odd indeed. Files should definitely not be removed.

On a side note, your code can probably be made a little more efficient:
File.listFiles returns a File[] so no need to create new File objects using only the file names. Arrays.asList transforms an array into a List; you can't add to or remove from that List but you can still set elements. So Collections.shuffle works for it, and it modifies the backing array.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 247

Files would "appear" to be deleted if they were renamed to the current working directory if that happens to be different than the directory originally containing the file.


Sent from my IBM 360 mainframe
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

That's a good one. They may be moved instead; the name "renameTo" is not completely accurate, since it's more powerful than just renaming. I even used it to move files to network shares.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

It's kind of a weird program anyway. Here's why: suppose you have files named 1.jpg, 2.jpg, 3.jpg, and 4.jpg. Then it appears that the program tries to shuffle them and rename them randomly. But even if it did use the correct directory, here's what would happen.

Let's suppose you shuffled them so you had 4.jpg, 2.jpg, 1.jpg, and 3.jpg. Then renaming them would go like this:
  • Rename 4.jpg to 1.jpg... can't be done because 1.jpg already exists.
  • Rename 2.jpg to 2.jpg... can't be done because 2.jpg already exists.
  • Rename 1.jpg to 3.jpg... can't be done because 3.jpg already exists.
  • Rename 3.jpg to 4.jpg... can't be done because 4.jpg already exists.
  • maya brown
    Ranch Hand

    Joined: Feb 18, 2009
    Posts: 42
    hmmm.. interesting...

    I think I did try some time ago to rename all files to a name starting with '0' or something then renaming them again.. I think it didn't work.. will try something like that again.. any other suggestions? thank you..

    maya brown
    Ranch Hand

    Joined: Feb 18, 2009
    Posts: 42

    ok, I rewrote the program so it renames all files at first temporarily.. "a_1.jpg" etc.. it still removes files when I run it a second time...

    relevant code:



    why does it do this??? again, entire code is here..
    http://mayacove.com/dev/java/renr.txt

    thank you..


    maya brown
    Ranch Hand

    Joined: Feb 18, 2009
    Posts: 42
    Carey Brown wrote:Files would "appear" to be deleted if they were renamed to the current working directory if that happens to be different than the directory originally containing the file.


    I just say 'renameTo' nothing about where to put the files.. they're supposed to stay in same dir.. why would it 'move' half the files when I run the prog a 2nd time??

    maya brown
    Ranch Hand

    Joined: Feb 18, 2009
    Posts: 42
    Rob Spoor wrote:That's a good one. They may be moved instead; the name "renameTo" is not completely accurate, since it's more powerful than just renaming. I even used it to move files to network shares.


    what? "the name 'renameTo' is not completely accurate"??? meaning what??? renameTo the method for renaming files.... what am I missing???

    I adapted from this example
    http://www.zanthan.com/itymbi/archives/000805.html

    at any rate, I changed this line

    to


     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: renaming all files in one dir