How do I get the full path of a copied file?

758 views Asked by At

I’m using Rails 5. How do I get the full path of the destination file after copying a file? I’m currently doing this

FileUtils.cp(file_path, dest_dir)

Unfortunately the result of this call is nil. Is there a call that will tell me the full destination path or something else I can do to get this destination path in a variable?

2

There are 2 answers

3
Mystieke On

FileUtils is a stdlib in Ruby. If I understood correctly, what you're trying to do is get the pathname for the file you copied.

You can try this, with Rails, where dest_dir is a path:

Rails.root.join(dest_dir)
5
Rails Fan On

You can get that with:

filename=File.basename("file/some/where/filename.txt")
 => "filename.txt"

File.join("/dest/dir", filename)
 => "/dest/dir/filename.txt"