ruby文件操作大全

上一篇 / 下一篇  2010-12-21 13:42:18 / 个人分类:自动化测试

ruby文件操作大全

1.创建文件夹
Dir.new %%1
Dir::mkdir #
不指定目录全名称时,缺省为工作目录
Dir::chdir()
改变当前脚本工作目录
FileUtils.mkdir 'test'
file = File.new("cmd.txt")
file.each do |line|
puts line if line =~ /target/
end

2.
创建文件
改变当前根目录

Dir.chdir("/home/guy/sandbox/tmp")
 
 
Dir.chroot("/home/guy/sandbox")
Dir.new %%1
#Dir::rmdir #
不指定目录全名称时,缺省为工作目录

3.
删除文件
改变当前根目录
Dir.chdir("/home/guy/sandbox/tmp")
 
Dir.chroot("/home/guy/sandbox")
Dir.new %%1
#Dir::rmdir #
不指定目录全名称时,缺省为工作目录

4.
删除文件夹
#require 'fileutils'
 
FileUtils.rm_r(%%1)

5.
删除一个文件下夹所有的文件夹
Dir::chdir
Dir::pwd
属性或者Dir.getwd()

#
改变当前脚本工作目录

6.
清空文件夹
Dir::chdir %%1
改变当前脚本工作目录
Dir::rmdir #
不指定目录全名称时,缺省为工作目录
Dir.new %%1

#require 'ftools'
FileUtils.mkdir 'test'
file = File.new(%%1)  #"cmd.txt"
file.each do |line|
puts line if line =~ /target/
end

7.
读取文件
#require 'ftools'
File.open(%%1).readlines #'
文件名'

#require 'ftools'
arr = IO.readlines(%%1)  #"myfile"
lines = arr.size
#puts "myfile has #{lines} lines in it."
#longest = arr.collect {|x| x.length}.max
#puts "The longest line in it has #{longest} characters."


8.
写入文件
f=open(%%1,"w")
f.puts(%%2)

9.
写入随机文件
#require 'ftools'
file
 = File.open(%%1,"w") 
file.seek(5)
 
str
 = file.gets # "fghi"

#require 'ftools'
File.open('
文件名')
File.open("cmd.txt","r") do |file|
while line=file.gets
puts line
end
end
puts
file=File.new("cmd.txt","r")
file.each_line do |line|
puts line
end
IO.foreach("cmd.txt") do |line|
puts line if line =~ /target/
puts line if line !~ /target/
end
###
Dir#pos
返回当前子文件指针

Dir#pos=
设置子文件指针

Dir#rewind
设置子文件指针到起始位置

Dir#seek
设置子文件指针

Dir#tell
获取当前指针

10.
读取文件属性
#
文件中是否有内容,(返回false为有内容,返回true为空)
File.new('
文件名').stat.zero?
#
文件大小
File.size?('
文件名')
flag1 = FileTest::zero?("file1")
flag2 = FileTest::size?("file2")
size1 = File.size("file1")
size2 = File.stat("file2").size
###
File::atime(filename)
返回指定文件的最后访问时间

11.
写入属性

12.
枚举一个文件夹中的所有文件夹
#require 'ftools'
puts Dir.glob('**/*').each { | file | file.downcase }

#
要区分目录和普通文件我们这样使用
file1 = File.new("/tmp")
file2 = File.new("/tmp/myfile")
test1 = file1.directory? # true
test2 = file1.file? # false
test3 = file2.directory? # false
test4 = file2.file? # true
###
遍历目录
Dir.foreach(%%1) { |entry| puts entry}

13.
复制文件夹
require "fileutils"
FileUtils.cp %%1,%%2

14.
复制一个目录下所有的文件夹到另一个文件夹下
#require 'FileUtils'
list=Dir.entries(%%1)
list.each_index do |x|
FileUtils.cp "#{list[x]}",%%2 if !File.directory?(list[x])
end

15.
移动文件夹
#require 'FileUtils'
FileUtils.mv %%1,%%2

16.
移动一个目录下所有的文件夹到另一个目录下
#require 'FileUtils'
list=Dir.entries(%%1)
list.each_index do |x|
FileUtils.mv "#{list[x]}",%%2 if !File.directory?(list[x])
end



36.
获得当前路径
File.dirname($0)

37.
读取XML数据库

38.
写入XML数据库

39.ZIP
压缩文件
#require 'rubygems'  
#require 'zip/zipfilesystem'
Zip::ZipFile.open(%%1, Zip::ZipFile::CREATE) do |zip|   #'zipfile.zip'
zip.file.open('file1', 'w') { |f| f << 'This is file 1.' }   
zip.dir.mkdir('sub_dir')   
zip.file.open('sub_dir/file2', 'w') { |f| f << 'This is file 2.' }   
end

40.ZIP
解压缩

41.
获得应用程序完整路径

42.ZIP
压缩文件夹
#require 'rubygems'  
#require 'zip/zipfilesystem'
def compress   
  Zip::ZipFile.open 'zipfile.zip', Zip::ZipFile::CREATE do |zip|   
    add_file_to_zip('dir', zip)   
  end  
end  
  
def add_file_to_zip(file_path, zip)   
  if File.directory?(file_path)   
    Dir.foreach(file_path) do |sub_file_name|   
      add_file_to_zip("#{file_path}/#{sub_file_name}", zip) unless sub_file_name == '.' or sub_file_name == '..'  
    end  
  else  
    zip.add(file_path, file_path)   
  end  
end  
add_file_to_zip %%1,%%2

43.
递归删除目录下的文件
#require 'ftools'
file_path = String.new
file_path="D:"
    if File.directory? file_path
      Dir.foreach(file_path) do |file|
        if file!="." and file!=".."
puts "File:"+file
        end
      end
    end



44.
验证DTD

45.Schema
验证

46.Grep
#!/usr/bin/env ruby
# Grep with full regexp-functionality via ruby

if ARGV.shift == "-p"
  pattern = Regexp.new(ARGV.shift)
else
  puts "Please give me a pattern with the '-p' option"
  exit
end
ARGV.each do |filename|
  File.open(filename) do |file|
    file.each do |line|
      puts "#{filename} #{file.lineno.to_s}: #{line}" if
pattern.match(line)
    end
  end
end

Using it via: rgrep -p '/delete /i' *.php does not match anything, but
this
#!/usr/bin/env ruby
# Grep with full regexp-functionality via ruby

if ARGV.shift == "-p"
  pattern = Regexp.new(ARGV.shift)
else
  puts "Please give me a pattern with the '-p' option"
  exit
end
ARGV.each do |filename|
  File.open(filename) do |file|
    file.each do |line|
      puts "#{filename} #{file.lineno.to_s}: #{line}" if /delete
/i.match(line)
    end
  end
end

47.
直接创建多级目录
#require
 "fileutils"
FileUtils.makedirs(%%1)


TAG:

 

评分:0

我来说两句

Open Toolbar