2013-04-17 39 views
-2

我期待寫一個腳本,可以做到以下幾點:Ruby腳本來管理文件

我有一組的正常文件,如「myFile.log」我希望在他們目前的狀態離開。

我想將所有類型爲myFile.log.year-month-day-hour的文件放入gzip壓縮包中,每天有不同的子文件夾。

任何人都可以指出我正確的方向,如何做到這一點?

+1

[whathaveyoutried](http://mattgemmell.com/2008/12/08/what-have-you-試圖/)? – creinig

+0

我剛剛開始這個,只是看着指向正確的方向 – user1694873

回答

1

如果你想做的任何事情,在Ruby文件有趣的,你需要

require 'fileutils' 

採取IRB看看方法,那麼你將不得不使用

1.9.3-p327 :001 > require 'fileutils' 
=> true 
1.9.3-p327 :002 > File.methods.sort - Object.methods 
=> [:absolute_path, :atime, :basename, :binread, :binwrite, :blockdev?, :chardev?, :chmod, :chown, :copy_stream, :ctime, :delete, :directory?, :dirname, :executable?, :executable_real?, :exist?, :exists?, :expand_path, :extname, :file?, :fnmatch, :fnmatch?, :for_fd, :foreach, :ftype, :grpowned?, :identical?, :join, :lchmod, :lchown, :link, :lstat, :mtime, :open, :owned?, :path, :pipe, :pipe?, :popen, :read, :readable?, :readable_real?, :readlines, :readlink, :realdirpath, :realpath, :rename, :select, :setgid?, :setuid?, :size, :size?, :socket?, :split, :stat, :sticky?, :symlink, :symlink?, :sysopen, :truncate, :try_convert, :umask, :unlink, :utime, :world_readable?, :world_writable?, :writable?, :writable_real?, :write, :zero?] 
1.9.3-p327 :007 > FileUtils.methods.sort - Object.methods 
=> [:cd, :chdir, :chmod, :chmod_R, :chown, :chown_R, :cmp, :collect_method, :commands, :compare_file, :compare_stream, :copy, :copy_entry, :copy_file, :copy_stream, :cp, :cp_r, :getwd, :have_option?, :identical?, :install, :link, :ln, :ln_s, :ln_sf, :makedirs, :mkdir, :mkdir_p, :mkpath, :move, :mv, :options, :options_of, :private_module_function, :pwd, :remove, :remove_dir, :remove_entry, :remove_entry_secure, :remove_file, :rm, :rm_f, :rm_r, :rm_rf, :rmdir, :rmtree, :safe_unlink, :symlink, :touch, :uptodate?] 
1.9.3-p327 :013 > Dir.methods.sort - Object.methods 
=> [:[], :chdir, :chroot, :delete, :entries, :exist?, :exists?, :foreach, :getwd, :glob, :home, :mkdir, :open, :pwd, :rmdir, :unlink] 

開始與如Dir.entries('/path'),並使用Ruby的數組方法遍歷列表,使用正則表達式來提取部分名稱,等等。

+0

乾杯尼爾,謝謝 – user1694873