Everyday Scripting with Ruby

  • 判断软件卸载是否删除了所有相关的文件夹及文件

1.假定要将软件安装到C盘,先在C盘创建文件inventory.rb,输出C盘下所有文件夹及文件包括子文件夹及子文件夹中文件。创建文件old_inventory.txt存放软件安装卸载前的文件列表,创建文件new_inventory.txt存放软件安装卸载后的文件列表,创建文件differences.txt存放比较结果。

puts Dir.glob('C:/**/*').each { | file | file.downcase }.sort

2.打开命令行切换到C盘,执行命令:ruby inventory.rb > old_inventory.txt,将文件夹及文件列表输出到文件old_inventory.txt中。

3.安装软件。

4.卸载软件。

5.打开命令行切换到C盘,执行命令:ruby inventory.rb > new_inventory.txt,将文件夹及文件列表输出到文件new_inventory.txt中。

6.在C盘新建文件difference.rb用来对比安装前卸载后的文件列表。

#old_inventory = File.open('old-inventory.txt').readlines
#new_inventory = File.open('new-inventory.txt').readlines

#ARGV[0] = 'old-inventory.txt' #Command-line arguments are available as strings in the array named ARGV.
#ARGV[1] = 'new-inventory.txt' #ARGV[0] is the first command-line argument and ARGV[1] is the 2nd one.
 
def check_usage 
  unless ARGV.length == 2
    puts "Incorrect argument!!\nUsage: ruby differences.rb old_inventory.txt new_inventory.txt"
    exit
  end
end

#def boring?(line)
#  puts "boring? " + line.inspect
#  line.split('/').include?('temp') or line.split('/').include?('recycler')
#end

def boring?(line, boring_words)
  boring_words.any? do | boring_word | 
    contains?(line, boring_word) #It returns true if any of the array elements make the attached block return true. 
  end 
end

def contains?(line, boring_word)
  line.chomp.split('/').include?(boring_word) #get rid of trailing \n
end

def inventory_from(filename)
  inventory = File.open(filename).readlines
  downcased = inventory.collect do | line |
      line.downcase  #downcase the file names
  end
  downcased.reject do | line |   #reject the entire contents of any folder named temp or recycler.
      boring?(line, ['temp', 'recycler'])
  end
end

# old_inventory = File.open(ARGV[0]).readlines.collect do | line |
#   line.downcase
# end
# new_inventory = File.open(ARGV[1]).readlines.collect do | line |
#   line.downcase
# end

def compare_inventory_files(old_inventory, new_inventory)
  old_inventory = inventory_from(ARGV[0])
  new_inventory = inventory_from(ARGV[1])

  number_of_new_files = (new_inventory - old_inventory).length
  puts "The following #{number_of_new_files} files have been added:"
  puts new_inventory - old_inventory

  puts ''

  number_of_deleted_files = (old_inventory - new_inventory).length
  puts "The following #{number_of_deleted_files} files have been deleted:"
  puts old_inventory - new_inventory

  #puts ''

  #number_of_unchanged_files = (new_inventory & old_inventory).length
  #puts "The following #{number_of_unchanged_files} files have not been changed:"
  #puts new_inventory & old_inventory
end 

if $0 == __FILE__  #if the file was run directly, not required or loaded 
  check_usage
  compare_inventory_files(ARGV[0], ARGV[1])    
end

7.在命令行切换到C盘运行:ruby differences.rb old_inventory.txt new_inventory.txt,将结果打印出来或运行:ruby differences.rb old_inventory.txt new_inventory.txt > differences.txt,将结果输出到文件difference.txt中。

 

转载于:https://www.cnblogs.com/lucien06/p/4521679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值