ruby怎么获取一个指定目录下最新的文件

Python012

ruby怎么获取一个指定目录下最新的文件,第1张

如果不需要递归删除文件夹里子文件夹的内容,下面这个就可以了。 directory = "D:\\data" Dir.foreach(directory) do |item| if item != '.' and item != '..' u_dir = directory + File::Separator + item if File.stat(u_dir).directory? Dir.foreach(u_dir) do |f| if File.stat(u_dir + File::Separator + f).file? File.delete(u_dir + File::Separator + f) end end end end end

本文在Windows7下测试成功。

安装和设置Git

下载Git for

Windows,采用默认安装,安装完成后就可以在本地使用Git了。

但要将内容放到Github上,必须先在Github网站上注册个账户,然后在本机使用Git创建SSH Key。操作如下:

在Git Bash上输入命令:

ssh-keygen -C "[email protected]" -t rsa

Note: “[email protected]”需要更换成你在Github上注册的Email地址或者是Username

这样会在用户目录(C:\Users\用户名)下产生一个.ssh文件夹,里面为对应的SSH

Keys,其中id_rsa.pub是Github需要的SSH公钥文件。

到c:\Users\用户名\.ssh\目录找到id_rsa.pub(可能位置不一定对,但是确认是以.pub结尾的文件),并用记事本打开复制全部内容。

Note:建议私钥公钥的名称最好写成"id_rsa",这样连接Github的时候会找这个文件,如果文件名已定,之后改名也行。

在github网站选择“Account Settings”>>“SSH Public Keys”>>“Add another

public key”,将刚才复制的内容粘贴到key文本框内。

这样就可以直接使用Git和GitHub了。

Note:建议在Git Bash中输入“ssh -v [email protected]”测试能够正常连接github

安装Ruby环境

下载RubyInstaller和DevKit。

因为Octopress需要的Ruby版本为1.9.2,所以选rubyinstaller-1.9.2-p290.exe,DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe。

先安装RubyInstaller,然后解压缩DevKit(路径中不能有中文)。

在“Start Command Prompt with Ruby”命令行中进入DevKit解压缩的目录,然后运行以下命令:

ruby dk.rb init

ruby dk.rb install

gem install rdiscount --platform=ruby

如果安装成功,就可以使用一些Ruby的工具了,也为后面搭建博客提供了基础环境。

安装Octopress

先通过Git从Github上克隆一份Octopress(在Git Bash上输入命令)

git clone git://github.com/imathis/octopress.git octopress

然后安装一些依赖的工具(后面都是在Start Command Prompt with Ruby中输入)

cd octopress

ruby --version # Should report Ruby 1.9.2

gem install bundler

bundle install

安装Octopress默认的Theme

rake install

配置Octopress

将octopress的文件夹下的_config.yml的编码改成UTF-8:

保存(或另存为)时选择编码格式为UTF-8

修改_config.yml,批改url、title、subtitle、author等等。

到Ruby的安装目次\lib\ruby\gems\1.9.1\gems\jekyll-0.11.2\lib\jekyll\找到convertible.rb这个文件,批改self.content

= File.read(File.join(base, name))为self.content = File.read(File.join(base,

name), :encoding =>"utf-8")。

写博文

最简单的方式:复制octopress\source\_posts下某个文件,例如2012-07-30-the-first-post.markdown,修改文件名和文件中的内容

或者,命令行中输入rake

new_post["title"],会创建一个新的Post,新文件在source/_post下,文件名如下面的格式:2012-07-31-title.markdown。该文件可以直接打开修改。

写文章时,可以使用Markdown和Octopress

Plugins等工具对内容进行格式排版。

预览效果

在修改设置或者写完文章后,想看看具体效果,可以通过如下命令来完成:

rake generate

rake preview

将博客部署到Github上

在预览的效果符合自己的预期后,就可以通过如下命令将内容部署到Github上了。

如果是第一次部署,需要在Github上创建一个username.github.com的repository

在github网站选择“Create a New Repo”,如图

填写对应的内容即可

note:Repository

name填写username.github.com,username一定要和github的username一致,建好的博客代表的是你这个github账户的主页即page

配置octopress与github的连接:

进入Octopress目录:

rake setup_github_pages

按照提示填入你的github项目网址,比如:

[email protected]:Username/yourname.github.com.git

note:可以按照上面的修改,也可以在github的项目页中找地址

分发到github上:

rake deploy

第一次运行时,会询问是否建立对github的授权,输入:yes。然后将站点更新的内容推送到github上。

补充一点:

最后的但并不是最重要的,我们需要将修改的日志同步到github上,因此下面的3个命令也是必须的。

git status

git add .

git commit -m 'your message'

git push origin source

大功告成!

一、新建文件

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