in reply to Function to sweep a file tree
The following is a JavaScript program I wrote which sweeps a directory tree and creates an unicode text file which contains all the file names in a directory. Runs under Windows XP/7/8/10. It should be saved with a .JS extension.
Some tasks are easier in perl, while others are easier in JavaScript. And although JavaScript does not require it, you can use $ signs in the variables, which I did, to make it more perlish. Lol
// 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 co +ntain the exact time when this program was executed. try // Write file... { // Here we try to open the text file for writing using the CreateTex +tFile() method. // The first "true" argument states that the output file will be ove +rwritten. // 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\nSAV +ED 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($Fi +le) + ($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') WScrip +t.Echo($msg); WScript.Quit($errorcode); }
Here's a sample output produced by this program:
1593733100343 C:\WINDOWS +1578861460 ----D--- Cursors +1578861460 ----D--- Driver Cache +1578861460 ----D--- Help +1578861460 ----D--- Media +1578861460 ----D--- NLDRV +1578861460 ----D--- Provisioning +1578861460 ----D--- Resources +1578861460 ----D--- WinSxS +1578861460 ----D--- ehome +1578861460 ----D--- msagent +1578861460 ----D--- mui +1578861460 ----D--- pchealth +1578861460 ----D--- repair +1578861460 ----D--- security +1578861460 ----D--- system +1578861460 ----D--- system32 +1578861460 ----D--- twain_32 +1578861460 -H--D--- inf +1578861460 R-S-D--- Fonts +1578861772 -HS-D--- Installer +1578862010 ----D--- Registration +1578862048 ----D--- srchasst +1578862072 R---D--- Web +1578862902 ----D--- SoftwareDistribution +1578863966 ----D--- SHELLNEW +1578868700 ----D--- RegisteredPackages +1578869970 -H--D--- $NtUninstallKB888111WXPSP2$ +1578888466 -H--D--- $MSI31Uninstall_KB893803v2$ +1578899930 ----D--- Microsoft.NET +1578899952 R-S-D--- assembly +1579014252 -HS-D--- Recycled +1579107350 -H--D--- $NtUninstallWIC$ +1579107390 -H--D--- $NtUninstallKB894476$ +1579107394 -H--D--- $NtUninstallKB909394$ +1579150942 -H--D--- $NtUninstallWdf01009$ +1579150946 -H--D--- $NtUninstallwinusb0200$ +1579151832 -H--D--- $NtUninstallKB942288-v3$ +1591766356 -H--D--- PIF +1593196520 ----D--- apppatch +1593196520 ----D--- ime +1593196520 ----D--- peernet +1593227820 ----D--- temp 00000000000 1578898458 -----A-- ativpsrm.bin 00000000000 1593196522 -----A-- Sti_Trace.log 00000000036 1578862012 -----A-- vb.ini 00000000037 1578862012 -----A-- vbaddin.ini 00000000038 1271437200 -----A-- avisplitter.ini 00000000042 1580688388 -----A-- boxworld.ini 00000000048 1593666958 -----A-- wiaservc.log 00000000080 1141128000 -----A-- explorer.scf 00000000109 1580687782 -----A-- WCHESS.INI 00000000116 1592963652 -----A-- NeroDigital.ini 00000000140 1578896578 -----A-- gife.ini 00000000159 1593729422 -----A-- wiadebug.log 00000000203 1593625384 -----A-- GIBW.INI 00000000227 1578871656 -----A-- system.ini 00000000335 1580305028 -----A-- nsreg.dat 00000000376 1578864048 -----A-- ODBC.INI 00000000383 1578893046 -----A-- SNAP.INI 00000000706 1578898910 -----A-- SS_SLIDE.INI 00000000749 1578862066 RH---A-- WindowsShell.Manifest 00000000808 1593521706 -----A-- win.ini 00000001272 1141128000 -----A-- Blue Lace 16.bmp 00000001405 1141128000 -----A-- msdfmap.ini 00000002048 1593729420 --S--A-- bootstat.dat 00000003626 1593666956 -----A-- WindowsUpdate.log 00000004079 1593229038 -----A-- IF40LE.INI 00000004161 1578862110 -----A-- ODBCINST.INI 00000009522 1141128000 -----A-- Zapotec.bmp 00000010752 1141128000 -----A-- hh.exe 00000015360 1141128000 -----A-- TASKMAN.EXE 00000016730 1141128000 -----A-- FeatherTexture.bmp 00000017062 1141128000 -----A-- Coffee Bean.bmp 00000017336 1141128000 -----A-- Gone Fishing.bmp 00000017362 1141128000 -----A-- Rhododendron.bmp 00000018944 1141128000 -----A-- vmmreg32.dll 00000025600 1141128000 -----A-- twunk_32.exe 00000026582 1141128000 -----A-- Greenstone.bmp 00000026680 1141128000 -----A-- River Sumida.bmp 00000035363 1312180208 -----A-- atiogl.xml 00000038065 1075075200 -----A-- EPHEM12.HLP 00000048680 1141128000 -HS----- winnt.bmp 00000048680 1141128000 -HS----- winnt256.bmp 00000049680 1141128000 -----A-- twunk_16.exe 00000050688 1141128000 -----A-- twain_32.dll 00000059392 1463460046 -----A-- STARFIELD.SCR 00000062976 1141128000 -----A-- SPGRMR.DLL 00000064512 1463460042 -----A-- CYLFRAC.SCR 00000065832 1141128000 -----A-- Santa Fe Stucco.bmp 00000065954 1141128000 -----A-- Prairie Wind.bmp 00000065978 1141128000 -----A-- Soap Bubbles.bmp 00000069120 1141128000 -----A-- NOTEPAD.EXE 00000069632 1115163808 -----A-- ALCMTR.EXE 00000075264 1463459448 -----A-- 3DTEXT.SCR 00000086016 1153516476 -----A-- SOUNDMAN.EXE 00000094784 1141128000 -----A-- twain.dll 00000128000 1463460046 -----A-- MATRIX.SCR 00000130048 1141128000 -----A-- SOFTKBD.DLL 00000146432 1141128000 -----A-- regedit.exe 00000149504 1463460046 -----A-- SSSTARS.SCR 00000220160 1141128000 -----A-- MSCANDUI.DLL 00000223744 1463460044 -----A-- BUTTERFLIES.SCR 00000250880 1141128000 -----A-- SPTIP.DLL 00000256192 1141128000 -----A-- winhelp.exe 00000260640 1075075200 -----A-- EPHEM12.SCR 00000282624 1118694920 -----A-- SLIDESHOW.SCR 00000283648 1141128000 -----A-- winhlp32.exe 00000316640 1578868716 -----A-- WMSysPr9.prx 00000626397 1593382024 -----A-- setupapi.log 00000720896 1579922638 -----A-- iun6002.exe 00001032192 1141128000 -----A-- explorer.exe 00001183744 1159470048 -----A-- RtlUpd.exe 00002157568 1160606578 -----A-- MicCal.exe 00002808832 1146777996 -----A-- ALCWZRD.EXE 00002879488 1147820666 -----A-- SkyTel.exe 00009709568 1146778514 -----A-- RTLCPL.EXE 00016269312 1162259394 -----A-- RTHDCPL.EXE
- Comment on Re: Function to sweep a file tree
- Download Code