Skip to content

Instantly share code, notes, and snippets.

@FibreFoX
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FibreFoX/28bed4e021a84f02618a to your computer and use it in GitHub Desktop.
Save FibreFoX/28bed4e021a84f02618a to your computer and use it in GitHub Desktop.
Remove thumbs.db (annoying windows-specific thumbnail-cache) from all subfolders with PhantomJS (1.9.8)
var fs = require('fs');
// --------------------------------------
// JS-fixes
if (!String.prototype.endsWith) {
console.log("fixing String-prototype (endsWith)");
Object.defineProperty(String.prototype, 'endsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString, position) {
position = position || this.length;
position = position - searchString.length;
var lastIndex = this.lastIndexOf(searchString);
return lastIndex !== -1 && lastIndex === position;
}
});
}else{
console.log("String-prototype WASNT fixed (endsWith)");
}
if (!String.prototype.startsWith) {
console.log("fixing String-prototype (startsWith)");
Object.defineProperty(String.prototype, 'startsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
}
});
}else{
console.log("String-prototype WASNT fixed (startsWith)");
}
// --------------------------------------
var possibleFileNames = ["thumbs.db","thumbs.DB","Thumbs.db"];
function scanAndWorkOnDirectoryRecursive(folder){
try {
//console.log("Reading folder:", folder);
fs.list(folder).forEach(function workOnFile(filename){
var target = folder + fs.separator + filename;
// recursive
if( fs.isDirectory(target) & fs.isReadable(target) ){
// dont work on special folders
if( filename !== "." && filename !== ".."){
checkIfIsThumbsDBAndRemoveIt(target);
scanAndWorkOnDirectoryRecursive(target);
}
}
});
} catch(e) {
console.log("Folder is not readable", folder, e);
}
}
function checkIfIsThumbsDBAndRemoveIt(folderpath){
possibleFileNames.forEach(function(thumbDBfileName){
var possibleTarget = folderpath + fs.separator + thumbDBfileName;
if(fs.isFile(possibleTarget) && fs.isReadable(possibleTarget) && fs. isWritable(possibleTarget)){
console.log("Removing thumbs.db file: " + possibleTarget);
fs.remove(possibleTarget);
}
});
}
console.log("Start scan for Thumbs.db ", new Date());
scanAndWorkOnDirectoryRecursive(fs.workingDirectory);
console.log("Finished scan for Thumbs.db ", new Date());
phantom.exit(0);
@FibreFoX
Copy link
Author

FibreFoX commented Jan 7, 2015

Just copy this file to the root-folder where all subfolders should be cleared of that windows-specific thumbs.db-file, could be improved to get target-folders as arguments, but i just needed this 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment