ruby copy 文件

Python014

ruby copy 文件,第1张

今天下载了spring3.2.0的新版本,打开libs目录后,发现除了要正常使用的jar之外,还有好多javadoc和sources的jar

文件,想把正常使用的Jar拷贝出来,但一个个选又太麻烦,就想着反正最近在学ruby,干脆用ruby写个小工具帮我拷贝吧,

就当是练习了,呵呵。

Ruby代码  

#! ruby -Ku

require "fileutils"

#原始jar文件目录

org_spring_lib_dir = "E:\\Download\\spring3.2.0\\spring-framework-3.2.0.RELEASE\\libs"

#拷贝后jar文件目录

dest_spring_lib_dir = "E:\\Download\\spring3.2.0\\spring-framework-3.2.0\\usedlibs"

#拷贝目录不存在的话创建

if !File.exist?(dest_spring_lib_dir) then

#windows下权限设置是个问题,有待解决

FileUtils.mkdir_p(dest_spring_lib_dir)

print(dest_spring_lib_dir + " was created!\n")

end

Dir.foreach(org_spring_lib_dir){|fileName|

#除javadoc和sources的jar文件全部拷贝到新目录

org_file = org_spring_lib_dir + "\\" + fileName

#记得排除.目录和..目录

if !(fileName.include? "javadoc") && !(fileName.include? "sources") && !File.directory?(org_file) then      

FileUtils.cp_r(org_file, dest_spring_lib_dir)

print(fileName + " was copied!\n")

end

}

[ruby] view plain copy

#! ruby -Ku

require "fileutils"

#原始jar文件目录

org_spring_lib_dir = "E:\\Download\\spring3.2.0\\spring-framework-3.2.0.RELEASE\\libs"

#拷贝后jar文件目录

dest_spring_lib_dir = "E:\\Download\\spring3.2.0\\spring-framework-3.2.0\\usedlibs"

#拷贝目录不存在的话创建

if !File.exist?(dest_spring_lib_dir) then

#windows下权限设置是个问题,有待解决

FileUtils.mkdir_p(dest_spring_lib_dir)

print(dest_spring_lib_dir + " was created!\n")

end

Dir.foreach(org_spring_lib_dir){|fileName|

#除javadoc和sources的jar文件全部拷贝到新目录

org_file = org_spring_lib_dir + "\\" + fileName

#记得排除.目录和..目录

if !(fileName.include? "javadoc") && !(fileName.include? "sources") && !File.directory?(org_file) then

FileUtils.cp_r(org_file, dest_spring_lib_dir)

print(fileName + " was copied!\n")

end

}

上面写的方法比较笨,准备以后再改改,我也是边查api边写的。

之后发现个问题,在创建目录的时候,用mkdir会报错,告诉我没有那样的目录,得用mkdir_p才行。

然后是关于创建后的目录的权限问题,看api好像全是linux的权限代码指定,貌似在windows下没法设定读写权限。我一开始创建目录后,发现是只读属性,导致我后面在拷贝文件时报错,告诉我没有权限。 果然,ruby还是适合在linux下用呀。

fin = File.new("C:\\1.txt")

tmp = []

fin.each_line do |line|

if line =~ /include.*?xercesc.*?>/

line.sub!(/(.*)<.*\/(.*?)>(.*)/, '\1"\2"\3')

end

tmp <<line

end

fin.close

# puts tmp.join()

fout = File.new("C:\\2.txt", "w")

fout.write(tmp.join())

fout.close

测试输入文件1.txt:

this is the first line

</include abc_xercesc.h>

xy <aaa/include 123_xercesc.c>zd

测试输出文件2.txt:

this is the first line

"include abc_xercesc.h"

xy "include 123_xercesc.c" zd

CHANGELOG, LICENSE, README, and UPGRADE text documents

demos folder

framework folder

requirements folder

第一部分为更新文档、许可(BSD许可,可以自由商用),升级文档以及README。

demos中有四个示例:a blog, the game Hangman, a basic “Hello, World!”, and a phone book。

framework 中是使用Yii必须的框架,核心部分。

requirements 主要是检查运行环境用的工具,另外还有一些小东西。