FileUtils

module FileUtils

Extended Modules

Defined in:

file_utils.cr

Instance Method Summary

Instance Method Detail

def cd(path : String)Source

Changes the current working directory of the process to the given string path.

require "file_utils"

FileUtils.cd("/tmp")

NOTE Alias of Dir.cd

def cd(path : String, &block)Source

Changes the current working firectory of the process to the given string path and invoked the block, restoring the original working directory when the block exits.

FileUtils.cd("/tmp") { Dir.current } # => "/tmp"

NOTE Alias of Dir.cd with block

def cmp(filename1 : String, filename2 : String)Source

Compares two files filename1 to filename2 to determine if they are identical. Returns true if content are the same, false otherwise.

File.write("file.cr", "1")
File.write("bar.cr", "1")
FileUtils.cmp("file.cr", "bar.cr") # => true

def cmp(stream1 : IO, stream2 : IO)Source

Compares two streams stream1 to stream2 to determine if they are identical. Returns true if content are the same, false otherwise.

File.write("afile", "123")
stream1 = File.open("afile")
stream2 = IO::Memory.new("123")
FileUtils.cmp(stream1, stream2) # => true

def cp(src_path : String, dest : String)Source

Copies the file src_path to the file or directory dest. If dest is a directory, a file with the same basename as src_path is created in dest Permission bits are copied too.

File.chmod("afile", 0o600)
FileUtils.cp("afile", "afile_copy")
File.stat("afile_copy").perm # => 0o600

def cp(srcs : Enumerable(String), dest : String)Source

Copies a list of files src to dest. dest must be an existing directory.

Dir.mkdir("files")
FileUtils.cp({"bar.cr", "afile"}, "files")

def cp_r(src_path : String, dest_path : String)Source

Copies a file or directory src_path to dest_path. If src_path is a directory, this method copies all its contents recursively.

FileUtils.cp_r("files", "dir")

def mkdir(path : String, mode = 511) : NilSource

Creates a new directory at the given path. The linux-style permission mode can be specified, with a default of 777 (0o777).

FileUtils.mkdir("src")

NOTE Alias of Dir.mkdir

def mkdir(paths : Enumerable(String), mode = 511) : NilSource

Creates a new directory at the given paths. The linux-style permission mode can be specified, with a default of 777 (0o777).

FileUtils.mkdir(["foo", "bar"])

def mkdir_p(paths : Enumerable(String), mode = 511) : NilSource

Creates a new directory at the given paths, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).

FileUtils.mkdir_p(["foo", "bar", "baz", "dir1", "dir2", "dir3"])

def mkdir_p(path : String, mode = 511) : NilSource

Creates a new directory at the given path, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).

FileUtils.mkdir_p("foo")

NOTE Alias of Dir.mkdir_p

def mv(src_path : String, dest_path : String) : NilSource

Moves src_path to dest_path.

FileUtils.mv("afile", "afile.cr")

NOTE Alias of File.rename

def mv(srcs : Enumerable(String), dest : String) : NilSource

Moves every srcs to dest.

FileUtils.mv(["foo", "bar"], "src")

def pwd : StringSource

Returns the current working directory.

FileUtils.pwd

NOTE Alias of Dir.current

def rm(path : String) : NilSource

Deletes the path file given.

FileUtils.rm("afile.cr")

NOTE Alias of File.delete

def rm(paths : Enumerable(String)) : NilSource

Deletes all paths file given.

FileUtils.rm(["dir/afile", "afile_copy"])

def rm_r(path : String) : NilSource

Deletes a file or directory path. If path is a directory, this method removes all its contents recursively.

FileUtils.rm_r("dir")
FileUtils.rm_r("file.cr")

def rm_r(paths : Enumerable(String)) : NilSource

Deletes a list of files or directories paths. If one path is a directory, this method removes all its contents recursively.

FileUtils.rm_r(["files", "bar.cr"])

def rm_rf(path : String) : NilSource

Deletes a file or directory path. If path is a directory, this method removes all its contents recursively. Ignore all errors.

FileUtils.rm_rf("dir")
FileUtils.rm_rf("file.cr")
FileUtils.rm_rf("non_existent_file")

def rm_rf(paths : Enumerable(String)) : NilSource

Deletes a list of files or directories paths. If one path is a directory, this method removes all its contents recursively. Ignore all errors.

FileUtils.rm_rf(["dir", "file.cr", "non_existent_file"])

def rmdir(path : String) : NilSource

Removes the directory at the given path.

FileUtils.rmdir("baz")

NOTE Alias of Dir.rmdir

def rmdir(paths : Enumerable(String)) : NilSource

Removes all directories at the given paths.

FileUtils.rmdir(["dir1", "dir2", "dir3"])

def touch(paths : Enumerable(String), time : Time = Time.now)Source

Attempts to set the access and modification times of each file given in the paths parameter to the value given in time.

If the file does not exist, it will be created.

FileUtils.touch(["foo", "bar"])

def touch(path : String, time : Time = Time.now)Source

Attempts to set the access and modification times of the file named in the path parameter to the value given in time.

If the file does not exist, it will be created.

FileUtils.touch("afile.cr")

NOTE Alias of File.touch

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/FileUtils.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部