ruby文件夹操作

Python08

ruby文件夹操作,第1张

一、新建文件

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

LZ好,你如果想用rpgmaker自己弄游戏的话,最好自己先学好ruby语句,这样自己修改脚本就方便多了。 默认脚本是没有问题的,你不会的话最好别在里面乱改东西,因为有时候少一个字就不行了。你把脚本还原就ok

你拿这段正常的替换脚本编辑器倒数第7行的Scene_Battle 3

class Scene_Battle

def start_phase3

@phase = 3

@actor_index = -1

@active_battler = nil

phase3_next_actor

end

def phase3_next_actor

begin

if @active_battler != nil

@active_battler.blink = false

end

if @actor_index == $game_party.actors.size-1

start_phase4

return

end

@actor_index += 1

@active_battler = $game_party.actors[@actor_index]

@active_battler.blink = true

end until @active_battler.inputable?

phase3_setup_command_window

end

def phase3_prior_actor

begin

if @active_battler != nil

@active_battler.blink = false

end

if @actor_index == 0

start_phase2

return

end

@actor_index -= 1

@active_battler = $game_party.actors[@actor_index]

@active_battler.blink = true

end until @active_battler.inputable?

phase3_setup_command_window

end

def phase3_setup_command_window

@party_command_window.active = false

@party_command_window.visible = false

@actor_command_window.active = true

@actor_command_window.visible = true

@actor_command_window.x = @actor_index * 160

@actor_command_window.index = 0

end

def update_phase3

if @enemy_arrow != nil

update_phase3_enemy_select

elsif @actor_arrow != nil

update_phase3_actor_select

elsif @skill_window != nil

update_phase3_skill_select

elsif @item_window != nil

update_phase3_item_select

elsif @actor_command_window.active

update_phase3_basic_command

end

end

def update_phase3_basic_command

if Input.trigger?(Input::B)

$game_system.se_play($data_system.cancel_se)

phase3_prior_actor

return

end

if Input.trigger?(Input::C)

case @actor_command_window.index

when 0

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.kind = 0

@active_battler.current_action.basic = 0

start_enemy_select

when 1

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.kind = 1

start_skill_select

when 2

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.kind = 0

@active_battler.current_action.basic = 1

phase3_next_actor

when 3

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.kind = 2

start_item_select

end

return

end

end

def update_phase3_skill_select

@skill_window.visible = true

@skill_window.update

if Input.trigger?(Input::B)

$game_system.se_play($data_system.cancel_se)

end_skill_select

return

end

if Input.trigger?(Input::C)

@skill = @skill_window.skill

if @skill == nil or not @active_battler.skill_can_use?(@skill.id)

$game_system.se_play($data_system.buzzer_se)

return

end

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.skill_id = @skill.id

@skill_window.visible = false

if @skill.scope == 1

start_enemy_select

elsif @skill.scope == 3 or @skill.scope == 5

start_actor_select

else

end_skill_select

phase3_next_actor

end

return

end

end

def update_phase3_item_select

@item_window.visible = true

@item_window.update

if Input.trigger?(Input::B)

$game_system.se_play($data_system.cancel_se)

end_item_select

return

end

if Input.trigger?(Input::C)

@item = @item_window.item

unless $game_party.item_can_use?(@item.id)

$game_system.se_play($data_system.buzzer_se)

return

end

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.item_id = @item.id

@item_window.visible = false

if @item.scope == 1

start_enemy_select

elsif @item.scope == 3 or @item.scope == 5

start_actor_select

else

end_item_select

phase3_next_actor

end

return

end

end

def update_phase3_enemy_select

@enemy_arrow.update

if Input.trigger?(Input::B)

$game_system.se_play($data_system.cancel_se)

end_enemy_select

return

end

if Input.trigger?(Input::C)

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.target_index = @enemy_arrow.index

end_enemy_select

if @skill_window != nil

end_skill_select

end

if @item_window != nil

end_item_select

end

phase3_next_actor

end

end

def update_phase3_actor_select

@actor_arrow.update

if Input.trigger?(Input::B)

$game_system.se_play($data_system.cancel_se)

end_actor_select

return

end

if Input.trigger?(Input::C)

$game_system.se_play($data_system.decision_se)

@active_battler.current_action.target_index = @actor_arrow.index

end_actor_select

if @skill_window != nil

end_skill_select

end

if @item_window != nil

end_item_select

end

phase3_next_actor

end

end

def start_enemy_select

@enemy_arrow = Arrow_Enemy.new(@spriteset.viewport1)

@enemy_arrow.help_window = @help_window

@actor_command_window.active = false

@actor_command_window.visible = false

end

def end_enemy_select

@enemy_arrow.dispose

@enemy_arrow = nil

if @actor_command_window.index == 0

@actor_command_window.active = true

@actor_command_window.visible = true

@help_window.visible = false

end

end

def start_actor_select

@actor_arrow = Arrow_Actor.new(@spriteset.viewport2)

@actor_arrow.index = @actor_index

@actor_arrow.help_window = @help_window

@actor_command_window.active = false

@actor_command_window.visible = false

end

def end_actor_select

@actor_arrow.dispose

@actor_arrow = nil

end

def start_skill_select

@skill_window = Window_Skill.new(@active_battler)

@skill_window.help_window = @help_window

@actor_command_window.active = false

@actor_command_window.visible = false

end

def end_skill_select

@skill_window.dispose

@skill_window = nil

@help_window.visible = false

@actor_command_window.active = true

@actor_command_window.visible = true

end

def start_item_select

@item_window = Window_Item.new

@item_window.help_window = @help_window

@actor_command_window.active = false

@actor_command_window.visible = false

end

def end_item_select

@item_window.dispose

@item_window = nil

@help_window.visible = false

@actor_command_window.active = true

@actor_command_window.visible = true

end

end 10866希望对你有帮助!