Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Created March 21, 2024 21:05
Show Gist options
  • Save cassidoo/c780a0045acb6b2c5b0b51b99ebda8b0 to your computer and use it in GitHub Desktop.
Save cassidoo/c780a0045acb6b2c5b0b51b99ebda8b0 to your computer and use it in GitHub Desktop.
A script for a Tauri application to iterate over files and generate new file names and not overwrite old ones
const { BaseDirectory, exists } = window.__TAURI__.fs;
async function doesFileExist(fileName) {
let fileExists = await exists(fileName + ".pdf", {
baseDir: BaseDirectory.Download,
});
let fileCounter = 0;
while (fileExists) {
fileCounter++;
fileExists = await exists(fileName + `-${fileCounter}.pdf`, {
baseDir: BaseDirectory.Download,
});
}
return fileCounter > 0 ? fileName + `-${fileCounter}` : fileName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment