Ruby FileUtils.mv: Error file not found - ruby

The following piece of code should iterate over a directory list replacing the old directory name with the new one. However, the FileUtils.mv call returns no such file or directory.
I have added the line File.exists? which returns true for all paths passed to it via this loop
Dir["projects/*/*/old"].each{|dir|
Dir.chdir dir
Dir.chdir "../"
puts File.exists?("#{Dir.pwd }/old")
FileUtils.mv "#{Dir.pwd }/old", "#{Dir.pwd }/new_path"
}
Any thoughts would be greatly appreciated.

It seems that you have a folder match as well. Because, File.exists? 'folder' returns true as well.
See if you have any folders with the name old.
# This will list all directories with a name "old"
find . -name old -type d

Related

How can I create a file on my desktop from a Ruby script?

I'm building a webcrawler and I want it to output to a new file that is timestamped. I've completed what I thought would be the more difficult part but I cannot seem to get it to save to the desktop.
Dir.chdir "~/Desktop"
dirname = "scraper_out"
filename = "#{time}"
Dir.mkdir(dirname) unless File.exists?(dirname)
Dir.chdir(dirname)
File.new(filename, "w")
It errors out on the first line
`chdir': No such file or directory # dir_chdir - ~/Desktop
I've read the documentation on FileUtils, File and cannot seem to find where people change into nested directories from the root.
Edit: I don't think FileUtils understands the ~.
~/ is not recognized by Ruby in this context.
Try:
Dir.chdir ENV['HOME']+"/Desktop"
This might help you
Create a file in a specified directory

Ruby - Why does Dir.foreach pass `.` and `..` as the first two parameters?

In an empty directory, the following code
Dir.foreach("./") do |file|
puts file
end
returns
.
..
In my understanding . refers to the working directory and .. refers to the parent directory; why does foreach seem to treat them as files within the working directory?
Because they are files within the working directory. The . and .. directories are not magical; they appear the same way any subdirectory does, as entries in the directory. Every directory on a UNIX-type file system has actual directory entries named . and ... So if you don't want to include them when processing a directory, you need to exclude them yourself.

Ruby FileUtils.mkdir_p is only creating parent directories

I have a controller in Rails, with an action that is meant to create a new directory.
This action should create the directory "/public/graph_templates/aaa/test". However, it leaves off the final directory "test". Why is this only creating parent directories?
def create_temporary_template
dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test")
FileUtils.mkdir_p dir
end
Docs: http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p
Because you use dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test"),
then the dir is "#{Rails.root}/public/graph_templates/aaa".
You could just pass the path to FileUtils.mkdir_p.
def create_temporary_template
dir = "#{Rails.root}/public/graph_templates/aaa/test"
FileUtils.mkdir_p dir
end
The problem is in your use of dirname:
File.dirname("/foo/bar")
# => "/foo"
dirname removes the last entry from the path. Per the documentation:
Returns all components of the filename given in file_name except the last one.
Usually that's the correct thing if your path contains a directory, or directory hierarchy, with the filename:
File.dirname("/foo/bar/baz.txt")
# => "/foo/bar"
But, in this case it's chopping off your desired trailing directory.
I'd recommend taking a look at the Pathname class that is included in Ruby's Standard Library. It wraps File, Dir, FileUtils, FileTest, and probably a Swiss-Army knife and kitchen sink into one class, making it very convenient to work on files and directories with one class.
require 'pathname'
dir = Pathname.new("/foo/bar/baz.txt")
# => "/foo/bar"
dir.mkpath # would create the path
I've found Pathname to be very useful, though it's still young.

Delete hidden files in Ruby

Does anyone know how to delete all files in a directory with Ruby. My script works well when there are no hidden files but when there are (i.e .svn files) I cannot delete them and Ruby raises the Errno::ENOTEMPTY error.
How do I do that ?
If you specifically want to get rid of your svn files, here's a script that will do it without harming the rest of your files:
require 'fileutils'
directories = Dir.glob(File.join('**','.svn'))
directories.each do |dir|
FileUtils.rm_rf dir
end
Just run the script in your base svn directory and that's all there is to it (if you're on windows with the asp.net hack, just change .svn to _svn).
Regardless, look up Dir.glob; it should help you in your quest.
.svn isn't a file, it's a directory.
Check out remove_dir in FileUtils.
It probably has nothing to do with the fact, that .svn is hidden. The error suggest, that you are trying to delete a non empty directory. You need to delete all files within the directory before you can delete the directory.
Yes, you can delete (hiden) directory using FileUtils.remove_dir path.
I happend to just write a script to delete all the .svn file in the directory recursively. Hope it helps.
#!/usr/bin/ruby
require 'fileutils'
def svnC dir
d = Dir.new(dir)
d.each do |f|
next if f.eql?(".") or f.eql?("..")
#if f is directory , call svnC on it
path = dir + "/" + "#{f}"
if File.stat(path).directory?
if f.eql?(".svn")
FileUtils.remove_dir path
else
svnC path
end
end
end
end
svnC FileUtils.pwd
As #evan said you can do
require 'fileutils'
Dir.glob('**/.svn').each {|dir| FileUtils.rm_rf(dir) }
or you can make it a one liner and just execute it from the command line
ruby -e "require 'fileutils'; Dir.glob('**/.svn').each {|dir| FileUtils.rm_rf(dir) }"

What a safe and easy way to delete a dir in Ruby?

I'd like to delete a directory that may or may not contain files or other directories. Looking in the Ruby docs I found Dir.rmdir but it won't delete non-empty dir. Is there a convenience method that let's me do this? Or do I need to write a recursive method to examine everything below the directory?
require 'fileutils'
FileUtils.rm_rf(dir)
A pure Ruby way:
require 'fileutils'
FileUtils.rm_rf("/directory/to/go")
If you need thread safety: (warning, changes working directory)
FileUtils.rm_rf("directory/to/go", :secure=>true)
If some looking for answer on #ferventcoder comment -
Just a note, I found that with Windows and Ruby 1.9.3 (at least) FileUtils.rm_rf will follow links (symlinks in this case) and delete those files and folders as well. This was found based on creating a symlink with CreateSymbolicLinkW and then running FileUtils.rm_rf against a parent directory the symlinks are in. Not exactly expected behavior.
– ferventcoder
Safest way is to iterate path and delete it manually.
def rec_del(path):
if path is file call FileUtils.rm_rf - it is safe to delete even in windows
if dir call Dir.rmdir which will succeed for empty dir and dir symlink. else will get ENOTEMPTY for regular non empty dir.
iterate the dir and call rec_del for each item.
now call again Dir.rmdir which will succeed
The laziest way is:
def delete_all(path)
`rm -rf "#{path}"`
end

Resources