문제
루비로 파일을 옮기고 싶습니다. 어떻게해야하나요?
해결책
FileUtil을 사용하여이를 수행 할 수 있습니다.
#!/usr/bin/env ruby
require 'fileutils'
FileUtils.mv('/tmp/your_file', '/opt/new/location/your_file')
기억하다; 파티션을 가로 질러 이동하는 경우 "MV"는 파일을 새 대상으로 복사하고 소스 경로를 풀지 않습니다.
다른 팁
오래된 질문, 나는 아무도이 간단한 해결책에 대답하지 않았다는 것에 놀랐습니다. FileUtil 또는 SystemCall이 필요하지 않으며 파일의 이름을 새 위치로 바꾸십시오.
File.rename source_path, target_path
행복한 코딩
require "FileUtils"
FileUtils.move 'stuff.rb', '/notexist/lib/ruby'
'FileUtils'모듈을 사용하고 fileUtils.mv를 사용하십시오.
http://www.ruby-doc.org/stdlib-2.0/libdoc/fileutils/rdoc/fileutils.html#method-c-mv
여기 템플릿이 있습니다.
src_dir = "/full_path/to_some/ex_file.txt"
dst_dir = "/full_path/target_dir"
#Use the method below to do the moving
move_src_to_target_dir(src_dir, dst_dir)
def archive_src_to_dst_dir(src_dir, dst_dir)
if File.exist ? (src_dir)
puts "about to move this file: #{src_dir}"
FileUtils.mv(src_dir, dst_dir)
else
puts "can not find source file to move"
end
end
이렇게 파일을 이동할 수 있습니다
Rails.root.join ( 'foo', 'bar')
제휴하지 않습니다 StackOverflow