Move entire directory and its contents to another location on the same server using Net::FTP in Ruby

99封情书 提交于 2020-01-02 23:58:22

问题


As stated in the title, I would like to move a directory on a FTP Server to some other path on the same server. I want to accomplish this using Net::FTP but other solutions are also welcome.

Since there's no proper method for moving files or directories in Net::FTP Documentation, a solution involving copying the directory to another path and deleting the original would be preferable.

Please stay on topic and leave solutions related to the question.


回答1:


Well, I found the solution and it's quite simple. Files (& Directories) can be moved using the rename() method of the Net::FTP Class. Example:

ftp = Net::FTP.new("ftp.myserver.com","myusername","mypassword")
ftp.binary = true
ftp.passive = true

path1 = "/original/dir/path/"    # Dir to move
path2 = "/new/path/"             # New path of Dir

ftp.rename(path1, path2)

And that's it! This causes all files to move from one path to another on the same FTP Server.



来源:https://stackoverflow.com/questions/17350846/move-entire-directory-and-its-contents-to-another-location-on-the-same-server-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!