Skip to content

Commit

Permalink
Update Filesystem.nts
Browse files Browse the repository at this point in the history
  • Loading branch information
MurageKabui authored Jan 5, 2025
1 parent 8f7b706 commit 1b66439
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions Examples/Filesystem.nts
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
/*
* Script Name : Filesystem.nts
* Date : Sun Nov 05 2023 15:11:55 GMT+0300 (East Africa Time)
* PhoneDo Version : 1.3.2
* Description : Reading a text file from file system.
* Author : PhoneDo
* License : None
*/

// Define the target file base path
const sPath = 'file:///storage/emulated/0/Documents/';

// Define the target filename
const sFileName = 'DOS.txt';

const sContents =
await fs.readTextFile(sPath, sFileName);

// Print the file content to stdout
console.log(sContents + '\n');

// Or read the text aloud using utter
// etc..
await utter.speak(
sContents,
0.95,
1.0,
'en-us-x-iol-network'
);
Script Name : FsSandboxDemo.nts
Date : Tue Dec 24 2022 12:30:00 GMT+0300 (East Africa Time)
PhoneDo Version : 1.3.2
Description : Demonstrates File System Operations
Author : PhoneDo
License : None
*/

async function exampleFileOperations() {
try {
// 1. Create a directory
console.log("Creating a new directory...");
await fs.makeDir(undefined, "SandboxFolder");
console.success("Directory created successfully!");

// 2. Check if directory exists
console.log("Checking if directory exists...");
const dirExists = await fs.dirExist("/storage/emulated/0/Documents/SandboxFolder/");
console.success(`Directory exists: ${dirExists}`);

// 3. Create a new file
console.log("Creating a new text file...");
await fs.makeFile("/storage/emulated/0/Documents/SandboxFolder/", "example.txt");
console.success("File created successfully!");

// 4. Write content to the file
console.log("Writing content to the file...");
await fs.writeFile("/storage/emulated/0/Documents/SandboxFolder/", "example.txt", "This is sandboxed content.");
console.success("Content written to file successfully!");

// 5. Read the file
console.log("Reading the file...");
const fileContent = await fs.readTextFile("/storage/emulated/0/Documents/SandboxFolder/", "example.txt");
console.success("File content:", fileContent);

// 6. Check if the file exists
console.log("Checking if file exists...");
const fileExists = await fs.fileExist("/storage/emulated/0/Documents/SandboxFolder/", "example.txt");
console.success(`File exists: ${fileExists}`);

// 7. Delete the file
console.log("Deleting the file...");
await fs.deleteFile("/storage/emulated/0/Documents/SandboxFolder/", "example.txt");
console.success("File deleted successfully!");

// 8. Delete the directory
console.log("Deleting the directory...");
await fs.deleteDir(undefined, "SandboxFolder");
console.success("Directory deleted successfully!");
} catch (error) {
console.error("An error occurred:", error);
exit(1, error);
}
}

await exampleFileOperations();
exit(0);

0 comments on commit 1b66439

Please sign in to comment.