// This JavaScript program reads directory contents // and saves the sorted list in a file in Unicode format. $PATH = "C:\\WINDOWS"; $RECURSIVE = 0; $OUTPUT_FILE = "Z:\\Output.txt"; $SAVE_DIR_SIZE = 0; $SAVE_FULLPATH = 0; // PROGRAM STARTS HERE: $T = (new Date()).getTime(); // Get current time in milliseconds $PATH_LENGTH = $PATH.length + (($PATH.slice(-1) == '\\') ? 0 : 1); try { $FSO = new ActiveXObject('Scripting.FileSystemObject'); } catch(e) { Quit(1, 'Cannot access File System.'); } if ($PATH.charAt(1) == ':') { $DRIVE = $PATH.substr(0, 2); if (!$FSO.DriveExists($DRIVE)) Quit(2, 'Drive does not exist - ' + $PATH); if (!($FSO.GetDrive($DRIVE)).IsReady) Quit(3, 'Drive is not ready - ' + $PATH); } OUTPUT = []; // create an array for file data DIR($PATH); // sweep directory and collect data OUTPUT = OUTPUT.sort(); // sort all files by size (this line can be removed) OUTPUT.unshift($PATH); // Save PATH starting point OUTPUT.unshift($T); // The first line of the output file will contain the exact time when this program was executed. try // Write file... { // Here we try to open the text file for writing using the CreateTextFile() method. // The first "true" argument states that the output file will be overwritten. // The second "true" argument states that the output text file will be in Unicode format. var $F = $FSO.CreateTextFile($OUTPUT_FILE, true, true); $F.Write(OUTPUT.join("\r\n")); // Write contents of the array into the file. $F.Close(); } catch (e) { Quit(4, 'Cannot save file - ' + $OUTPUT_FILE); } Quit(0, "SUCCESS!!!!!\n\nDIRECTORY CONTENTS OF\n\n" + $PATH + "\n\nSAVED SUCCESSFULLY TO:\n\n" + $OUTPUT_FILE); // This function reads the contents of one directory and saves the // contents in the OUTPUT array. function DIR($path) { var $F = $FSO.GetFolder($path), $FC, $File, $FullName; // First we record all the sub-directories. for ($FC = new Enumerator($F.SubFolders); !$FC.atEnd(); $FC.moveNext()) { $FullName = $FC.item(); $File = $FSO.GetFolder($FullName); OUTPUT.push('+' + ($SAVE_DIR_SIZE ? '00000000000 ' : '') + DateOf($File) + AttributesOf($File) + ($SAVE_FULLPATH ? $FullName : Shorten($FullName))); if ($RECURSIVE) DIR($FullName); } // Then we record all the files. for ($FC = new Enumerator($F.files); !$FC.atEnd(); $FC.moveNext()) { $FullName = $FC.item(); $File = $FSO.GetFile($FullName); OUTPUT.push('-' + SizeOf($File) + DateOf($File) + AttributesOf($File) + ($SAVE_FULLPATH ? $FullName : Shorten($FullName))); } } // This function returns the last modified date of a file or directory. function DateOf($f) { return ('0000000000' + ($f.DateLastModified * 1)).slice(-13).substr(0, 10) + ' '; } // This function returns the size of a file. function SizeOf($f) { return ('00000000000' + $f.Size + ' ').slice(-12); } // This function returns the file attributes in a nice formatted way. function AttributesOf($f) { var $A = $f.Attributes; return ($A & 1 ? 'R' : '-') // Read-only + ($A & 2 ? 'H' : '-') // Hidden file + ($A & 4 ? 'S' : '-') // System file + ($A & 8 ? 'V' : '-') // Volume label (attribute is read-only) + ($A & 16 ? 'D' : '-') // Directory + ($A & 32 ? 'A' : '-') // Archive + ($A & 1024 ? 'L' : '-') // Link or shortcut file + ($A & 2048 ? 'C' : '-') + ' '; // Compressed file } // Removes the first part of a fullpath. function Shorten($n) { return ($n + '').slice($PATH_LENGTH); } // Terminates the program and maybe displays a message. function Quit($errorcode, $msg) { if (typeof($msg) == 'string') WScript.Echo($msg); WScript.Quit($errorcode); }