Directory Example
Demonstration of Basic Operations of Directory
Code:
import std.fs.*
main() {
let testDirPath: Path = Path("./testDir")
let subDirPath: Path = Path("./testDir/subDir")
if (exists(testDirPath)) {
remove(testDirPath, recursive: true)
}
/* Recursively creates a directory and "./testDir/subDir". */
Directory.create(subDirPath, recursive: true)
if (exists(subDirPath)) {
println("The directory './testDir/subDir' is successfully created recursively in current directory.")
}
/* Creates a temporary directory in "./testDir". */
let tempDirPath: Path = Directory.createTemp(testDirPath)
if (exists(tempDirPath)) {
println("The temporary directory is created successfully in directory './testDir'.")
}
/* Moves "subDir" to the temporary directory and renames it "subDir_new". */
let newSubDirPath: Path = tempDirPath.join("subDir_new")
rename(subDirPath, to: newSubDirPath)
if (exists(newSubDirPath) && !exists(subDirPath)) {
println("The directory './testDir/subDir' is moved successfully to the temporary directory and renamed 'subDir_new'.")
}
/* Copies "subDir_new" to "./testDir" and renames it "subDir". */
copy(newSubDirPath, to: subDirPath, overwrite: false)
if (exists(subDirPath) && exists(newSubDirPath)) {
println("The directory 'subDir_new' is copied successfully to directory './testDir' and renamed 'subDir'.")
}
remove(testDirPath, recursive: true)
return 0
}
Running result:
The directory './testDir/subDir' is successfully created recursively in current directory.
The temporary directory is created successfully in directory './testDir'.
The directory './testDir/subDir' is moved successfully to the temporary directory and renamed 'subDir_new'.
The directory 'subDir_new' is copied successfully to directory './testDir' and renamed 'subDir'.