修改后的类(LibScreen.rb)如下:
LibScreen.rb
class ScreenClass
def initialize
require 'Win32API'
end
#功能说明:
#- 将文件重命名
#
#参数说明:
#- from:原文件路径,必须带文件名,格式如:c:\\test.txt
#- to:新文件路径及名称,必须带文件名,格式如:c:\\test_bak.txt,不带该参数时,默认在原文件名后加上日期
#
#调用示例:
#- $TxtClass.RenameFile("c:\\test.txt","c:\\test_bak.txt")
#- $TxtClass.RenameFile("c:\\test.txt")
#
#返回值说明:
#- 成功:返回true
#- 失败:返回false
def RenameFile(from,to = nil)
begin
if (FileTest::exist?(from)) and (File.basename(from) =~ /.*\..*/ )
if (to == nil)
extname = File.extname(from)
filename = File.basename(from,extname)
new_filename = filename + '.' + Time.now.strftime("%Y%m%d%H%M%S") + extname
to = File.dirname(from) + '/'+ new_filename
end
File.rename(from, to)
return true
else
puts "重命名文件失败,原因:文件不存在,路径为#{from}"
return false
end
rescue StandardError => bang
puts "Error running script: " + bang
return false
end
end
if (file_basename =~ /.*\..*/)
file_dirname = File.dirname(file_path)
else
file_basename = ''
file_dirname = file_path
end
if (!FileTest::exist?(file_dirname)) #判断目录是否存在,不存在则创建相应目录
FileUtils.makedirs(file_dirname)
end
if (file_dirname[0,2] == './')
real_dir = Pathname.new(File.dirname(File.dirname(file_dirname[0,2]))).realpath
real_path = File.join(real_dir,file_dirname[2,file_dirname.length] )
else
real_path = file_dirname
end
if (real_path.include?(".."))
temp_row = real_path.split('/')
temp_row.each do |dir|
if(dir == "..")
@@real_dir_row.pop
else
@@real_dir_row.push(dir)
end
end
real_path = @@real_dir_row.join('/')
end
if (return_file.upcase == 'Y')
result = File.join(real_path,file_basename)
else
result = real_path
end
result = result.to_s.gsub('/','\\')
return result
else
puts "获取文件路径失败,原因:#{real_path}路径格式不正确。"
return false
end
rescue StandardError => bang
puts "Error running script: " + bang
return false
end
end #def GetRealPath