Skip to content

Latest commit

 

History

History
830 lines (505 loc) · 15.9 KB

FileSystem.md

File metadata and controls

830 lines (505 loc) · 15.9 KB

dw::io::file::FileSystem


Simple File module to do basic operations

Index

Functions

Name Description
baseNameOf Returns the base name of this file
contentOf Returns the content of the given path
copyTo Copies the specified binary into the given path.
exists Returns true if the file exits
extensionOf Returns the extension of the file with the dot.
home Returns the Path value of the home directory.
kindOf Returns the file type. "File" or "Folder" or null if it doesn't exits
ls Returns the list child file path
mimeTypeOf Tries to guess the mimeType of the given Path
mkdir Creates the a folder in the given path. And returns the path.
nameOf Returns the name of this file
parentOf Returns the path to the parent folder of a given file.
path Creates a valid path with the specified parts
rm Removes the file at the given location. Returns true if the file or folder was removed.
separator Returns the system-dependent default name-separator character, represented as a string for convenience.
tmp Returns the Path value of the tmp directory.
toUrl Transform the specified file path into a valid Url
tree
unzipTo Unzips the specified file into the given directory
wd Returns the Path value of the working directory.
writeTo
zipInto Zips the specified collection of files into the given zip path.

Variables

Name Description
FILE_EXTENSIONS: { _: String } Mapping between file extension and MimeType

Types

Name Description
FileKind The two kind of file
Path

Functions

baseNameOf ↑↑

baseNameOf(path: Path): String

Returns the base name of this file

Parameters
Name Description
path
Example

This example shows how the baseNameOf behaves under different inputs.

Source
%dw 2.0
output application/json
---
dw::io::file::FileSystem::baseNameOf("/tmp/a/test.json")
Output
"test"

contentOf ↑↑

contentOf(path: Path): Binary

Returns the content of the given path

Parameters
Name Description
path The path of the file
Example

This example shows how the contentOf behaves under different inputs.

Source
%dw 2.0
output application/json
---
contentOf("/tmp/foo/bar.txt") as String {encoding: "UTF-8"}
Output
"Hello"

copyTo ↑↑

copyTo(binary: Binary, path: Path): Number

Copies the specified binary into the given path.

Parameters
Name Description
binary The content to be written
path The path were is going to be written
Example

This example shows how the writeTo behaves under different inputs.

Source
%dw 2.0
output application/json
---
copyTo( "Hello" as Binary {encoding: "UTF-8"}, "/tmp/foo/bar.txt")
Output
5

exists ↑↑

exists(path: Path): Boolean

Returns true if the file exits

Parameters
Name Description
path The path to be checked
Example

This example shows how the exists behaves under different inputs.

Source
%dw 2.0
output application/json
---
dw::io::file::FileSystem::exists("/tmp")
Output
true

extensionOf ↑↑

extensionOf(path: Path): String | Null

Returns the extension of the file with the dot.

Parameters
Name Description
path The path
Example

This example shows how the extensionOf behaves under different inputs.

Source
%dw 2.0
output application/json
---
%dw 2.0
 import * from dw::io::file::FileSystem
 output application/json
 ---
 {
   a: extensionOf(path("/tmp","foo.txt")),
   b: extensionOf(path("/tmp","foo.html")),
   c: extensionOf(path("/tmp","foo.json")),
   d: extensionOf(tmp()) //Directory should return null
 }
Output
{
   "a": ".txt",
   "b": ".html",
   "c": ".json",
   "d": null
 }

home ↑↑

home(): Path

Returns the Path value of the home directory.


kindOf ↑↑

kindOf(path: Path): FileKind | Null

Returns the file type. "File" or "Folder" or null if it doesn't exits


ls(folder: Path): Array

Returns the list child file path

Parameters
Name Description
folder The of the contained file
Example

This example shows how the ls behaves under different inputs.

Source
%dw 2.0
output application/json
---
  ls("/tmp")
Output
["/tmp/foo.txt","/tmp/dw-input-buffer-0.tmp","/tmp/dw-output-buffer-0.tmp"]

ls(folder: Path, filterExpr: Regex): Array

Return the list of child elements of the specified path. That matches the specified regex pattern

Parameters
Name Description
folder The of the contained file
filterExpr The file expression regex to be used on each name
Example

This example shows how to filter tmp file to all the one that contains the text 'dw'

Source
%dw 2.0
output application/json
---
  ls("/tmp", /dw/)
Output
["/tmp/dw-input-buffer-0.tmp","/tmp/dw-output-buffer-0.tmp"]

mimeTypeOf ↑↑

mimeTypeOf(path: Path): String | Null

Tries to guess the mimeType of the given Path

Parameters
Name Description
path The path
Example

This example shows how the mimeTypeOf behaves under different inputs.

