سؤال

وأريد أن نقل ملف مع روبي. كيف أفعل ذلك؟

هل كانت مفيدة؟

المحلول

ويمكنك استخدام fileutils حكومة الوحدة للقيام بذلك.

#!/usr/bin/env ruby

require 'fileutils'

FileUtils.mv('/tmp/your_file', '/opt/new/location/your_file')

تذكر. إذا كنت تتحرك عبر الجدران، "ام" سيتم نسخ الملف إلى وجهة جديدة وإلغاء ارتباط مسار المصدر.

نصائح أخرى

والسؤال القديم، أنا مندهش لا أحد أجاب هذا حل بسيط. لا تحتاج fileutils حكومة الوحدة أو systemcall، مجرد إعادة تسمية الملف إلى الموقع الجديد.

File.rename source_path, target_path

وسعيد الترميز

fileutils حكومة الوحدة. الخطوة

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 ( 'فو'، 'بار')

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top