Ruby File Operations

Source: Internet
Author: User

1. Create a folder
Dir. New % 1
Dir: mkdir # When the full directory name is not specified, the default value is the working directory.
Dir: chdir () change the working directory of the current script
Fileutils. mkdir 'test'
File = file. New ("cmd.txt ")
File. Each do | Line |
Puts line if line = ~ /Target/
End

2. Create a file
Change the current root directory
Dir. chdir ("/home/GUY/sandbox/tmp ")
Dir. chroot ("/home/GUY/sandbox ")
Dir. New % 1
# Dir: rmdir # When the full directory name is not specified, the default value is the working directory.

3. delete an object
Change the current root directory
Dir. chdir ("/home/GUY/sandbox/tmp ")
Dir. chroot ("/home/GUY/sandbox ")
Dir. New % 1
# Dir: rmdir # When the full directory name is not specified, the default value is the working directory.

4. delete folders
# Require 'fileutils'
Fileutils. rm_r (% 1)
5. Delete all folders under a file
Dir: chdir
Dir: Pwd property or dir. getwd ()
# Change the current script working directory

6. Clear folders
Dir: chdir % 1 change the current script working directory
Dir: rmdir # When the full directory name is not specified, the default value is the working directory.
Dir. New % 1
# Require 'ftool'
Fileutils. mkdir 'test'
File = file. New (% 1) # "cmd.txt"
File. Each do | Line |
Puts line if line = ~ /Target/
End

7. Read files
# Require 'ftool'
File. Open (% 1). readlines # 'file name'
# Require 'ftool'
Arr = Io. readlines (% 1) # "myfile"
Lines = arr. Size
# Puts "myfile has # {lines} lines in it ."
# Longest = arr. Collect {| x | X. Length}. Max
# Puts "the longest line in it has # {longest} characters ."

8. Write files
F = open (% 1, "W ")
F. Puts (% 2)

9. Write random files
# Require 'ftool'
File = file. Open (% 1, "W ")
File. Seek (5)
STR = file. Gets # "fghi"
# Require 'ftool'
File. Open ('filename ')
File. Open ("cmd.txt", "R") Do | file |
While line = file. Gets
Puts line
End
End
Puts
File = file. New ("cmd.txt", "R ")
File. each_line do | Line |
Puts line
End
Io. foreach ("cmd.txt") Do | Line |
Puts line if line = ~ /Target/
Puts line if line !~ /Target/
End
###
Dir # POS
Returns the pointer to the current sub-file.
Dir # Pos =
Set the sub-file pointer
Dir # rewind
Set the sub-file pointer to the starting position
Dir # Seek
Set the sub-file pointer
Dir # Tell
Get current pointer

10. Read File Attributes
# Check whether there is content in the file. (If false is returned, there is content. If true is returned, it is null)
File. New ('filename '). Stat. Zero?
# File Size
File. Size? ('Filename ')
Flag1 = filetest: Zero? ("File1 ")
Flag2 = filetest: size? ("File2 ")
Size1 = file. Size ("file1 ")
Size2 = file. Stat ("file2"). Size
###
File: atime (filename)
Returns the last access time of the specified object.

11. Write attributes

12. enumerate all folders in a folder
# Require 'ftool'
Puts dir. glob ('**/*'). Each {| file. downcase}
# Use this method to distinguish between directories and common files
File1 = file. New ("/tmp ")
File2 = file. New ("/tmp/myfile ")
Test1 = file1.directory? # True
Test2 = file1.file? # False
Test3 = file2.directory? # False
Test4 = file2.file? # True
###
Traverse directories
Dir. foreach (% 1) {| entry | puts entry}

13. Copy a folder
Require "fileutils"
Fileutils. CP % 1, % 2

14. Copy all the folders in one directory to another.
# Require 'fileutils'
List = dir. Entries (% 1)
List. each_index do | x |
Fileutils. CP "# {list [x]}", % 2 if! File. directory? (List [x])
End

15. Move folders
# Require 'fileutils'
Fileutils. Mv % 1, % 2

16. move all the folders in one directory to another
# require 'fileutils'
List = dir. entries (% 1)
list. each_index do | x |
fileutils. mv "# {list [x]}", % 2 if! File. directory? (List [x])
end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.