Source
%dw 2.0
output application/json
---
dw::io::file::FileSystem::mimeTypeOf("/tmp/test.json")
Output
"application/json"

mkdir ↑↑

mkdir(path: Path): Path | Null

Creates the a folder in the given path. And returns the path.

Parameters
Name Description
path The path to be created
Example

This example shows how the mkdir behaves under different inputs.

Source
%dw 2.0
output application/json
---
mkdir("/tmp/a")
Output
"/tmp/a"

nameOf ↑↑

nameOf(path: Path): String

Returns the name of this file


parentOf ↑↑

parentOf(path: Path): String | Null

Returns the path to the parent folder of a given file.

Parameters
Name Type Description
path Path The path to the file of interest.
Example

This example shows how the parentOf behaves under different inputs.

Source
%dw 2.0
output application/json
import parentOf from dw::io::file::FileSystem
---
parentOf("tmp/someDir/someFile.txt")
Output
"tmp/someDir"

path ↑↑

path(basePath: Path, part: String): Path

Creates a valid path with the specified parts

Parameters
Name Description
basePath The base path
part The child path to append
Example

This example shows how the path behaves under different inputs.

Source
%dw 2.0
output application/json
---
path("/tmp/a","b")
Output
"/tmp/a/b"

path(basePath: Path, part: String, part2: String): Path

Creates a valid Path with the specified parts

Parameters
Name Description
basePath The base path
part Child Part 1
part2 Child Part 2
Example

This example shows how the path behaves under different inputs.

Source
%dw 2.0
output application/json
---
path("/tmp", "a","b")
Output
"/tmp/a/b"

path(basePath: Path, part: String, part2: String, part3: String): Path

Creates a valid Path with the specified parts

Parameters
Name Description
basePath The base path
part Child Part 1
part2 Child Part 2
part3 Child Part 3
Example

This example shows how the path behaves under different inputs.

Source
%dw 2.0
output application/json
---
path("/tmp", "a","b","c")
Output
"/tmp/a/b/c"

path(basePath: Path, parts: Array): Path

Creates a valid Path with the specified parts

Parameters
Name Description
basePath The base path
parts All that child part
Example

This example shows how the path behaves under different inputs.

Source
%dw 2.0
output application/json
---
path("/tmp", ["a","b","c"])
Output
"/tmp/a/b/c"

rm(path: Path): Boolean

Removes the file at the given location. Returns true if the file or folder was removed.

If the path is a file it will delete everything recursively.

Parameters
Name Description
path The path to delete
Example

This example shows how the rm behaves under different inputs.

Source
%dw 2.0
import rm from dw::io::file::FileSystem
output application/json
---
rm("/home/dw/toRemove")
Output
true

separator ↑↑

separator(): String

Returns the system-dependent default name-separator character, represented as a string for convenience.

Example

This example shows how the separator behaves under different inputs.

Source
%dw 2.0
output application/json
import separator from dw::io::file::FileSystem
---
// Will return "/" for Unix-based systems, and "\" for Windows-based systems.
separator()
Output
"/"

tmp ↑↑

tmp(): Path

Returns the Path value of the tmp directory.


toUrl ↑↑

toUrl(path: Path): String

Transform the specified file path into a valid Url

Parameters
Name Description
path The path to be converted
Example

This example shows how the toUrl behaves under different inputs.

Source
%dw 2.0
output application/json
---
toUrl( "/tmp/Application Test")
Output
"file:/tmp/Application%20Test"

tree ↑↑

tree(path: Path): Array


unzipTo ↑↑

unzipTo(zipPath: Path, targetDirectory: Path): Path

Unzips the specified file into the given directory

Parameters
Name Description
zipPath
targetDirectory
Example

This example shows how the unzipTo behaves under different inputs.

Source
%dw 2.0
import * from dw::io::file::FileSystem
output application/json
---
fileToUnzip unzipTo path(tmp(), "dw_io_test" ,"outputZip")
Output
"/tmp/dw_io_test/outputZip"

wd(): Path

Returns the Path value of the working directory.


writeTo ↑↑

writeTo(path: Path, binary: Binary): Number


zipInto ↑↑

zipInto(paths: Array, zipPath: Path): Path

Zips the specified collection of files into the given zip path.

Parameters
Name Description
paths The array of paths to be zipped
zipPath The zip file path to be created
Example

This example shows how the zipInto behaves under different inputs.

Source
%dw 2.0
import * from dw::io::file::FileSystem
output application/json
---
[path(tmp(),"dw_io_test")] zipInto path(tmp(),"outputZip.zip")
Output
"/tmp/outputZip.zip"

Variables

FILE_EXTENSIONS: { _: String } ↑↑

Mapping between file extension and MimeType


Types

FileKind ↑↑

The two kind of file

Definition

"File" | "Folder"

Path ↑↑

Definition

String