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.
I do not want to rename the files, but move them from one place to another.
Sree Potluri wrote:I do not want to rename the files, but move them from one place to another.
And the difference is?
Take a look at the java.io.File.renameTo() documentation.
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.
it returns an array
so you can always check the length parameter
int num_of_file=new File(<directory path>).list().length ;
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.
<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.
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.
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.