PDA

View Full Version : Delete all folders except some



jwil1
18-02-2009, 01:15 PM
I want to delete all the user profiles on several machines via a batch file.

Is there a way to tell the batch file to delete all folders in C:\Documents and Settings EXCEPT All Users, Default User and Administrator??

TIA :)

dyewitness
18-02-2009, 04:24 PM
Not easily using DOS commands, but this script would accomplish it:


//************************************************** ************************
function FolderDeleter() {
try {
this.excludedFolders = new ActiveXObject("Scripting.Dictionary");
this.excludedFolders.add("C:\\Documents and Settings\\Administrator", true);
this.excludedFolders.add("C:\\Documents and Settings\\All Users", true);
this.excludedFolders.add("C:\\Documents and Settings\\Default User", true);

this.fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
this.rootFolder = this.fileSystemObject.GetFolder("C:\\Documents and Settings");
} catch (exception) {
WScript.Echo("Error:");
WScript.Echo(exception.message);
WScript.Quit(1);
}
}

//************************************************** ************************
function FolderDeleter.prototype.deleteSubFolders() {
try {
var subFoldersEnum = new Enumerator(this.rootFolder.SubFolders);

while (!subFoldersEnum.atEnd()) {
var item = subFoldersEnum.item();

if (this.excludedFolders.exists(item.Path)) {
WScript.Echo("excluding: " + item.Path);
} else {
WScript.Echo("deleting: " + item.Path);
try {
this.fileSystemObject.DeleteFolder(item.Path, true)
} catch (exception) {
WScript.Echo("Error when deleting: " + item.Path);
WScript.Echo(exception.message);
}
}

subFoldersEnum.moveNext();
}
} catch (exception) {
WScript.Echo("Error:");
WScript.Echo(exception.number);
WScript.Echo(exception.descrption);
WScript.Quit(1);
}
}

//************************************************** ************************
function main() {

var folderDeleter = new FolderDeleter();
folderDeleter.deleteSubFolders();

WScript.Echo("done.");
}

//************************************************** ************************

main();



And to run the script (pays to run it with Administrator rights), save the above into a file named "FolderDeleter.js":


cscript [path-to-where-you-saved-it]\FolderDeleter.js