From 445d24a0fbf81e1c1efdf5df462560bcb6a36d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Loyer?= Date: Thu, 9 May 2024 12:33:26 +0200 Subject: [PATCH] add filesystem cookbook --- data/cookbook/file-system/00-stdlib.ml | 26 ++++++++++++++++++++++++++ data/cookbook/tasks.yml | 4 ++++ 2 files changed, 30 insertions(+) create mode 100644 data/cookbook/file-system/00-stdlib.ml diff --git a/data/cookbook/file-system/00-stdlib.ml b/data/cookbook/file-system/00-stdlib.ml new file mode 100644 index 0000000000..a88c0889e8 --- /dev/null +++ b/data/cookbook/file-system/00-stdlib.ml @@ -0,0 +1,26 @@ +--- +packages: [] +discussion: | + - **Reference:** All the presented functions are available in the [Sys module reference](https://v2.ocaml.org/api/Stdlib.Sys.html) +--- +(* Testing the presence of a file/directory *) +let () = if Sys.file_exists "my_file" then + print_string "The file/directory exists." +(* Testing if file or directory which exists is a directory. *) +let () = if Sys.is_directory "my_file" then + print_string "It is a directory." + else + print_string "It is a file (regular or not)." +(* Deleting a file *) +let () = Sys.remove "my_file" +(* Renaming a file *) + let () = Sys.rename "my_file" "new_name" +(* Changing the current directory *) + let () = Sys.chdir "new_directory_path" +(* Deleting a directory *) + let () = Sys.rmdir "directory_name" +(* Listing a directory *) + let () = + let file_array = Sys.readdir "directory_name" in + Array.iter (fun filename -> Printf.printf "%s\n" filename) file_array + diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 5ef3052485..cb2b697b4b 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -8,6 +8,10 @@ categories: description: > Use ANSI escape codes for cursor location, font and colour in video text terminal emulators. +- title: File System + tasks: + - title: Miscellaneous File System Operations + slug: file-system - title: Compilation & Linking tasks: - title: Compile and Link to a Bundled C Library