File Manipulation
These functions provide a low-level interface to the computer's file system.
These functions provide a low-level interface to the computer's file system.
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| GetSha1Hash | 1 | Generate SHA1 checksum of a file |
| getRelativePath | 1 | Gets the relative pathname relative to a directory. |
| filesize | 1 | Extract File Information Utility function to extract information about files on the user's file systems. |
| filepath | 1 | Construct Path to File Construct the path to a file from components in a platform-independent way. |
| fileinfo | 1 | Extract File Information Utility function to extract information about files on the user's file systems. |
| filecopy | 1 | file.copy works in a similar way to file.append but with the arguments in the natural order for copying. |
| fileExt | 1 | Get file extension name |
| dirCopy | 1 | copy file contents in one dir to another dir |
| openDir | 1 | Open an interface to a specific local filesystem location |
| normalizePath | 1 | Express File Paths in Canonical Form Convert file paths to canonical form for the platform, to display them in a user-understandable form and so that relative and absolute pat… |
| Rhome | 1 | Return the R Home Directory Return the R home directory, or the full path to a component of the R installation. |
| dirname | 1 | dirname returns the part of the path up to but excluding the last path separator, or "." if there is no path separator. |
| listFiles | 1 | List the Files in a Directory/Folder |
| listDirs | 1 | List the Files in a Directory/Folder |
| file_ext | 1 | File Utilities |
| basename | 1 | removes all of the path up to and including the last path separator (if any). |
| normalizeFileName | 1 | removes all of the invalid character for the windows file name |
| exists | 1 | file.exists returns a logical vector indicating whether the files named by its argument exist. |
| file_create | 1 | File Manipulation These functions provide a low-level interface to the computer's file system. |
| dirCreate | 1 | dir.create creates the last element of the path, unless recursive = TRUE. |
| dir_exists | 1 | dir.exists returns a logical vector of TRUE or FALSE values (without names). |
| readLines | 1 | Read Text Lines from a Connection Read some or all text lines from a connection. |
| readText | 1 | Reads all characters from the current position to the end of the given stream. |
| writeLines | 1 | Write Lines to a Connection Write text lines to a connection. |
| getwd | 1 | getwd returns an absolute filepath representing the current working directory of the R process; |
| setwd | 1 | setwd(dir) is used to set the working directory to dir. |
| saveList | 1 | Save a R# object list in json file format |
| readList | 1 | read list from a given json file |
| gzfile | 1 | open a *.gz file for make file data read/write |
| gzcheck | 1 | check of the target gz file data is corrupted or not |
| file | 1 | Functions to create, open and close connections, i.e., "generalized files", such as possibly compressed files, URLs, pipes, etc. |
| readBin | 1 | ransfer Binary Data To and From Connections Read binary data from or write binary data to a connection or raw vector. |
| writeBin | 1 | |
| close | 1 | close connections, i.e., “generalized files”, such as possibly compressed files, URLs, pipes, etc. |
| openZip | 1 | open a zip file |
| openGzip | 1 | decompression of a gzip file and get the deflate file data stream. |
| buffer | 1 | create a new buffer object |
| tempfile | 1 | Create Names for Temporary Files tempfile returns a vector of character strings which can be used as names for temporary files. |
| tempdir | 1 | Create Names For Temporary Files |
| fileRename | 1 | File renames |
| fileRemove | 1 | Delete files or directories file.remove attempts to remove the files named in its argument. |
| unlinks | 1 | Delete files or directories |
| erase | 1 | delete all contents in target directory |
| isSystemDir | 1 | Check of the given directory path is a system internal directory or not. |
| dataUri | 1 | read file as data URI string |
| bytes | 1 | create a in-memory byte stream object |
| file_allocate | 1 | |
| openTargzip | 1 |
03 Members
String)| Name | Type | Description |
|---|---|---|
filePath | String | - |
String(), Object)Gets the relative pathname relative to a directory.
In case the two paths are on different file systems, for instance, C:/foo/bar/ and D:/foo/, the method returns pathname as is.
| Name | Type | Description |
|---|---|---|
pathname | String() | A character String Of the pathname To be converted into an relative pathname. |
relativeTo | Object | A character string of the reference pathname. |
Returns a character string of the relative pathname.
Object, Environment)Utility function to extract information about files on the user's file systems.
What constitutes a ‘file’ is OS-dependent but includes directories. (However, directory names must not include a trailing backslash or slash on Windows.) See also the section in the help for file.exists on case-insensitive file systems.
The file 'mode’ follows POSIX conventions, giving three octal digits summarizing the permissions for the file owner, the owner's group and for anyone respectively. Each digit is the logical or of read (4), write (2) and execute/search (1) permissions.
See files For how file paths With marked encodings are interpreted.
File modes are probably only useful On NTFS file systems, And it seems all three digits refer To the file's owner. The execute/search bits are set for directories, and for files based on their extensions (e.g., ‘.exe’, ‘.com’, ‘.cmd’ and ‘.bat’ files). file.access will give a more reliable view of read/write access availability to the R process.
UTF-8-encoded file names Not valid in the current locale can be used.
Junction points And symbolic links are followed, so information Is given about the file/directory To which the link points rather than about the link.
| Name | Type | Description |
|---|---|---|
x | Object | character vectors containing file paths. Tilde-expansion is done: see path.expand. |
Double: File size In bytes. For missing file, this function will returns a negative number -1; and the file is exists on the filesystem, this function returns ZERO(empty file) or a positive number.
Construct Path to File
Construct the path to a file from components in a platform-independent way.
The implementation is designed to be fast (faster than ‘paste’) as this Function is() used extensively In R itself. It can also be used for environment paths such as 'PATH’ and 'R_LIBS’ with ‘fsep = .Platform$path.sep’. Trailing Path separators are invalid For Windows file paths apart from '/’ and ‘d:/’ (although some functions/utilities do accept them), so a trailing '/’ or ‘\’ is removed there.
| Name | Type | Description |
|---|---|---|
x | Object | character vectors. Long vectors are not supported. |
fsep | String | the path separator to use (assumed to be ASCII). The components are by default separated by ‘/’ (not ‘\’) on Windows. |
A character vector of the arguments concatenated term-by-term and separated by 'fsep’ if all arguments have positive length; otherwise, an empty character vector (unlike 'paste’).
An element Of the result will be marked (see 'Encoding’ as UTF-8 If run In a UTF-8 locale (When marked inputs are converted To UTF-8) Or if an component of the result Is marked as UTF-8, Or as Latin-1 in a non-Latin-1 locale.
Extract File Information
Utility function to extract information about files on the user's file systems.
| Name | Type | Description |
|---|---|---|
files | Object | The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character. |
env | Boolean | - |
a object list with slots:
file.copy works in a similar way to file.append but with the arguments in the natural order for copying. Copying to existing destination files is skipped unless overwrite = TRUE. The to argument can specify a single existing directory. If copy.mode = TRUE file read/write/execute permissions are copied where possible, restricted by ‘umask’. (On Windows this applies only to files. ) Other security attributes such as ACLs are not copied. On a POSIX filesystem the targets of symbolic links will be copied rather than the links themselves, and hard links are copied separately. Using copy.date = TRUE may or may not copy the timestamp exactly (for example, fractional seconds may be omitted), but is more likely to do so as from R 3.4.0.
| Name | Type | Description |
|---|---|---|
from | String() | - |
To | String() | - |
These functions return a logical vector indicating which operation succeeded for each of the files attempted. Using a missing value for a file or path name will always be regarded as a failure.
Object, Environment)Get file extension name
| Name | Type | Description |
|---|---|---|
path | Object | the file path string |
returns a file extension suffix name in lower case, if there is no extension name or path string is empty, then empty string value will be returned.
copy file contents in one dir to another dir
| Name | Type | Description |
|---|---|---|
from | String | - |
To | String | - |
env | Environment | - |
Open an interface to a specific local filesystem location
| Name | Type | Description |
|---|---|---|
dir | String | - |
fs | IFileSystemEnvironment | the logical filesystem view |
env | Environment | - |
a directory model
String(), Environment)Express File Paths in Canonical Form
Convert file paths to canonical form for the platform, to display them in a user-understandable form and so that relative and absolute paths can be compared.
| Name | Type | Description |
|---|---|---|
fileNames | String() | character vector of file paths. |
envir | Environment | - |
Return the R Home Directory
Return the R home directory, or the full path to a component of the R installation.
The R home directory is the top-level directory of the R installation being run.
The R home directory Is often referred To As R_HOME, And Is the value Of an environment variable Of that name In an R session. It can be found outside an R session by R RHOME.
dirname returns the part of the path up to but excluding the last path separator, or "." if there is no path separator.
| Name | Type | Description |
|---|---|---|
fileNames | Object | character vector, containing path names. |
List the Files in a Directory/Folder
| Name | Type | Description |
|---|---|---|
dir | Object | a character vector of full path names; the default corresponds to the working directory, or zip folder object if this parameter is a file stream r zip file path. |
pattern | String() | an optional regular expression/wildcard expression. Only file names which match the regular expression will be returned. |
String, Boolean, Boolean, IFileSystemEnvironment)List the Files in a Directory/Folder
| Name | Type | Description |
|---|---|---|
dir | String | a character vector of full path names; the default corresponds to the working directory, |
fullNames | Boolean | - |
recursive | Boolean | - |
String())| Name | Type | Description |
|---|---|---|
filenames | String() | character vector giving file paths. |
file_ext returns the file (name) extensions (excluding the leading dot). (Only purely alphanumeric extensions are recognized.)
removes all of the path up to and including the last path separator (if any).
| Name | Type | Description |
|---|---|---|
fileNames | String() | character vector, containing path names. |
withExtensionName | Boolean | option for config keeps the extension suffix in the name or not, removes the file suffix name by default. |
removes all of the invalid character for the windows file name
| Name | Type | Description |
|---|---|---|
strings | Object | - |
alphabetOnly | Boolean | - |
replacement | String | all of the invalid character for the windows file name will be replaced as this placeholder character |
env | Boolean | - |
file.exists returns a logical vector indicating whether the files named by its argument exist. (Here ‘exists’ is in the sense of the system's stat call: a file will be reported as existing only if you have the permissions needed by stat. Existence can also be checked by file.access, which might use different permissions and so obtain a different result. Note that the existence of a file does not imply that it is readable: for that use file.access.) What constitutes a ‘file’ is system-dependent, but should include directories. (However, directory names must not include a trailing backslash or slash on Windows.) Note that if the file is a symbolic link on a Unix-alike, the result indicates if the link points to an actual file, not just if the link exists. Lastly, note the different function exists which checks for existence of R objects.
| Name | Type | Description |
|---|---|---|
files | Object | character vectors, containing file names or paths. |
fs | Boolean | a virtual filesystem object |
this function returns FALSE if the given files value is NULL
These functions provide a low-level interface to the computer's file system.
The ... arguments are concatenated to form one character string: you can specify the files separately or as one vector. All of these functions expand path names: see path.expand. (file.exists silently reports false for paths that would be too long after expansion: the rest will give a warning.)
file.create creates files with the given names if they do Not already exist And truncates them if they do. They are created with the maximal read/write permissions allowed by the 'umask’ setting (where relevant). By default a warning is given (with the reason) if the operation fails.
Case-insensitive file systems are the norm on Windows and macOS, but can be found on all OSes (for example a FAT-formatted USB drive is probably case-insensitive).
These functions will most likely match existing files regardless of case on such file systems: however this is an OS function and it is possible that file names might be mapped to upper or lower case.
Symbolic links in the sense of POSIX file systems do not exist on Windows: however, NTFS file systems support two similar concepts.
There are ‘junctions’ (or ‘junction points’), unfortunately without a public API: a Windows version of the Unix concept of mounting one directory on another. A junction can link directories located on the same or different local volumes of the same computer, but cannot link to a network drive. Function Sys.junction creates one or more junctions: to should either specify a single existing directory or a set of non-existent file paths of the same length as from. (Command mklink /J can also be used to create junctions.)
A version of symbolic linking to files/directories was implemented more recently, and file.symlink makes use of that interface. However, it has restrictions which are crippling. First, the user needs permission to make symbolic links, and that permission is not normally granted except to Administrator accounts (note: not users with Administrator rights): further many users report that whereas the Policy Editor appears to be able to grant such rights, the API still reports insufficient permissions. Second, the interface needs to know if from is a file or a directory (and it need not yet exist): we have implemented this to allow linking from a directory only if it currently exists.
Care is needed with removing a junction (and most likely also a symbolic link): many tools will remove the target and its contents.
Always check the return value of these functions when used in package code. This is especially important for file.rename, which has OS-specific restrictions (and note that the session temporary directory is commonly on a different file system from the working directory): it is only portable to use file.rename to change file name(s) within a single directory.
There is no guarantee that these functions will handle Windows relative paths of the form ‘d:path’: try ‘d:./path’ instead. In particular, ‘d:’ is not recognized as a directory. Nor are ‘\\?\’ prefixes (and similar) supported.
Most of these functions accept UTF-8 filepaths not valid in the current locale.
User error in supplying invalid file names (and note that ‘foo/’ and ‘foo\’ are invalid on Windows) has undefined consequences.
| Name | Type | Description |
|---|---|---|
x | Object | character vectors, containing file names or paths. |
showWarnings | Boolean | logical; should the warnings on failure be shown? |
These functions return a logical vector indicating which operation succeeded for each of the files attempted. Using a missing value for a file or path name will always be regarded as a failure. If showWarnings = True, file.create will give a warning For an unexpected failure.
String, Boolean, Boolean, String)dir.create creates the last element of the path, unless recursive = TRUE. Trailing path separators are discarded. On Windows drives are allowed in the path specification and unless the path is rooted, it will be interpreted relative to the current directory on that drive. mode is ignored on Windows.
One of the idiosyncrasies of Windows Is that directory creation may report success but create a directory with a different name, for example dir.create("G.S.") creates '"G.S"’. This is undocumented, and what are the precise circumstances is unknown (and might depend on the version of Windows). Also avoid directory names with a trailing space.
There is no guarantee that these functions will handle Windows relative paths of the form ‘d:path’: try ‘d:./path’ instead. In particular, ‘d:’ is not recognized as a directory. Nor are \\?\ prefixes (and similar) supported.
UTF-8-encoded dirnames Not valid in the current locale can be used.
| Name | Type | Description |
|---|---|---|
path | String | a character vector containing a single path name. |
showWarnings | Boolean | logical; should the warnings on failure be shown? |
recursive | Boolean | logical. Should elements of the path other than the last be created? If true, Like the Unix command mkdir -p. |
mode | String | the mode To be used On Unix-alikes: it will be coerced by as.octmode. For Sys.chmod it Is recycled along paths. |
dir.create and Sys.chmod return invisibly a logical vector indicating if the operation succeeded for each of the files attempted. Using a missing value for a path name will always be regarded as a failure. dir.create indicates failure if the directory already exists. If showWarnings = TRUE, dir.create will give a warning for an unexpected failure (e.g., not for a missing value nor for an already existing component for recursive = TRUE).
String())dir.exists returns a logical vector of TRUE or FALSE values (without names).
| Name | Type | Description |
|---|---|---|
paths | String() | character vectors containing file or directory paths. Tilde expansion (see path.expand) is done. |
Read some or all text lines from a connection.
| Name | Type | Description |
|---|---|---|
con | Object | a connection object or a character string. |
stream | Int32 | if this options is config as TRUE, means this function will returns a lazy load data pipeline. default value of this option is FALSE, which means this function will returns a character vector which contains all data content lines directly. |
strict | Encodings | this function will returns an empty string vector if not in strict mode |
Reads all characters from the current position to the end of the given stream.
| Name | Type | Description |
|---|---|---|
con | Object | - |
encoding | Encodings | - |
Write text lines to a connection.
If the con is a character string, the function calls file to obtain a file connection which is opened for the duration of the function call.
If the connection Is open it Is written from its current position. If it Is Not open, it Is opened For the duration Of the Call In "wt" mode And Then closed again.
Normally writeLines Is used With a text-mode connection, And the Default separator Is converted To the normal separator For that platform (LF On Unix/Linux, CRLF On Windows). For more control, open a binary connection And specify the precise value you want written To the file In sep. For even more control, use writeChar On a binary connection.
useBytes Is for expert use. Normally (when false) character strings with marked encodings are converted to the current encoding before being passed to the connection (which might do further re-encoding). useBytes = TRUE suppresses the re-encoding of marked strings so they are passed byte-by-byte to the connection: this can be useful When strings have already been re-encoded by e.g. iconv. (It Is invoked automatically For strings With marked encoding "bytes".)
| Name | Type | Description |
|---|---|---|
text | Object | A character vector, or a serials of compatible interface for get text contents. |
con | Object | A connection Object Or a character String. |
sep | String | character string. A string to be written to the connection after each line of text. |
getwd returns an absolute filepath representing the current working directory of the R process;
String(), Environment)setwd(dir) is used to set the working directory to dir.
| Name | Type | Description |
|---|---|---|
dir | String() | A character String: tilde expansion will be done. |
envir | Environment | - |
Save a R# object list in json file format
| Name | Type | Description |
|---|---|---|
list | Object | - |
file$ | String | - |
read list from a given json file
| Name | Type | Description |
|---|---|---|
file | String | A json file path |
mode | String | The value mode of the loaded list object in |
ofVector | Boolean | Is a list of vector? |
envir | Encodings | - |
open a *.gz file for make file data read/write
| Name | Type | Description |
|---|---|---|
description | String | - |
stream object for read/write data
Object, Environment)check of the target gz file data is corrupted or not
| Name | Type | Description |
|---|---|---|
file | Object | - |
env | Environment | - |
true for no error in gzfile, false means the given gz file is corrupted
String, FileModeDescriptor, Boolean, IFileSystemEnvironment)Functions to create, open and close connections, i.e., "generalized files", such as possibly compressed files, URLs, pipes, etc.
stdin for stdinput stream, andstdout for stdoutput stream.| Name | Type | Description |
|---|---|---|
description | String | character string. A description of the connection: see ‘Details’. |
open | FileModeDescriptor | character string. A description of how to open the connection (if it should be opened initially). See section ‘Modes’ for possible values. |
repo | Boolean | this function will open an internal block stream if this repository reference has been specificed. |
Read binary data from or write binary data to a connection or raw vector.
| Name | Type | Description |
|---|---|---|
con | Object | A connection Object Or a character String naming a file Or a raw vector. |
n | Object | numeric. The (maximal) number of records to be read. You can use an over-estimate here, but not too large as storage is reserved for n items. |
size | Int32 | Integer.The number Of bytes per element In the Byte stream. The Default, NA_integer_, uses the natural size. Size changing Is Not supported For raw And complex vectors. |
signed | Int32 | logical. Only used for integers of sizes 1 and 2, when it determines if the quantity on file should be regarded as a signed or unsigned integer. |
endian | Boolean | The endian-ness ("big" Or "little") Of the target system For the file. Using "swap" will force swapping endian-ness. |
what | endianness | Either an object whose mode will give the mode of the vector to be read, or a character vector of length one describing the mode: one of "numeric", "double", "integer", "int", "logical", "complex", "character", "raw". |
| Name | Type | Description |
|---|---|---|
[object] | Object | - |
con | Object | - |
size | Int32 | - |
endian | endianness | - |
useBytes | Boolean | - |
env | list | - |
Object, Environment)close connections, i.e., “generalized files”, such as possibly compressed files, URLs, pipes, etc.
| Name | Type | Description |
|---|---|---|
con | Object | a connection. |
String, Environment)open a zip file
| Name | Type | Description |
|---|---|---|
file | String | - |
env | Environment | - |
a folder liked list object
decompression of a gzip file and get the deflate file data stream.
| Name | Type | Description |
|---|---|---|
file | Object | the file path or file stream data. |
tmpfileWorker | String | using tempfile for process the large data file which its file length is greater then the memorystream its upbound capacity. |
env | Environment | - |
create a new buffer object
this function usually works for the http web services.
| Name | Type | Description |
|---|---|---|
type | BufferObjects | the r-sharp internal buffer data type |
mime | String | the data mime-type for http response, some buffer object type may required of this parameter for specific the correct mine content type. |
tempfile returns a vector of character strings which can be used as names for temporary files.
The length of the result is the maximum of the lengths of the three arguments; values of shorter arguments are recycled.
The names are very likely To be unique among calls To tempfile In an R session And across simultaneous R sessions (unless tmpdir Is specified). The filenames are guaranteed Not To be currently In use.
The file name Is made by concatenating the path given by tmpdir, the pattern String, a random String In hex And a suffix Of fileext.
By Default, tmpdir will be the directory given by tempdir(). This will be a subdirectory of the per-session temporary directory found by the following rule when the R session Is started. The environment variables TMPDIR, TMP And TEMP are checked in turn And the first found which points to a writable directory Is used: If none succeeds the value Of R_USER (see Rconsole) Is used. If the path To the directory contains a space In any Of the components, the path returned will use the shortnames version Of the path. Note that setting any Of these environment variables In the R session has no effect On tempdir(): the per-session temporary directory Is created before the interpreter Is started.
| Name | Type | Description |
|---|---|---|
pattern | Object | a non-empty character vector giving the initial part of the name. |
tmpdir | String | a non-empty character vector giving the directory name |
fileext | Object | a non-empty character vector giving the file extension |
a character vector giving the names of possible (temporary) files. Note that no files are generated by tempfile.
Boolean)| Name | Type | Description |
|---|---|---|
check | Boolean | logical indicating if |
the path of the per-session temporary directory.
File renames
| Name | Type | Description |
|---|---|---|
from | String | character vectors, containing file names Or paths. |
to | String | character vectors, containing file names Or paths. |
env | Environment | - |
file.remove attempts to remove the files named in its argument. On most Unix platforms ‘file’ includes empty directories, symbolic links, fifos and sockets. On Windows, ‘file’ means a regular file and not, say, an empty directory.
| Name | Type | Description |
|---|---|---|
x | String() | character vectors, containing file names or paths. |
String(), Environment)Delete files or directories
this function is the alias name of the function file.remove.
| Name | Type | Description |
|---|---|---|
x | String() | - |
String, Environment)delete all contents in target directory
| Name | Type | Description |
|---|---|---|
dir | String | - |
String)Check of the given directory path is a system internal directory or not.
The system directories are:
| Name | Type | Description |
|---|---|---|
dir | String | - |
Object, Environment)read file as data URI string
| Name | Type | Description |
|---|---|---|
file | Object | the file path |
Object, Environment)create a in-memory byte stream object
| Name | Type | Description |
|---|---|---|
byts | Object | - |
env | Environment | - |
String, IFileSystemEnvironment)String, Environment)