Tombart
Tombart

Reputation: 32428

Ruby moving directory error:

I have a directory with many subdirectories and each of the subdirectories contains a different version of programme to test.

all_dirs.each do |dir|
  FileUtils.mv(test_spec, target_dir, :verbose => true)
  system("rspec #{test_folder_name}")
  puts "moving back: '#{spec_dir}' to '#{current_dir}'"
  FileUtils.mv(spec_dir, current_dir, :verbose => true)
end

In most cases the code is successful, however sometimes it refuses to move a directory to a parent directory, because it already exists.

mv ~/test/version-1/01-spec ~/test
~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:509:in `block in mv': File exists - ~/test (Errno::EEXIST)
        from ~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in `block in fu_each_src_dest'
        from ~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1437:in `fu_each_src_dest0'
        from ~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1421:in `fu_each_src_dest'
        from ~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:504:in `mv'

The unix mv command is valid and can be executed after this exception was thrown. Am I doing something wrong?

Upvotes: 2

Views: 4349

Answers (1)

pje
pje

Reputation: 22707

As the error tells you, it's failing because ~/test already exists, and FileUtils won't clobber it by default. You can pass the force parameter if you don't care about overwriting:

FileUtils.mv(test_spec, target_dir, :verbose => true, :force => true)

Upvotes: 2

Related Questions