จัดการ File บน Desktop

Ruby by Keptcode.com

Last Updated: 2024-07-06 11:37

Path Folder

Path: [skeleton]\ruby\file.rb

Language: Ruby

# Path Folder
File.dirname(__FILE__)

Check File

Path: [skeleton]\ruby\file.rb

Language: Ruby

begin
  if File.exists?(File.dirname(__FILE__) + '/keptcode.txt')
    content = File.read(File.dirname(__FILE__) + '/keptcode.txt')
    puts content
  end
rescue IOError => e
  puts e
end

Write File

Path: [skeleton]\ruby\file.rb

Language: Ruby

begin
  file = File.open(File.dirname(__FILE__) + '/keptcode.txt', 'w')
  file.write("Write File") 
rescue IOError => e
  puts e
ensure
  file.close unless file.nil?
end

Read File

Path: [skeleton]\ruby\file.rb

Language: Ruby

begin
  file = File.open(File.dirname(__FILE__) + '/keptcode.txt', 'r') do | f |
    content = File.read(File.dirname(__FILE__) + '/keptcode.txt')
    puts content
  end
rescue IOError => e
  puts e
ensure
  file.close unless file.nil?
end

Delete File

Path: [skeleton]\ruby\file.rb

Language: Ruby

begin
  file = File.open(File.dirname(__FILE__) + '/keptcode.txt', 'r') do | f |
    File.delete(f)
  end
rescue IOError => e
  puts e
ensure
  file.close unless file.nil?
end