Delete a directory of files

I want to write a script that will delete all the files in a given directory that isn’t under the Shortcuts app. Any suggestions for where to start?

By “Under the Shortcuts App” I assume you mean in the Shortcuts folder in iCloud Drive.

If that’s your goal, you currently can’t do that with Shortcuts alone. You’d need to use Scriptable to access other folders (by creating a file bookmark in Scriptable) and going from there.

1 Like

That what I thought. Do you have any Scriptable samples that I can learn from? I don’t know how to specify the path, etc.

Scriptable has a bunch of examples and documentation built in. Here’s the only script I’m currently using that accesses the file system, but it works with Drafts to append text to a file with information for the path being passed in by Drafts:

// write to comlog based on pre-determined file path
// invoked via Drafts

// collect data from URL parameters passed from Drafts
const client = JSON.parse(args.queryParameters.client);
const addition = args.queryParameters.addition;
const successURL = args.queryParameters["x-success"];

// set up basics
const fm = FileManager.iCloud();
const root = fm.bookmarkedPath("iCloud Drive");

// get the path and the file content passed in from Drafts
const path = root + "/• Work/clients/" + client.nickname + " — " + client.number + "/working files/" + client.nickname + "";

// prepend to existing comlog, or create one if it doesn't exist
if (fm.fileExists(path)) {
	console.log("the comlog exists, so I'm going to prepend the new info");
	await fm.downloadFileFromiCloud(path);
	let content = fm.readString(path);
	let newContent = addition + content;
	fm.writeString(path, newContent);
} else {
	console.log("the comlog doesn't exist, so I'm creating it");
	const clientDirectory = root + "/• Work/clients/" + client.nickname + " — " + client.number + "/working files/";
	fm.createDirectory(clientDirectory, true);
	fm.writeString(path, addition);
	fm.addTag(path, "comlog");	

// return to Drafts;