FileUtils

Constants

Win32Exts
LINKING_SUPPORTED
Win32Exts

Public Instance Methods

copy_entryx(src, dest, filter, preserve = false, dereference_root = false, remove_destination = false) click to toggle source

Like FileUtils.copy_entry, but takes a filter proc that can return false to skip a file.

Note that if the filter rejects a subdirectory then everything within that subdirectory is automatically skipped as well.

    # File lib/more/facets/fileutils/cp_rx.rb, line 30
30:   def copy_entryx(src, dest, filter, preserve = false, dereference_root = false, remove_destination = false)
31:           Entry_.new(src, nil, dereference_root).traverse do |ent|
32:                   if filter.call(ent.path) then
33:                           destent = Entry_.new(dest, ent.rel, false)
34:                           File.unlink destent.path if remove_destination && File.file?(destent.path)
35:                           ent.copy destent.path
36:                           ent.copy_metadata(destent.path) if preserve
37:                   end
38:           end
39:   end
cp_rx(src, dest, options = {}, &filter) click to toggle source

Like FileUtils.cp_r, but takes a filter proc that can return false to skip a file.

  cp_rx "bigDirectoryTree", "dest", {:noop => true} do |name|
    /dontCopyThis$/.match(name)
  end

Note that if the filter rejects a subdirectory then everything within that subdirectory is automatically skipped as well.

    # File lib/more/facets/fileutils/cp_rx.rb, line 14
14:   def cp_rx(src, dest, options = {}, &filter)
15:           fu_check_options(options, OPT_TABLE['cp_r'])
16:     if options[:verbose]
17:             fu_output_message("cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}")
18:     end
19:           return if options[:noop]
20:           fu_each_src_dest(src, dest) do |s, d|
21:                   copy_entryx(s, d, filter, options[:preserve], options[:dereference_root], options[:remove_destination])
22:           end
23:   end
head(filename,lines=10) click to toggle source

In block form, yields the first number of ((lines)) of file ((filename)). In non-block form, it returns an array of the first number of ((lines)).

  # Returns first 10 lines of 'myfile'
  FileUtils.head("myfile")
    # File lib/more/facets/fileutils/slice.rb, line 11
11:   def head(filename,lines=10) #:yield:
12:     a = []
13:     IO.foreach(filename){|line|
14:         break if lines <= 0
15:         lines -= 1
16:         if block_given?
17:           yield line
18:         else
19:           a << line
20:         end
21:     }
22:     return a.empty? ? nil : a
23:   end
safe_ln(*args) click to toggle source

Attempt to do a normal file link, but fall back to a copy if the link fails.

CREDIT Jim Weirich

    # File lib/more/facets/fileutils/safe_ln.rb, line 14
14:   def safe_ln(*args)
15:     unless LINKING_SUPPORTED[0]
16:       cp(*args)
17:     else
18:       begin
19:         ln(*args)
20:       rescue Errno::EOPNOTSUPP
21:         LINKING_SUPPORTED[0] = false
22:         cp(*args)
23:       end
24:     end
25:   end
slice(filename,from,to) click to toggle source

In block form, yields lines ((from))-((to)). In non-block form, returns an array of lines ((from))-((to)).

  # Returns lines 8-12 of 'myfile'
  FileUtils.body("myfile",8,12)

CREDIT Shashank Date, via Daniel Berger.

    # File lib/more/facets/fileutils/slice.rb, line 48
48:   def slice(filename,from,to) #:yield:
49:     IO.readlines(filename)[from-1..to-1]
50:   end
tail(filename,lines=10) click to toggle source

In block form, yields the last number of ((lines)) of file ((filename)). In non-block form, it returns the lines as an array.

Note that this method slurps the entire file, so I don’t recommend it for very large files. If you want an advanced form of ((tail)), I suggest using file-tail, by Florian Frank (available on the RAA). And no tail -f.

  # Returns last 3 lines of 'myfile'
  FileUtils.tail("myfile",3)
    # File lib/more/facets/fileutils/slice.rb, line 36
36:   def tail(filename,lines=10) #:yield
37:     IO.readlines(filename).reverse[0..lines-1].reverse
38:   end
wc(filename,option='all') click to toggle source

With no arguments, returns a four element array consisting of the number of bytes, characters, words and lines in filename, respectively.

Valid options are bytes, characters (or just ‘chars’), words and lines.

  # Return the number of words in 'myfile'
  FileUtils.wc("myfile",'words')

