-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchangeFileName.js
More file actions
33 lines (33 loc) · 958 Bytes
/
Copy pathchangeFileName.js
File metadata and controls
33 lines (33 loc) · 958 Bytes
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
/** @param {NS} ns */
export async function main(ns) {
const originalName=ns.args[0];
const targetName=ns.args[1];
let found=[false,false];
for (const script of ns.ls("home")){
if (script==originalName)found[0]=true
if (script==targetName)found[1]=true
}
if (!found[0]){
ns.tprint("file "+originalName+" not found.")
ns.exit();
}
if (found[1]){
ns.tprint("file with target name already exists")
ns.exit();
}
{
const contents=ns.read(originalName);
ns.write(targetName,contents);
if (!ns.rm(originalName))ns.tprint("file removal was unsuccessful");
ns.tprint("file succesfully renamed");
}
for (const script of ns.ls("home")){
let contents = ns.read(script)
if (contents.includes(originalName)){
contents=contents.replaceAll("'"+originalName+"'","'"+targetName+"'");
contents=contents.replaceAll('"'+originalName+'"','"'+targetName+'"');
ns.write(script,contents,"w");
}
}
ns.tprint("name mentions updated");
}