Path Example
Attributes of Different Path Instances
Prints the directory portion, full file name (with a file name extension), file name extension, or file name (without a file name extension) of a Path instance, and determines whether the Path instance is an absolute path or a relative path.
Code:
import std.fs.Path
main() {
let pathStrArr: Array<String> = [
// Absolute path
"/a/b/c",
"/a/b/",
"/a/b/c.cj",
"/a",
"/",
// Relative path
"./a/b/c",
"./a/b/",
"./a/b/c.cj",
"./",
".",
"123."
]
for (i in 0..pathStrArr.size) {
let path: Path = Path(pathStrArr[i])
// Prints the entire path string.
println("Path${i}: ${path.toString()}")
// Prints the directory path.
println("Path.directoryName: ${path.directoryName}")
// Prints the full file name of the path (with a file name extension).
println("Path.fileName: ${path.fileName}")
// Prints the path file name extension.
println("Path.extensionName: ${path.extensionName}")
// Prints the file name of the path (without a file name extension).
println("Path.fileNameWithoutExtension: ${path.fileNameWithoutExtension}")
// Prints the directory path and full file name after the path is split.
var (directoryName, fileName): (Option<Path>, Option<String>) = path.split()
println("Path.split: (${directoryName}, ${fileName})")
// Prints whether the path is an absolute path or a relative path.
println("Path.isAbsolute: ${path.isAbsolute()}; Path.isRelative: ${path.isRelative()}")
println()
}
return 0
}
Running result:
Path0: /a/b/c
Path.directoryName: Some(/a/b)
Path.fileName: Some(c)
Path.extensionName: None
Path.fileNameWithoutExtension: Some(c)
Path.split: (Some(/a/b), Some(c))
Path.isAbsolute: true; Path.isRelative: false
Path1: /a/b/
Path.directoryName: Some(/a/b)
Path.fileName: None
Path.extensionName: None
Path.fileNameWithoutExtension: None
Path.split: (Some(/a/b), None)
Path.isAbsolute: true; Path.isRelative: false
Path2: /a/b/c.cj
Path.directoryName: Some(/a/b)
Path.fileName: Some(c.cj)
Path.extensionName: Some(cj)
Path.fileNameWithoutExtension: Some(c)
Path.split: (Some(/a/b), Some(c.cj))
Path.isAbsolute: true; Path.isRelative: false
Path3: /a
Path.directoryName: Some(/)
Path.fileName: Some(a)
Path.extensionName: None
Path.fileNameWithoutExtension: Some(a)
Path.split: (Some(/), Some(a))
Path.isAbsolute: true; Path.isRelative: false
Path4: /
Path.directoryName: Some(/)
Path.fileName: None
Path.extensionName: None
Path.fileNameWithoutExtension: None
Path.split: (Some(/), None)
Path.isAbsolute: true; Path.isRelative: false
Path5: ./a/b/c
Path.directoryName: Some(./a/b)
Path.fileName: Some(c)
Path.extensionName: None
Path.fileNameWithoutExtension: Some(c)
Path.split: (Some(./a/b), Some(c))
Path.isAbsolute: false; Path.isRelative: true
Path6: ./a/b/
Path.directoryName: Some(./a/b)
Path.fileName: None
Path.extensionName: None
Path.fileNameWithoutExtension: None
Path.split: (Some(./a/b), None)
Path.isAbsolute: false; Path.isRelative: true
Path7: ./a/b/c.cj
Path.directoryName: Some(./a/b)
Path.fileName: Some(c.cj)
Path.extensionName: Some(cj)
Path.fileNameWithoutExtension: Some(c)
Path.split: (Some(./a/b), Some(c.cj))
Path.isAbsolute: false; Path.isRelative: true
Path8: ./
Path.directoryName: Some(.)
Path.fileName: None
Path.extensionName: None
Path.fileNameWithoutExtension: None
Path.split: (Some(.), None)
Path.isAbsolute: false; Path.isRelative: true
Path9: .
Path.directoryName: None
Path.fileName: Some(.)
Path.extensionName: None
Path.fileNameWithoutExtension: None
Path.split: (None, Some(.))
Path.isAbsolute: false; Path.isRelative: true
Path10: 123.
Path.directoryName: None
Path.fileName: Some(123.)
Path.extensionName: None
Path.fileNameWithoutExtension: Some(123)
Path.split: (None, Some(123.))
Path.isAbsolute: false; Path.isRelative: true
Operations such as Path Concatenation, Equality Check, and Normalization
Code:
import std.fs.*
main() {
let dirPath: Path = Path("./a/b/c")
if (!Directory.exists(dirPath)) {
Directory.create(dirPath, recursive: true)
}
let filePath: Path = dirPath.join("d.cj") // ./a/b/c/d.cj
if (filePath == Path("./a/b/c/d.cj")) {
println("filePath.join: success")
}
if (!File.exists(filePath)) {
File.create(filePath).close()
}
let curNormalizedPath: Path = Path(".").toCanonical()
let fileNormalizedPath: Path = Path("././././a/./../a/b/../../a/b/c/.././../../a/b/c/d.cj").toCanonical()
if (fileNormalizedPath == filePath &&
fileNormalizedPath.toString() == curNormalizedPath.toString() + "/a/b/c/d.cj") {
println("filePath.toCanonical: success")
}
Directory.delete(dirPath, recursive: true)
return 0
}
Running result:
filePath.join: success
filePath.toCanonical: success
Creating Files and Directories Using Path
Code:
import std.fs.*
main() {
let curPath: Path = Path("./")
let dirPath: Path = curPath.join("tempDir")
let filePath: Path = dirPath.join("tempFile.txt")
if (Directory.exists(dirPath)) {
Directory.delete(dirPath, recursive: true)
}
Directory.create(dirPath)
if (Directory.exists(dirPath)) {
println("Directory 'tempDir' is created successfully.")
}
File.create(filePath).close()
if (File.exists(filePath)) {
println("File 'tempFile.txt' is created successfully in directory 'tempDir'.")
}
Directory.delete(dirPath, recursive: true)
return 0
}
Running result:
Directory 'tempDir' is created successfully.
File 'tempFile.txt' is created successfully in directory 'tempDir'.