Pages

Showing posts with label rename. Show all posts
Showing posts with label rename. Show all posts

Thursday, June 25, 2009

Move/Rename multiple file from the remote machine via FTP

I recently being assigned a task to FTP to a remote machine and move the files from a location to another location of the remote machine. Looking at the basic of FTP command, it seems like the only way that I can use is the "Rename" command. However, if I work it out directly via the FTP screen, I can only rename a file at a time. But now the issue is, how do I rename multiple file to a location? After googling for more that an hour, finally I got the solution and I feel that I should blog about it since there isn't much people who know about the solution.

Based on my solution, there are multiple way to do and the following are the workable solution:

  1. FTP (mget) those file from the remote machine to the local directory and ftp (mput) back those files to the new remote directory. Well, not a bad suggestion but it's tedious. Another withdraw way is once I have moved those file to the local directory, I will need to "DELETE" those in the remote directory,JUST TO AVOID DUPLICATION! And again, sounds nice but risky.
  2. Write a java program to do the job. I believe some libraries like com.jcraft.jsch which is an open source java code that able to do the workaround.
  3. Write a funky perl script but I guess not everyone knows about Perl Scripts and I am the first who come to the line.
  4. The last solution is what I am going to show below. Sounds tedious but it eventually work. A simple scripts will do all the job!
How does the scripts work is first I will retrieve all the filename from the remote server and place it to a dummy file. For example: filename.txt. If you look at the FTP basic command, there is a command called "mls" which you can list contents of multiple remote directories. For details, you can try FTP to the remote machine and type in the command of ?mls. For example: ftp>?mls

Well, this is what I used to put the listing directory the the local output file. However, if you wish to list out any specific file in the particular directory, you can always use the "*" wildcard to do the listing. The example will be shown later. Once I have listed out all the filename that I needed in the remote directory, I will use the "mls" to write to an output file to my local directory. The command will be something similar as the following: ftp> mls home/kaynny/*.dat /temp/kaynny/logs/filenameList.txt


Meaning that all files which match with the extension of *.dat will write to the filenameList.txt file in my local machine.
Ok, now I already have the list of the file name and I am ready to rename the file. Here is the trick, read the filenameList.txt line by line using the while loop and place the filename which is in the content in the loop so that the rename command will be executed. But of course, you need to FTP to the remote machine in order to execute the rename command. The following is the example of reading the filenameList.txt once I have FTP to the remote machine:



As you can see from the above portion of code, I actually read the file with the number of lines in the filenameList.txt and place the number of line with N. Then loop according to the number of N with the rename command. Once the rename has completed, I will remove the filenameList.txt so that when the next time the scripts is called, it will not append the new filename to the same file. This is also to avoid duplication when reading the file. The full scripts will look something as the following:



Although this is not the best solution, however I hope this help for some of you who have doubt on how to move file from the remote server to any location on the remoter server via the FTP method.


-Kay Nny-