Created
April 7, 2025 16:48
-
-
Save gala8y/09d8203f3b50d19e241bbebb67c8ddff to your computer and use it in GitHub Desktop.
Obsidian dataviewjs script to remove "Pasted image " from all image files in a vault (button)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```dataviewjs | |
// // Obsidian dataviewjs script to remove "Pasted image " from all image files in a vault (button) | |
// // ai generated | |
// Create a button to run the rename operation | |
const btn = this.container.createEl('button', {text: 'Rename Pasted Images'}); | |
btn.addEventListener('click', async () => { | |
const vault = app.vault; | |
const files = vault.getFiles(); | |
const imageFiles = files.filter(file => | |
file.name.startsWith("Pasted image ") && | |
file.extension.match(/png|jpg|jpeg|gif|svg|webp/i) | |
); | |
let renamedCount = 0; | |
for (const file of imageFiles) { | |
const newName = file.name.replace("Pasted image ", ""); | |
const folderPath = file.parent ? file.parent.path : ''; | |
const newPath = folderPath ? `${folderPath}/${newName}` : newName; | |
try { | |
await app.fileManager.renameFile(file, newPath); | |
renamedCount++; | |
} catch (error) { | |
console.error(`Failed to rename ${file.name}:`, error); | |
} | |
} | |
// Create a new result paragraph or update existing one | |
const resultEl = this.container.createEl('p'); | |
resultEl.setText(`Renamed ${renamedCount} images.`); | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment