ruby copy 文件

Python010

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下用呀。

报这个ERROR: While executing gem ... (Gem::FilePermissionError)

You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

权限错误的解决办法

因为ruby环境系统自带,所以Mac系统为了保证自身环境的问题,对权限进行了限制,最终导致一般用户无法对系统的ruby环境相关的文件读写内容。所以解决点也就在这了

解决办法: 1第一种修改权限(不建议这么做,因为是root权限,修改权限可能对系统造成影响,这里不提供修改方法了)

                 2第二种 两套ruby 环境

第二种方案大体也就分成三步了:                 

安装针对于用户所使用的ruby环境

导入新的ruby环境的环境变量

查看是否ruby环境是否安装成功

进行安装之前,我们通过以下命令进行排查,查看当前的ruby环境是否是用的系统自带的环境

whichruby

如果ruby使用的路径如下,那么此时就是用的系统自带的ruby环境了

/usr/bin

推荐使用homebrew安装ruby环境,如果没有安装homebrew`,可以通过以下命令安装:

/bin/bash -c"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

假定已经安装了homebrew环境了,使用以下命令进行ruby的安装

brew install ruby

此时可能默认安装到了系统自带的路径,在执行,建议当前安装的最新版本 把2.7.0换成你安装成的最先版本 如果不知道最新版本 可以执行 ruby -v 查看

echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7.0/bin:$PATH"' >>~/.zshrc

然后在执行

source ~/.zshrc

继续执行which ruby

此时能看到路径已经不是系统路径了,退出终端重新执行安装cocoapods,问题解决

一、新建文件

f=File.new(File.join("C:","Test.txt"), "w+")

f.puts("I am Jack")

f.puts("Hello World")

文件模式

"r" :Read-only. Starts at beginning of file (default mode).

"r+" :Read-write. Starts at beginning of file.

"w" :Write-only. Truncates existing file to zero length or creates a new file for writing.

"w+" :Read-write. Truncates existing file to zero length or creates a new file for reading and writing.

"a" :Write-only. Starts at end of file if file existsotherwise, creates a new file for writing.

"a+" :Read-write. Starts at end of file if file existsotherwise, creates a new file for reading and writing.

"b" :(DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above

二、读取文件

file=File.open(File.join("C:","Test.txt"),"r")

file.each { |line| print "#{file.lineno}.", line }

file.close

三、新建、删除、重命名文件

File.new( "books.txt", "w" )

File.rename( "books.txt", "chaps.txt" )

File.delete( "chaps.txt" )

四、目录操作

1     创建目录

Dir.mkdir("c:/testdir")

04     #删除目录

05     Dir.rmdir("c:/testdir")

07     #查询目录里的文件

08     p Dir.entries(File.join("C:","Ruby")).join(' ')

10     #遍历目录

11     Dir.entries(File.join("C:","Ruby")).each {

|e| puts e

}

1、ARGV and ARGF

ARGV

ARGV <<"cnblogslink.txt"

#The gets method is a Kernel method that gets lines from ARGV

print while gets

p ARGV.class

ARGF

while line = ARGF.gets

print line

end

2、文件信息查询

#文件是否存在

p File::exists?( "cnblogslink.txt" ) # =>true

#是否是文件

p File.file?( "cnblogslink.txt" ) # =>true

#是否是目录

p File::directory?( "c:/ruby" ) # =>true

p File::directory?( "cnblogslink.txt" ) # =>false

#文件权限

p File.readable?( "cnblogslink.txt" ) # =>true

p File.writable?( "cnblogslink.txt" ) # =>true

p File.executable?( "cnblogslink.txt" ) # =>false

#是否是零长度

p File.zero?( "cnblogslink.txt" ) # =>false

#文件大小 bytes

p File.size?( "cnblogslink.txt" ) # =>74

p File.size( "cnblogslink.txt" ) # =>74

#文件或文件夹

p File::ftype( "cnblogslink.txt" ) # =>"file"

#文件创建、修改、最后一次存取时间

p File::ctime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

p File::mtime( "cnblogslink.txt" ) # =>Sat Sep 19 08:06:34 +0800 2009

p File::atime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

3、查找文件

puts "查找目录下所有文件及文件夹"

Dir["c:/ruby/*"].each {|x|

puts x

}

puts "条件查询"

Dir.foreach('c:/ruby') {

|x| puts x if x != "." &&x != ".."

}

puts "查找某一类型文件"

Dir["*.rb"].each {|x|

puts x

}

puts "Open 查询"

Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}

puts "---------------------------"

puts "正则表达式查询"

Dir["c:/ruby/ruby/[rs]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/[^s]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/{ruby,li}*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/?b*"].each{|x| puts x}

puts "查找目录及子目录的文件"

require 'find'

Find.find('./') { |path| puts path }

3、查询目录及子目录文件

require "find"

Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|

Find.prune if f == "."

puts f

end

原型:ref.find( [ aName ]* ) {| aFileName | block }

prune:Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find::find.

4、文件比较 复制等

require 'ftools'

File.copy 'testfile', 'testfile1'  » true

File.compare 'testfile', 'testfile1'  » true