Filepath

$filepath

$filepath implements utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths.

Go reference: https://pkg.go.dev/path/filepath

Helpers

const baseName = $filepath.base("C:\Users\user\Downloads\file.mkv");
console.log(baseName); // file.mkv

const dirName = $filepath.dir("C:\Users\user\Downloads\file.mkv");
console.log(dirName); // C:\Users\user\Donwloads

const extName = $filepath.ext("C:\Users\user\Downloads\file.mkv");
console.log(extName); // .mkv

const joinedPath = $filepath.join("C:", "Users", "user", "subdir", "file.txt");
console.log(joinedPath); // C:\Users\user\subdir\file.txt

const [dir, file] = $filepath.split("C:\Users\user\Downloads\file.mkv");
console.log(dir, file); // C:\Users\user\Downloads, file.mkv

const globResults = $filepath.glob("C:\Users\user\Downloads", "*.txt");
console.log(globResults); // test.txt, test2.txt

const isMatch = $filepath.match("*.txt", "test.txt");
console.log(isMatch); // true

const isAbsPath = $filepath.isAbs("C:\Users\user\Downloads\file.mkv");
console.log(isAbsPath); // true

// Test toSlash and fromSlash
const slashPath = $filepath.toSlash("C:\Users\user\Downloads\file.mkv");
console.log(slashPath); // C:/Users/user/Downloads/file.mkv

const fromSlashPath = $filepath.fromSlash(slashPath);
console.log(fromSlashPath); // C:\Users\user\Downloads\file.mkv

Walk directories

// walk calls 'lstat' on each file path it encounters, which can be slower
$filepath.walk("C:\Users\user\Downloads", (path, info, err) => {
	if (err) {
		console.log("Walk error:", path, err);
		return; // Continue walking
	}
	
	// We can skip directories
	if (info.isDir() && info.name() === "ignoredDir") {
		console.log("Skipping directory:", path);
		return $filepath.skipDir;
	}
	
	console.log("Walk path:", path, "isDir:", info.isDir());
	return; // Continue walking
});

// walkDir is more efficient
$filepath.walkDir("C:\Users\user\Downloads", (path, d, err) => {
	if (err) {
		console.log("WalkDir error:", path, err);
		return; // Continue walking
	}
	
	// We can skip directories
	if (d.isDir() && d.name() === "ignoredDir") {
		console.log("Skipping directory:", path);
		return $filepath.skipDir;
	}
	
	console.log("WalkDir path:", path, "isDir:", d.isDir());
	return; // Continue walking
});

Last updated