-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path3-paths.js
More file actions
32 lines (26 loc) · 1.52 KB
/
3-paths.js
File metadata and controls
32 lines (26 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// The diagram below shows the different names for parts of a file path on a Unix operating system
// ┌─────────────────────┬────────────┐
// │ dir │ base │
// ├──────┬ ├──────┬─────┤
// │ root │ │ name │ ext │
// " / home/user/dir / file .txt "
// └──────┴──────────────┴──────┴─────┘
// (All spaces in the "" line should be ignored. They are purely for formatting.)
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
/*
const slashIndex = filePath.indexOf("/");
const dir =filePath.slice(slashIndex + 0, 45) ;
const ext =filePath.slice(slashIndex + 49, 53);
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);
*/
let dirDirectory = filePath.slice(filePath.indexOf("/"),filePath.lastIndexOf("/"));
let extDirectory = filePath.slice(filePath.lastIndexOf("/"));
console.log(`The dir part of the file is ${dirDirectory}.`);
console.log(`The ext part of the file is ${extDirectory}.`);
// https://www.google.com/search?q=slice+mdn