Rename a file: Difference between revisions

Content added Content deleted
m (→‎{{header|Ruby}}: add core rename method to solution)
Line 275: Line 275:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>File.rename('input.txt', 'output.txt')
It uses a hash to store the source and destination, so it's easy to add more dirs/files to move and you can easily switch to using another method to move them.
File.rename('/input.txt', '/output.txt')
File.rename('docs', 'mydocs')
File.rename('/docs', '/mydocs')</lang>


{{libheader|fileutils.rb}}
<lang ruby> #!/usr/bin/env ruby
The <tt>FileUtils#move</tt> method has some more flexibility than the core <tt>File#rename</tt> method (not really demonstrated here).
require 'FileUtils'

<lang ruby> require 'fileutils'
moves = { "input.txt" => "output.txt", "/input.txt" => "/output.txt", "docs" => "mydocs","/docs" => "/mydocs"}
moves = { "input.txt" => "output.txt", "/input.txt" => "/output.txt", "docs" => "mydocs","/docs" => "/mydocs"}
moves.each{ |src, dest| FileUtils.move( src, dest, :verbose => true ) }</lang>
moves.each{ |src, dest| FileUtils.move( src, dest, :verbose => true ) }</lang>