CREDIT Daniel J. Berger

    # File lib/more/facets/fileutils/wc.rb, line 16
16:   def wc(filename,option='all')
17:     option.downcase!
18:     valid = %all bytes characters chars lines words/
19: 
20:     unless valid.include?(option)
21:         raise "Invalid option: '#{option}'"
22:     end
23: 
24:     n = 0
25:     if option == 'lines'
26:         IO.foreach(filename){ n += 1 }
27:         return n
28:     elsif option == 'bytes'
29:         File.open(filename){ |f|
30:           f.each_byte{ n += 1 }
31:         }
32:         return n
33:     elsif option == 'characters' || option == 'chars'
34:         File.open(filename){ |f|
35:           while f.getc
36:               n += 1
37:           end
38:         }
39:         return n
40:     elsif option == 'words'
41:         IO.foreach(filename){ |line|
42:           n += line.split.length
43:         }
44:         return n
45:     else
46:         bytes,chars,lines,words = 0,0,0,0
47:         IO.foreach(filename){ |line|
48:           lines += 1
49:           words += line.split.length
50:           chars += line.split('').length
51:         }
52:         File.open(filename){ |f|
53:           while f.getc
54:               bytes += 1
55:           end
56:         }
57:         return [bytes,chars,words,lines]
58:     end
59:   end
whereis(prog, path=ENV['PATH']) click to toggle source

In block form, yields each ((program)) within ((path)). In non-block form, returns an array of each ((program)) within ((path)). Returns (({nil})) if not found.

On the MS Windows platform, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name.

   FileUtils.whereis("ruby") -> ['/usr/local/bin/ruby','/opt/bin/ruby']

CREDIT Daniel J. Berger

    # File lib/more/facets/fileutils/whereis.rb, line 23
23:   def whereis(prog, path=ENV['PATH']) #:yield:
24:     dirs = []
25:     path.split(File::PATH_SEPARATOR).each{|dir|
26:         # Windows checks against specific extensions
27:         if File::ALT_SEPARATOR
28:           if prog.include?('.')
29:               f = File.join(dir,prog)
30:               if File.executable?(f) && !File.directory?(f)
31:                 if block_given?
32:                     yield f.gsub(/\//,'\')
33:                 else
34:                     dirs << f.gsub(/\//,'\')
35:                 end
36:               end
37:           else
38:               Win32Exts.find_all{|ext|
39:                 f = File.join(dir,prog+ext)
40:                 if File.executable?(f) && !File.directory?(f)
41:                     if block_given?
42:                       yield f.gsub(/\//,'\')
43:                     else
44:                       dirs << f.gsub(/\//,'\')
45:                     end
46:                 end
47:               }
48:           end
49:         else
50:           f = File.join(dir,prog)
51:           # Avoid /usr/lib/ruby, for example
52:           if File.executable?(f) && !File.directory?(f)
53:               if block_given?
54:                 yield f
55:               else
56:                 dirs << f
57:               end
58:           end
59:         end
60:     }
61:     dirs.empty? ? nil : dirs
62:   end
which(prog, path=ENV['PATH']) click to toggle source

Looks for the first occurrence of program within path.

On the MS Windows platform, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name. Returns nil if not found.

CREDIT Daniel J. Berger & Michael Granger

    # File lib/more/facets/fileutils/which.rb, line 24
24:   def which(prog, path=ENV['PATH'])
25:     path.split(File::PATH_SEPARATOR).each {|dir|
26:       # Windows checks against specific extensions
27:       if File::ALT_SEPARATOR
28:         ext = Win32Exts.find{|ext|
29:           if prog.include?('.') # Assume extension already included
30:             f = File.join(dir,prog)
31:           else
32:             f = File.join(dir,prog+ext)
33:           end
34:           File.executable?(f) && !File.directory?(f)
35:         }
36:         if ext
37:           # Use backslashes, not forward slashes
38:           if prog.include?('.') # Assume extension already included
39:             f = File.join( dir, prog ).gsub(/\//,'\')
40:           else
41:             f = File.join( dir, prog + ext ).gsub(/\//,'\')
42:           end
43:           return f
44:         end
45:       else
46:         f = File.join(dir,prog)
47:         # Avoid /usr/lib/ruby, for example
48:         if File.executable?(f) && !File.directory?(f)
49:           return File::join( dir, prog )
50:         end
51:       end
52:     }
53:     nil
54:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.