Repository navigation
Expand file tree
/
Copy pathscripting.asc
More file actions
36 lines (29 loc) · 1.52 KB
/
Copy pathscripting.asc
File metadata and controls
36 lines (29 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
33
34
35
36
// Using Ascend for scripts: running programs, files and folders, CSV,
// hashing and dates.
// Run a command through the shell and capture what it prints.
result := run("echo hello from the shell")
print("exit code", result.code, "printed:", trim(result.stdout))
// Work in a scratch folder inside the system's temporary folder.
folder := path_join(temp_dir(), "ascend-scripting-" + uuid()[0:8])
make_dir(folder)
try {
// Write a CSV file, then read it back and summarize it.
csv_file := path_join(folder, "prices.csv")
write_file(csv_file, csv_stringify([["item", "price"], ["tea", 2.5], ["cake", 4], ["coffee", 3.25]]))
table := csv_parse(read_file(csv_file))
map<string, f64> prices = {row[0]: f64(row[1]) for row in table[1:]}
printf("%d items, %.2f in total, the most expensive is %s\n", len(prices), sum(values(prices)),
max_by(keys(prices), item => prices[item]))
// More files in a subfolder, then a look at everything.
make_dir(path_join(folder, "notes"))
write_file(path_join(folder, "notes", "todo.txt"), "buy milk\nwrite more Ascend\n")
for (string file : walk_dir(folder)) {
path := path_join(folder, file)
printf("%-16s %3d bytes sha256 %s...\n", file, file_size(path), sha256(read_file(path))[0:12])
}
} finally {
remove_dir(folder) // clean up, even if something above failed
}
print("the scratch folder is gone:", !is_dir(folder))
today := now()
printf("today is %04d-%02d-%02d, day %d of the year\n", today.year, today.month, today.day, today.yearday)