Wednesday, October 19, 2011

[Ruby] File Info Getter and Directory Manager

Hello, this is my first post in this blog. In this occasion I want to tell you how to make file info getter and directory manager program using Ruby programming language.

If you're never heard of Ruby, Ruby is a dynamic, open source interpreted programming language which you don’t need to compile the source code first if you want to run it. Ruby is focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. It is used on many programs, including NASA Langley Research Center simulator, Motorola simulator, Google Sketch-Up, RPG Maker, and many more.

In this program, first it will print current directory and the items in current directory. If the items is Directory then add text [Directory] before the item name, but if it is a file, then show the file size. If file cannot be accessed and size-checked, it will print (N/A) instead. The items in this directory is all items on your harddisk, even if it is a hidden or system file.

The user can select one item from the items list by inputting the number index. If the items is a directory, program will open the directory and set it as current directory. But, if the item is a file, it will show the file information, including location, file name, and the size.

Beside that, user can also create a new directory or delete current directory. User also can navigate to the upper directory, the root directory, or go to specified drive and folder by inputting the command.

Here's the output if you're having difficulty in imagining what I tell you before:

FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/Documents and Settings/Administrator/Desktop/RubyApplication1/lib
Directory content: 
  1. [Directory] hello world                                       
  2. main.rb                                               5.58 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> c
Create what directory?
> new



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/Documents and Settings/Administrator/Desktop/RubyApplication1/lib
Directory content: 
  1. [Directory] hello world                                       
  2. [Directory] new                                               
  3. main.rb                                               5.58 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> 2



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/Documents and Settings/Administrator/Desktop/RubyApplication1/lib/new
Directory content: 

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> d



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/Documents and Settings/Administrator/Desktop/RubyApplication1/lib
Directory content: 
  1. [Directory] hello world                                       
  2. main.rb                                               5.58 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> u



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/Documents and Settings/Administrator/Desktop/RubyApplication1
Directory content: 
  1. [Directory] lib                                               
  2. [Directory] nbproject                                         
  3. [Directory] spec                                              
  4. [Directory] test                                              

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> r



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
C:/
Directory content: 
  1. [Directory] $AVG                                              
  2. [Directory] Documents and Settings                            
  3. [Directory] Novell                                            
  4. [Directory] Program Files                                     
  5. [Directory] WINDOWS                                           
  6. .rnd                                                  1.00 KiB
  7. AUTOEXEC.BAT                                         (N/A) KiB
  8. boot.ini                                              0.21 KiB
  9. CONFIG.SYS                                           (N/A) KiB
 10. hiberfil.sys                                    1039604.00 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> g
Go to what directory?
> d:/



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
D:/
Directory content: 
  1. text.txt                                              0.06 KiB
  2. Thumbs.db                                            10.50 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> 2

File Information:
Location : D:/
File name: Thumbs.db
File size: 10752 bytes



FILE INFO GETTER AND DIRECTORY MANAGER
--------------------------------------

Current directory: 
D:/
Directory content: 
  1. text.txt                                              0.06 KiB
  2. Thumbs.db                                            10.50 KiB

Available Commands:
NAVIGATION: U: To Upper    R: To Root     G: Go to       
OTHERS:     C: Create dir  D: Delete dir  X: Exit        

Type your selected item number, or command letter
> x
Thank you for using this program.


I originally typed the code in NetBeans, but you can try to run it using another IDE or interpreter. So, here's the code. If you don't understand some part of it, you can ask it through the comment in this post.

begin
  puts "\n" * 25
  puts 'FILE INFO GETTER AND DIRECTORY MANAGER'
  puts '--------------------------------------'
  puts

  no = 0
  item.clear
  
  puts 'Current directory: ' << Dir.pwd
  puts 'Directory content: '
  
  for x in Dir.entries(Dir.pwd)
    if File::directory?(x) == true and x!='.' and x!='..' and no < 1000
      no += 1
      item[no] = x
      puts no.to_s.rjust(3)<< '. [Directory] '<< item[no][0..49].ljust(50)
    end
  end

  for x in Dir.entries(Dir.pwd)
    if File::directory?(x) == false and no < 1000
      no += 1
      item[no] = x
      print no.to_s.rjust(3) << '. ' << item[no][0..47].ljust(48)
      begin
        filesize = (File::size?(x)/1024.0)
        printf "%10.2f KiB", filesize
      rescue
        print '     (N/A) KiB'
      end
      puts
    end
  end

  puts 
  puts 'Available Commands:'
  print 'NAVIGATION: ' << 'U: To Upper'.ljust(15)
  puts  'R: To Root'.ljust(15)<<'G: Go to'.ljust(15)
  print 'OTHERS:     ' << 'C: Create dir'.ljust(15)
  puts  'D: Delete dir'.ljust(15)<<'X: Exit'.ljust(15)

  puts
  puts 'Type your selected item number, or command letter'
  print '> '
  input = gets.chomp.downcase

  case input
  when 'u'
    Dir.chdir('..')
  when 'r'
    Dir.chdir(Dir.pwd[0..2])
  when 'g'
    puts 'Go to what directory?'
    print '> '
    foldergoto = gets.chomp.downcase
    begin
      Dir.chdir(foldergoto)
    rescue Exception => e
      puts e.message
      gets
    end
  when 'c'
    puts 'Create what directory?'
    print '> '
    foldercreate = gets.chomp.downcase
    begin
      Dir.mkdir(foldercreate)
    rescue Exception => e
      puts e.message
      gets
    end
  when 'd'
    begin
      Dir.rmdir(Dir.pwd)
    rescue Exception => e
      puts e.message
      gets
    end
  when 'x'
    puts 'Thank you for using this program'
  else
    inputint = input.to_i
    if inputint < 1 or inputint > item.length - 1
      puts 'Invalid input. Max. value is ' << (item.length - 1).to_s
      gets
    else
      if File::directory?(item[inputint]) == true
        begin
          Dir.chdir(item[inputint])
        rescue Exception => e
          puts e.message
          gets
        end
      else
        puts
        puts  'File Information:'
        puts  'Location : ' << Dir.pwd
        puts  'File name: ' << item[inputint]
        print 'File size: '
        begin
          puts File::size?(item[inputint]).to_s << " bytes"
        rescue
          puts 'Unknown'
        end
        gets
      end
    end
  end
end until input == 'x'


I think that's all for now. I hope this article can made you understand how to access file and directory in Ruby. See you in the next post.

No comments:

Post a Comment