Skip to content
Create a gist now

Instantly share code, notes, and snippets.

PrintEverything
<job id="main">
<runtime>
<description>
Print everything in a folder (and its subfolders, recursively)
to the default printer. (c) 2011 mstephenson6, please share
with friends, bsd license
</description>
</runtime>
<resource id="WelcomeText">PrintEverything - easy way to print all files in a folder and its subfolders</resource>
<script language="JScript">
// return an array of all interesting files (as Strings) in the Folder argument,
// and look through subfolders too. to be called by 'the client'
function getAllFiles(Folder) {
var ToReturn = new ActiveXObject("Scripting.Dictionary"); // seems to be the easiest collection to work with under WSH's JS
__getAllFiles(Folder, ToReturn);
return (new VBArray((ToReturn).Keys())).toArray(); // nasty, but now it's a vanilla JS array. thanks msdn!
}
// recursive, does all the work, kicked off from the 'public' getAllFiles()
function __getAllFiles(Folder, ToAdd) {
var fileEnum = new Enumerator(Folder.Files)
for (fileEnum.moveFirst(); !fileEnum.atEnd(); fileEnum.moveNext()) {
var currFile = fileEnum.item();
var currPath = currFile.Path.toString();
var currType = currFile.Type.toString();
var fileIsScript = (currType.indexOf("Script") != -1);
var fileIsHidden = (currFile.attributes & 2);
var fileIsSpotlightStore = (currPath.indexOf("Spotlight-V100") != -1); // had a thumbdrive going between osx and win
if (!(fileIsScript || fileIsHidden || fileIsSpotlightStore)) {
ToAdd.add(currPath, ""); // no values needed, we'll look for only keys later
}
}
var subFolderEnum = new Enumerator(Folder.SubFolders);
for (subFolderEnum.moveFirst(); !subFolderEnum.atEnd(); subFolderEnum.moveNext()) {
var currSubFolder = subFolderEnum.item();
__getAllFiles(currSubFolder, ToAdd);
}
}
</script>
<script language="JScript">
// execution begins here
var fso = WScript.CreateObject("Scripting.FileSystemObject");
var wshShell = WScript.CreateObject("WScript.Shell");
var rootFolder = fso.GetFolder(wshShell.CurrentDirectory);
wshShell.Popup(getResource("WelcomeText"), 0, "PrintEverything", 0);
var allFiles = getAllFiles(rootFolder);
var dirStatsMsg = "We are in the folder:\n" + rootFolder.Path + "\n\n";
dirStatsMsg += "In total, we found " + allFiles.length + " files to send to the default printer\n\n";
dirStatsMsg += "Do you want to send them to the default printer now?\n";
var responseInt = wshShell.Popup(dirStatsMsg, 0, "PrintEverything", 4); // a yes/no box that waits for user response
if (responseInt == 7) { // no
wshShell.Popup("We didn't send anything to the default printer. Exiting.", 0, "PrintEverything", 0);
WScript.Quit();
}
var appShell = WScript.CreateObject("Shell.Application");
var printsSuccess = 0;
var printsFail = 0;
for (var i = 0; i < allFiles.length; i++ ) {
var currFilePath = allFiles[i];
var currFileDir = currFilePath.substring(0, currFilePath.lastIndexOf("\\"));
var currFileName = currFilePath.substring(currFilePath.lastIndexOf("\\") + 1, currFilePath.length);
var objFolder = appShell.NameSpace(currFileDir);
var objItem = objFolder.ParseName(currFileName);
var verbCollection = objItem.Verbs();
//verbCollection.item(1).DoIt(); // not sure if we can count on &Print always being at this spot
var invokedPrintVerb = false;
var verbEnum = new Enumerator(verbCollection);
for (verbEnum.moveFirst(); !verbEnum.atEnd(); verbEnum.moveNext()) {
var currVerb = verbEnum.item();
var verbName = new String(currVerb.Name);
if (verbName == "&Print") {
currVerb.DoIt(); // best method name ever
invokedPrintVerb = true;
printsSuccess++;
break;
}
}
if (!invokedPrintVerb) {
wshShell.Popup("Couldn't figure out how to print " + currFilePath, 0, "PrintEverything", 0);
printsFail++;
}
}
var finalMsg = "Done! Sent " + printsSuccess + " to the default printer. ";
if (printsFail > 0) {
finalMsg += "Failed to send " + printsFail + ". ";
}
finalMsg += "Exiting.";
wshShell.Popup(finalMsg, 0, "PrintEverything", 0);
</script>
</job>
@geofrey
geofrey commented Jun 8, 2015

I was sure there had to be a simpler way to enumerate subfolders, but even the MSDN reference (which took forever to find) does it that way. Why can't it just be an array?

@mstephenson6
Owner

"Why can't it just be an array?" went through my mind the whole time I wrote this, but specifically targeting a locked down Win XP machine, this seemed to be the only way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.