• Post Reply Bookmark Topic Watch Topic
  • New Topic

renaming all files in one dir

 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I still haven't found solution to this problem..
http://www.coderanch.com/t/533908/Streams/java/renaming-all-files-one-dir#2421578

I just ran the class again, it removed three images, have no idea where it put them... :-( they're not in the trash.. this is bad bad bad...:-(

have six folders, with 17 imgs each.. now have start all over again organizing these photos, b/c have no way of knowing which ones it removed.. why does my class it do this... :-(

again, what it does.. when I first put imgs in folder they're called DSC_0007.JPG for example; I rename to "1.jpg" etc.. (I shuffle them first).. then if I want to shuffle photos again, I run class again, it's AT THIS POINT that it removes photos, when I run it a second time.. (actually not when I run it a 2nd time, but when the files already have names like "1.jpg"... b/c I just ran it now and it removed three photos.. but I hadn't ran it in that dir in days...)

I first rename temporarily to "a_.....jpg" etc.. then rename to "1.jpg" etc.. (so it works when photos already have names "1.jpg" etc)

as it says in other thread.. code for entire class is here, http://mayacove.com/dev/java/renr.txt

thank you..

 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed your link, but you might want to paste the code back in here. From what I see, you rename your files to temporary names, and then do nothing. Everything else is commented out. If you uncommented the code that renamed the temp file to the new file name, it still wouldn't work, because that code is in the same loop as the code creating the temporary names.

If files are disappearing, what is happening is name conflicts. You're renaming a file to a file name that already exists. For example, let's say after your shuffle, the first file to rename is 4.jpg. You rename it to 1.jpg. That works, but now what was in 1.jpg is gone. It looks like you made an attempt to use temporary names, but the way it's implemented (if you uncommented some code), you'd rename 4.jpg to a_4.jpg, then immediately rename a_4.jpg to 1.jpg. That gives the same result.

You need to figure out a way to avoid conflicts. Maybe that's by creating a temporary directory, and maybe that's by using temporary names that you are sure won't conflict. I suggest printing out some information before the rename step: old file name, new file name, and a true or false saying if the new file name already exists. That will help you figure out where your conflicts are happening. Make a backup copy of the directory before you run your program, so you can restore it again and again until you get your logic right.
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh my gosh.. I never thought this would be this complicated...
(commented out code was for renaming w/o temp renaming... I have posted again w/o commented out code...
http://mayacove.com/dev/java/renr.txt
clearly showing where temp rename is and then 2nd rename..)

I thought if I have files 1.jpg, 2.jpg, 3.jpg.. etc.. then rename them all first to a_1.jpg, a_2.jpg, a_3.jpg, etc.. that would be fine, b/c then I would rename all of them again (w/o shuffling again) just by taking out the "a_"...

(& yes, I print out a lot of stuff, as you can see in the code.. but: even the names of files that disappear get printed!!! I guess I need to do conditionals as you suggest.. )

funny how w/programming sometimes things that seem simple are complicated and things that seem complicated at first sometimes turn out to be simple.. here unfortunately it's the other way around....:-(

thank you very much for your response...

 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I hate computers too. However, I mostly like programing. What you have to remember is that the program can't understand your intent; it just blindly follows the instructions you give it.

OK, so I'm looking at your reposted code, but I can't immediately tell what's changed. The commented out part is still there, and still commented out. I did notice something else though. The line:



... isn't going to work. The way you've defined it _photosArr * contains java.io.Files. What happens when you concatenate a String ("a_") with an Object? The Objects toString() method is called, and for File that returns the whole file path, not just the file name. So you start with something like "c:/Documents and Settings/users/maya/My Pictures/1.jpg" and create a temporary name "a_c:/Documents and Settings/users/maya/My Pictures/1.jpg". I'm not even sure what will happen when you try to rename a file to that. Try:



instead.

So what you should be targeting is an algorithm like this:

1. Rename all files to temp names, like a_1.jpg, a_2.jpg, etc.
2. Then shuffle them
3. In another loop rename the temp files to new file names. E.g., a_4.jpg -> 1.jpg, a_1.jpg -> 2.jpg, etc.

It might help you to create a renameFile(...) method that does the actual renaming, but checks first for a name conflicts and throws an exception if there is one, rather than wiping out the existing file.

* A couple of style notes: don't use _, avoid using Array as part of a variable name, and never abbreviate.
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thank you for yr help.. following yr algorithm idea:



get error: "incompatible types" on this line:



but why, when further up I have:



thank you very much...




 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that one's easy. tempNewFile is a File, while "a_" + currentPhotoName is a String. You can't assign a String to a File.
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh man.... how come this line in the other one wasn't throwing any errors?





here, http://download.oracle.com/javase/1,5.0/docs/api/java/io/File.html under "constructor summary", it looks like you can pass a string to new File() constructor.....

thank you..

 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that's two different things.

1. Assign a String to a File variable. BAD
2. Instantiate a File object using a String parameter to the constructor, then assign the object to a File variable. GOOD

Do you see the difference?
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

oh brother.. yeah, that was a stupid mistake.. I was tired... of course this line



needed to be :



ok, still need to "refine" things (do the method you say, put "success" cond for 2nd rename...) but will do those things later...

finally got it to work so at least doesn't it remove files.. followed yr algorithm suggestion..

HOWEVER: even though you can clearly see in system.out printouts that files DO get shuffled, even once they've been renamed to "1.jpg" etc.. they STILL don't get shuffled/renamed (???) if I run class once they're called "1.jpg" etc..
I don't get this... (I look at dir window with thumbnails when am running class.. clearly once files are called "1.jpg" etc they don't get shuffled, for some reason..)
(I need to run the class a 2nd time sometimes b/c I have removed an image or something.. if files are not named from 1 to whatever in sequence the program (my photosite, www.francesdelrio.com/photoblog/ I'm adding a new section, that will after last section, "Chile, July 2008") breaks

here is all the code as it stands now:

http://mayacove.com/dev/java/renr.txt

thank you very much...



 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's weird, but I still don't see the changes we discussed reflected at that link. Lucky for you that I'm more interested in your problem than what I'm supposed to be working on today. Try this:



I hard-coded the directory and didn't put in your file filter, but I'm sure you can make those mods.

Did you take all the pictures on that blog? Very nice! I'm also interested in photography, so I'll check it out from time to time.
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg,

thank you so much for all yr help..I really appreciate it..

you can't see my new code? that's so weird.. here's a screenshot:
http://mayacove.com/dev/java/renr.jpg

just got back from a meeting.. can't look @ this now.. but wanted to see if you can see the code in the screenshot.. (what do mean by you can see the code? you can't see the url at all?)

thank you for the comments about the photos... yes I took all the photos... I have been an avid amateur photogr for about 30 years..

thank you very much.. I will be working on this further over the weekend..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!