// File Manipulation Module // created by Volv // This file and contents can be reused and redistributed freely // // Functionality: // - binReadFile > Reads the binary contents of a file // > Parameters: // - filePath - The string path of the file to be read. // // - binWriteFile > Writes binary data to a file // > Parameters: // - binData - The binary data to be written to the file. // - filePath - The string path of the file to be written to. // // - asciiReadFile > Reads the contents of a file in the form of an ascii string. // > Parameters: // - filePath - The string path of the file to be read. // // - asciiWriteFile > Writes ascii data to a file // > Parameters: // - strData - The data in the form of a string to be written to the file. // - filePath - The string path of the file to be written to. // // - GetFileSize > Retrieves the size of a file in bytes // > Parameters: // - filePath - The string path of the file. function binReadFile(filePath) { var fstream = new ActiveXObject("ADODB.Stream"); fstream.Type = 1; fstream.Open; fstream.LoadFromFile(filePath); return fstream.Read; } function binWriteFile(binData, filePath) { var fstream = new ActiveXObject("ADODB.Stream"); fstream.Type = 1; fstream.Open; fstream.Write(binData); fstream.SaveToFile(filePath, 2); fstream.Close; } function asciiReadFile(filePath) { var fso = new ActiveXObject("Scripting.FileSystemObject"); var contents = fso.OpenTextFile(filePath, 1, 0).ReadAll(); return contents; } function asciiWriteFile(strData, filePath) { var fso = new ActiveXObject("Scripting.FileSystemObject"); var txtfile = fso.CreateTextFile(filePath, true); txtfile.Write(strData); txtfile.close(); } function GetFileSize(filePath) { var fso = new ActiveXObject("Scripting.FileSystemObject"); if(fso.FileExists(filePath)) { var file = fso.getFile(filePath); return file.size; } else { return -1; } } function GetFileExt(filePath) { var ExtLoc = str_reverse(filePath).indexOf('.'); if(ExtLoc < 0) { ExtLoc = 0; } var strExt = filePath.substr(filePath.length - ExtLoc, ExtLoc) return strExt; } var MIMETypes = new Array(); function GetMIMEType(filePath) { if(MIMETypes.length == 0) { asciiReadFile(MsgPlus.ScriptFilesPath + "\\mimetypes.txt"); MIMETypes = asciiReadFile(MsgPlus.ScriptFilesPath + "\\mimetypes.txt").split("\r\n"); } try { var WsShell = new ActiveXObject("WScript.Shell"); return WsShell.RegRead("HKCR\\." + GetFileExt(filePath) + "\\Content Type"); } catch(ex) { var mTypeIndex = LinearSearch('.' + GetFileExt(filePath), MIMETypes, true); if(mTypeIndex >= 0) { return MIMETypes[mTypeIndex].split(":")[1]; } else { return ''; } } } function GetPerceivedType(filePath) { try { var WsShell = new ActiveXObject("WScript.Shell"); return WsShell.RegRead("HKCR\\." + GetFileExt(filePath) + "\\PerceivedType"); } catch(ex) { return ''; } } function GetFileName(filePath) { var PathP = new Array(); PathP = filePath.split("\\"); return PathP[PathP.length-1]; } var OFN_ENABLESIZING = 0x800000; var OFN_EXPLORER = 0x80000; var OFN_FILEMUSTEXIST = 0x1000; var OFN_HIDEREADONLY = 0x4; var OFN_LONGNAMES = 0x200000; var OFN_PATHMUSTEXIST = 0x800; function BrowseForFile(strFilter, strCaption) { var OpenFileName = Interop.Allocate(88); var filter; var s_filter; var file; var s_file; var title; var s_title; var ret; var tdata; with (OpenFileName) { WriteDWORD(0, Size); // lStructSize WriteDWORD(4, 0); // hwndOwner WriteDWORD(8, 0); // hInstance filter = strFilter; s_filter = Interop.Allocate(2 * (filter.length + 1)); WriteMultiStringW(s_filter, filter); WriteDWORD(12, s_filter.DataPtr); // lpstrFilter WriteDWORD(16, 0); // lpstrCustomFilter WriteDWORD(20, 0); // nMaxCustomFilter WriteDWORD(24, 1); // nFilterIndex file = "" + Space(256); s_file = Interop.Allocate(2 * (file.length + 1)); s_file.WriteString(0, file); WriteDWORD(28, s_file.DataPtr); // lpstrFile WriteDWORD(32, file.length); // nMaxFile WriteDWORD(36, 0); // lpstrFileTitle WriteDWORD(40, 0); // nMaxFileTitle WriteDWORD(44, 0); // lpstrInitialDir if (strCaption) { title = "Select original file"; } else { title = "Select file"; } s_title = Interop.Allocate(2 * (title.length + 1)); s_title.WriteString(0, title); WriteDWORD(48, s_title.DataPtr); // lpstrTitle WriteDWORD(52, OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST); // flags WriteWORD(56, 0); // nFileOffset WriteWORD(58, 0); // nFileExtension WriteDWORD(60, 0); // lpstrDefExt WriteDWORD(64, 0); // lCustData WriteDWORD(68, 0); // lpfnHook WriteDWORD(72, 0); // lpTemplateName WriteDWORD(76, 0); // pvReserved WriteDWORD(80, 0); // dwReserved WriteDWORD(84, 0); // FlagsEx } ret = Interop.Call("comdlg32.dll", "GetOpenFileNameW", OpenFileName); if (ret == 0) { return 0; } else { return s_file.ReadString(0); } }