• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need an OO approach for bulk file rename process  RSS feed

 
Dietmar Meise
Greenhorn
Posts: 4
Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I need to rename around 4,000 files on a windows PC.
The files are digital photos called DSC_0001, DSC_0002 and so on and they are located in a directory with around 50 subdirectories.
The rename information (old filename, new filename) will be contained in a text file that I will create from a MS Works database file (which contatins the detail information about the 4,000 photos).

I have been programming in procedural language (REXX) quite a while some time ago and I know that I would have been able to write a program in REXX within 1 hour to do this job.
Just two nested loops: 1st loop: read the text file with the filename information, for every file get the new filename, 2nd loop: find the file to be renamed and rename it.
(Or something like that, it has been quite a while ago since I last programmed in REXX).

I now want to write a OO program in java for this.
I have worked through "Java, head first" 2 years ago but I don't have an idea how to approach this solution in an OO-way.
I suppose I have to read the text file with the old-new filename information into a "collection" and then work through this "list".
I think that "file" is an object that is already defined in java and I assume that I can change the name of these objects.
Though I don't know how to do this.
Which object will do the rename?
Which object will go through the list of filenames and start the rename process?

I know this sound like being an idiot but coming from an procedural programming mindset it is really hard to switch to OO thinking.
Any ideas how to approach?
Any short lecture for me to get an idea?

Thank you in advance
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Welcome to CodeRanch!

First, let me say that not all tasks are 'best created' using full OO concepts. If you wanted a simple task you could just as well put the work into a single main() method with the two loops you described. Not very OO, but pretty quick.

But back to an Object Oriented approach. What I do is first find the 'Things' in the task at hand, and turn them into Objects. In OO speech these usually become the Model. From your description, you have Files, then you have a relationship between old and new file names, which I might call a FilePair. The first thing to do would be to make Objects representing these two things. Luckily Java already has a suitable File type for us, java.io.File, which will serve the purpose nicely, which means all you need to do is create a FilePair class which holds the relationship between the 'old name' and the 'new name.'

Then its on to the work. Here a see a pretty straight forward FileCommand, for a given FilePair, rename the file from one to the other. As a first run, then I would make a RenameFileCommand which takes a FilePair as a parameter and does the work of renaming. I would code the RenameFileCommand in a fairly generic fashion, trying to make the method names and functions re-usable in case I want to add other FileCommands (like delete, move, copy, etc...). If you are careful, you could extract an Interface you can apply to more than one job which would make your work of updating the application later on easier. In OO you might see this called the 'Business Logic'.

Now you have the Data (File and FilePair) and the Work (the RenameFileCommand), so it is time to put it together. I usually call this a Controller. It would be responsible for generating the Model and calling the Business Logic as appropriate. So your controller would be responsible for reading the input text file, using it to generate FilePairs, and for each FilePair calling the RenameFileCommand on it. Once you have it written you can then look at it and try to move the tasks around to different methods, and maybe different classes. The goal is to make it modular so changes in how the FilePairs are made don't affect how the FileCommand gets called, etc...
 
Dietmar Meise
Greenhorn
Posts: 4
Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steve,

thank you very much for this good introduction.

I will try to set it up the OO way. (I am afraid if I put it into a single main() method I will end up in spaghetti code).

I suppose that with Model and Controller you refer to the MVC concept (we only have no View here because it's just simple batch processing).

I have one additional question: the RenameFileCommand, could it be a method of the FilePair class?

So I would create class FilePair with class variables OldName and NewName that can contain strings.
And then perhaps a method RenameFileCommand with the function that you have suggested above.
Hm, let me try this out.
It will perhaps take a little time because I have some more things to do but I will be back with my results.

Thank you again.
Dietmar

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dietmar Meise wrote:Hello Steve,

thank you very much for this good introduction.

I will try to set it up the OO way. (I am afraid if I put it into a single main() method I will end up in spaghetti code).

I suppose that with Model and Controller you refer to the MVC concept (we only have no View here because it's just simple batch processing).

I have one additional question: the RenameFileCommand, could it be a method of the FilePair class?


Correct, this separation of concerns is the basis for MVC design patterns.

I would suggest not making the RenameFileCommand a method in the FilePair class, but instead a class itself which has a method which takes a FilePair and does work. This approach separates the 'Data' in the Model from the 'Work' and allows you to plug in different Commands later on. Basically, you want to keep things as isolated and small as you can to make sense. In this case, the FilePair just carries data (and would be called a 'Data Transfer Object' or DTO) and the RenameFileCommand would be a very simple class surrounding a single method which would help you implement the 'Command' pattern.

So I would create class FilePair with class variables OldName and NewName that can contain strings.
And then perhaps a method RenameFileCommand with the function that you have suggested above.
Hm, let me try this out.
It will perhaps take a little time because I have some more things to do but I will be back with my results.

Thank you again.
Dietmar


Best of luck. Remember, this is all just suggestions and things to 'think about' when you make the design, there are tons of different ways to get from here to there.
 
Dietmar Meise
Greenhorn
Posts: 4
Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

finally good old procedural-Dinosaur decided to leave OO aside for a while and put it all into one dirty big (batch) class (Thank you for that tip, Steve).

Though this one nearly drove me crazy yesterday evening:

E:\>E:\bin\javac BigRenameJob.java
BigRenameJob.java:22: not a statement
File subd = new File(BASE_DIR2 + "\\" + a[1]);
^
BigRenameJob.java:22: ';' expected
File subd = new File(BASE_DIR2 + "\\" + a[1]);
.....^
2 errors



The according code is:



a[1] just contains the name of the subdirectory with certain photos.
line 21 is: if ( i > 100 )
line 22 is: File subd = new File(BASE_DIR + "\\" + a[1]);

In despair I used (even more dirty) BASE_DIR = BASE_DIR2 (which to my surprise seems to work).
Any idea what is wrong with the if-clause?
Thank you.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dietmar Meise wrote:

That's your entire if-clause. The body of an if-clause must be a statement or block. This is a declaration. I think you forgot to add some curly braces {} to create a block.
 
Dietmar Meise
Greenhorn
Posts: 4
Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,

thank you for the explanation.

Though when trying this one


I get

E:\>E:\bin\javac BigRenameJob.java
BigRenameJob.java:23: subd is already defined in main(java.lang.String[])
File subd = new File(BASE_DIR2 + "\\" + a[1]);}
.....^
1 error


And when I try this one


I get this error:

E:\>E:\bin\javac BigRenameJob.java
BigRenameJob.java:23: incompatible types
found : java.lang.String
required: java.io.File
subd = BASE_DIR2 + "\\" + a[1];}
.......................................^
1 error


I though a[1] is a String?

Trying


yields:

E:\>E:\bin\javac BigRenameJob.java
BigRenameJob.java:23: cannot find symbol
symbol : method File(java.lang.String)
location: class BigRenameJob
subd = File(BASE_DIR2 + "\\" + a[1]);}



O.K. After trying all kind of combinations I think I found it:



This compiles and works correctly.

Thank you for your help.
Dietmar
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!