// // This JavaScript program creates an M3U audio playlist // of MP3 WMV and WAV files in the current directory // and all subdirectories individually. // Written by Zsolt Nagy-Perge in April 2021, // Pensacola, Fla. // try { var FSO = new ActiveXObject('Scripting.FileSystemObject'); } catch(e) { Exit("ERROR:\nCannot access File System."); } SELF = WScript.ScriptFullName; CURDIR = cut(SELF, "\\", 0x10010); MYNAME = $b; ABOUT = cut(ReadFile(SELF), "\r\n\r\n", 0x10).replace(/\/\/\s*/g, ""); Exit_If_Cancel("M3U Playlist Creator", MYNAME + "\n\n" + ABOUT + "\nClick on OK to run this script..."); WalkDIR(CURDIR); Alert("DONE"); Exit(); ////////////////////////////////////////////////// function Alert(s) { WScript.Echo(s); } function Exit(s) { if (typeof(s) == "string") Alert(s); WScript.Quit(0); } ////////////////////////////////////////////////// // This function reads all files in a directory // and processes each file, then does the same with // each subdirectory as well. // Usage: ARRAY = ReadDIR(PATH) // function WalkDIR(PATH) { var FC, EXT, File, FullName, FileName, FILELIST = [], OUTPUT_FILE = PATH + "\\PLAY.M3U", F = FSO.GetFolder(PATH); for (FC = new Enumerator(F.files); !FC.atEnd(); FC.moveNext()) { FullName = FC.item(); FileName = cut(FullName, "\\", 0x10001); EXT = cut(FileName, ".", 0x10001).toUpperCase(); if (EXT == "MP3" || EXT == "WMV" || EXT == "WAV") FILELIST.push(FullName); if (FILELIST.length > 1) CreateFile(OUTPUT_FILE, "#EXTM3U\r\n" + FILELIST.join("\r\n")); } FILELIST = 0; // Check each sub-directory... for (FC = new Enumerator(F.SubFolders); !FC.atEnd(); FC.moveNext()) WalkDIR(FC.item()); } ////////////////////////////////////////////////// // This function splits string into two parts // along the first occurrence of substring. // Returns the first part if CMD is 0x10. // Returns the second part if CMD is 1. // If substring is not found, returns the original // string if CMD is 0x100. Ignores case when // CMD is 0x1000. Starts searching from end of // string when CMD is 0x10000. // function cut(STR, SUB, CMD) { if (typeof(CMD) === "undefined") CMD = 0x111; STR += ""; SUB += ""; var P; if (CMD & 0x1000) P = (CMD & 0x10000) ? STR.toUpperCase().lastIndexOf(SUB) : STR.toUpperCase().indexOf(SUB); else P = (CMD & 0x10000) ? STR.lastIndexOf(SUB) : STR.indexOf(SUB); $a = $b = ""; if (P < 0) return (CMD & 256) ? STR : ""; $a = STR.substr(0, P); $b = STR.slice(P + SUB.length); return (CMD & 16 ? $a : '') + (CMD & 1 ? $b : ''); } ////////////////////////////////////////////////// // v2021.4.11 // Creates and overwrites a file and with a string. // Retuns 0 on success or 1 if an error occurred. // Usage: INTEGER = CreateFile(FILENAME, STRING) // function CreateFile(FILENAME, STRING) { try { var FSO = new ActiveXObject("Scripting.FileSystemObject"); var F = FSO.CreateTextFile(FILENAME, 1, 0); // // Usage: FILE_HANDLE = FSO.CreateTextFile(FILENAME, OVERWRITE, FORMAT) // OVERWRITE: 0=Don't overwrite file if it exists // 1=Overwrite file if it already exists // FORMAT: 0=ASCII // 1=UNICODE // F.Write(STRING); F.Close(); return 0; } catch (e) {} return 1; } ////////////////////////////////////////////////// // v2021.4.11 // Returns the entire contents of a text or binary file. // Usage: STRING = ReadFile(FILENAME) // function ReadFile(FILENAME) { var F, FSO, DATA = ""; try { FSO = new ActiveXObject("Scripting.FileSystemObject"); F = FSO.OpenTextFile(FILENAME, 1); // Read UNICODE format DATA = F.ReadAll(); F.Close(); return DATA; } catch (e) {} return ""; } ////////////////////////////////////////////////// // // This function will display an OK-OR-CANCEL popup // window with some message. If the user clicks // Cancel, the script terminates. // Usage: Exit_If_Cancel(TITLE, MESSAGE) // function Exit_If_Cancel(Title, Msg) { var A = 0; try { var WshShell = WScript.CreateObject("WScript.Shell"); A = WshShell.Popup(Msg, 1000, Title, 33); } catch(e) { Alert(Msg); } if (A == 2) Exit(); } //////////////////////////////////////////////////