Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Files

 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I have a file Structure C:\Files\java. In java I have 3 folders F1, F2, F3 and some files . In each of the folders there are zip files a.zip, b.zip, c.zip. I have to go into each folder and move the file to different place. Ex:C:\Moved.

Initial file Structure C:\Files\java\F1\a.zip, C:\Files\java\F2\b.zip,C:\Files\java\F3\c.zip
After moving: C:\Moved\a.zip, C:\Moved\b.zip, C:\Moved\c.zip

how do I achieve this?

Thanks in Advance.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: look at java.io.File.renameTo().
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I do not want to rename the files, but move them from one place to another.

Thanks
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree Potluri wrote:I do not want to rename the files, but move them from one place to another.

And the difference is?
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "And the difference is?" I am not understanding your question. I want to move files from one directory, where in if the path is the directory then go into the directory until there are no more directories, then take the file and put it in another location.

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree Potluri wrote:What do you mean by "And the difference is?" I am not understanding your question. I want to move files from one directory, where in if the path is the directory then go into the directory until there are no more directories, then take the file and put it in another location.
Take a look at the java.io.File.renameTo() documentation.
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know how many files are there in F1 directory. If there are F11, F12.. inside F1, then it should go to each individual directory and move the file to C:\Moved folder


 
Vib Mator
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make use of list method
it returns an array
so you can always check the length parameter

int num_of_file=new File(<directory path>).list().length ;
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree Potluri wrote: I want to know how many files are there in F1 directory. If there are F11, F12.. inside F1, then it should go to each individual directory and move the file to C:\Moved folder



So, in this particular example you want to "rename" the original file from C:\Files\java\F1\a.zip to C:\Moved\a.zip. That isn't what you coded. Instead you tried to rename the original file from C:\Files\Java\F1\a.zip to C:\Moved, which is going to fail if that directory already exists.
 
Vib Mator
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



<File ref for file_to_be_moved>.renameTo(new File(<dest_folder File ref>,<File ref for file_to_be_moved >.getName()));

is what would be the right thing to do.

 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree Potluri wrote:What do you mean by "And the difference is?" I am not understanding your question.

I mean that conceptually, renaming a file and moving a file are the same thing. In each case you're changing the absolute path of the file. Compare Unix commands - whether you want to move or rename a file you still use the mv command.
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mattew,

There can be more than one file in each directory that I need to move. So first I have to scan and check how many files are present before moving the files.

Thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just check the File documentation. It's all there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic