¿Cómo muevo un archivo con Ruby?


Quiero mover un archivo con Ruby. ¿Cómo hago eso?

 157
Author: Costique, 2008-12-31

6 answers

Puede usar FileUtils para hacer esto.

#!/usr/bin/env ruby

require 'fileutils'

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

Recuerde; si se está moviendo a través de particiones, "mv" copiará el archivo a un nuevo destino y desvinculará la ruta de origen.

 232
Author: Berk D. Demir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-25 07:33:30

Una vieja pregunta, me sorprende que nadie haya respondido a esta simple solución. No necesita fileutils o un systemcall, simplemente cambie el nombre del archivo a la nueva ubicación.

File.rename source_path, target_path

Feliz codificación

 84
Author: peter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-04 18:01:14

FileUtils.move

require "FileUtils"
FileUtils.move 'stuff.rb', '/notexist/lib/ruby'
 15
Author: Željko Filipin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-18 22:08:22
 11
Author: Nicolas Martyanoff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-10 15:49:41

Aquí hay una plantilla .

 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
 1
Author: zee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-17 06:22:17

Puedes mover tu archivo así

Rails.arraigar.join ("foo", "bar")

 -6
Author: kajal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-04 09:55:33