welsonjs/lib/file.js

207 lines
6.3 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
2022-01-27 03:42:55 +00:00
// file.js
2023-09-19 06:55:45 +00:00
// with the PIPE based IPC (lib/pipe-ipc.js)
2020-06-28 14:22:57 +00:00
//
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
var LIB = require("lib/std");
2023-09-19 06:55:45 +00:00
var PipeIPC = require("lib/pipe-ipc");
2020-06-28 14:22:57 +00:00
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// fileExists
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function fileExists(FN) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FileExists(FN);
FSO = null;
return exists;
2022-01-27 03:42:55 +00:00
}
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// folderExists
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function folderExists(FN) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FolderExists(FN);
FSO = null;
return exists;
2022-01-27 03:42:55 +00:00
}
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// fileGet
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function fileGet(FN) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var file = FSO.GetFile(FN);
FSO = null;
return file;
2022-01-27 03:42:55 +00:00
}
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// readFile
// Read the conents of the pass filename and return as a string
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function readFile(FN, charset) {
2023-09-21 12:45:26 +00:00
if (typeof charset === "undefined") {
console.warn("CHARSET has not been passed. Force to UTF-8.");
charset = PipeIPC.CdoUTF_8;
}
2023-09-21 09:55:35 +00:00
var text = '';
var pipe = PipeIPC.connect("volatile");
2023-09-19 06:55:45 +00:00
pipe.setCharset(charset);
2023-09-21 12:45:26 +00:00
pipe.loadFromFile(FN, charset);
2023-09-21 09:55:35 +00:00
text += pipe.read();
pipe.destroy();
return text;
2022-01-27 03:42:55 +00:00
}
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// writeFile
// Write the passed content to named disk file
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function writeFile(FN, content, charset) {
2023-09-21 11:47:43 +00:00
if (typeof content === "undefined") {
2023-09-21 12:45:26 +00:00
console.warn("CONTENT has not been passed. Force to empty string.");
2023-09-21 11:47:43 +00:00
content = '';
}
if (typeof charset === "undefined") {
2023-09-21 12:45:26 +00:00
console.warn("CHARSET has not been passed. Force to UTF-8.");
2023-09-21 11:47:43 +00:00
charset = PipeIPC.CdoUTF_8;
}
2023-09-19 07:08:24 +00:00
var pipe = PipeIPC.connect("volatile");
2023-09-19 06:55:45 +00:00
pipe.setCharset(charset);
2023-09-19 07:08:24 +00:00
pipe.startRecorder(FN, PipeIPC.ForWriting);
2023-09-19 06:55:45 +00:00
pipe.write(content);
pipe.destroy();
return true;
2022-01-27 03:42:55 +00:00
}
2020-06-28 14:22:57 +00:00
2022-01-27 03:42:55 +00:00
/////////////////////////////////////////////////////////////////////////////////
// writeBinaryFile
/////////////////////////////////////////////////////////////////////////////////
function writeBinaryFile(FN, DATA) {
2020-12-07 08:33:12 +00:00
var BinaryStream = CreateObject("ADODB.Stream");
2023-09-19 06:55:45 +00:00
BinaryStream.Type = PipeIPC.adTypeBinary;
2020-12-07 08:33:12 +00:00
BinaryStream.Open();
BinaryStream.Write(DATA);
BinaryStream.SaveToFile(FN, adSaveCreateOverWrite);
BinaryStream.Close();
2022-01-27 03:42:55 +00:00
}
2020-12-07 08:33:12 +00:00
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// moveFile
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-11-25 02:46:20 +00:00
2022-01-27 03:42:55 +00:00
function moveFile(FROM, TO) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
2021-08-16 20:23:49 +00:00
var result = FSO.MoveFile(FROM, TO);
2020-11-26 04:46:44 +00:00
FSO = null;
2021-08-16 20:23:49 +00:00
return result;
2022-01-27 03:42:55 +00:00
}
2020-11-25 02:46:20 +00:00
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// createFolder
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-06-28 19:04:10 +00:00
2022-01-27 03:42:55 +00:00
function createFolder(FN) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
2021-08-16 20:23:49 +00:00
var result = FSO.CreateFolder(FN);
2020-11-26 04:46:44 +00:00
FSO = null;
2021-08-16 20:23:49 +00:00
return result;
2022-01-27 03:42:55 +00:00
}
2021-08-16 20:23:49 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// deleteFolder
2021-08-16 20:23:49 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function deleteFolder(FN) {
2021-08-16 20:23:49 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.DeleteFolder(FN);
FSO = null;
return result;
2022-01-27 03:42:55 +00:00
}
2020-06-28 19:04:10 +00:00
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
// deleteFile
2020-11-26 04:46:44 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-11-25 02:46:20 +00:00
2022-01-27 03:42:55 +00:00
function deleteFile(FN) {
2020-11-26 04:46:44 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
2021-08-16 20:23:49 +00:00
var result = FSO.DeleteFile(FN);
2020-11-26 04:46:44 +00:00
FSO = null;
2021-08-16 20:23:49 +00:00
return result;
2022-01-27 03:42:55 +00:00
}
2020-11-25 02:46:20 +00:00
2022-01-27 03:42:55 +00:00
/////////////////////////////////////////////////////////////////////////////////
// includeFile
2020-12-07 08:33:12 +00:00
/////////////////////////////////////////////////////////////////////////////////
2022-01-27 03:42:55 +00:00
function includeFile(FN) {
2024-03-20 02:13:58 +00:00
try {
eval(readFile(FN));
} catch (e) {
console.error(e.message, "in", FN);
}
2022-01-27 03:42:55 +00:00
}
2024-03-20 02:13:58 +00:00
/////////////////////////////////////////////////////////////////////////////////
// appendFile
/////////////////////////////////////////////////////////////////////////////////
2022-02-24 08:13:31 +00:00
function appendFile(FN, content, charset) {
2023-09-21 07:39:49 +00:00
var result = false;
2023-09-20 10:07:50 +00:00
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
2023-09-19 06:55:45 +00:00
pipe.setCharset(charset);
pipe.startRecorder(FN, PipeIPC.ForAppending);
2023-09-21 07:39:49 +00:00
result = pipe.write(content);
pipe.close();
return result;
2023-09-19 06:55:45 +00:00
}
2024-03-20 02:13:58 +00:00
/////////////////////////////////////////////////////////////////////////////////
// rotateFile
/////////////////////////////////////////////////////////////////////////////////
2023-09-19 06:55:45 +00:00
function rotateFile(FN, content, numOfLines, charset) {
2023-09-21 07:39:49 +00:00
var result = false;
2023-09-20 10:07:50 +00:00
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
2023-09-19 06:55:45 +00:00
pipe.setCharset(charset);
pipe.setMaxSentences(numOfLines);
pipe.startRecorder(FN, PipeIPC.ForAppending);
2023-09-21 07:39:49 +00:00
result = pipe.write(content);
pipe.close();
return result;
2022-02-24 08:13:31 +00:00
}
2022-01-27 03:42:55 +00:00
exports.fileExists = fileExists;
exports.folderExists = folderExists;
exports.fileGet = fileGet;
exports.readFile = readFile;
exports.writeFile = writeFile;
exports.writeBinaryFile = writeBinaryFile;
exports.moveFile = moveFile;
exports.createFolder = createFolder;
exports.deleteFolder = deleteFolder;
exports.deleteFile = deleteFile;
exports.includeFile = includeFile;
2022-09-22 05:41:01 +00:00
exports.appendFile = appendFile;
2023-09-19 06:55:45 +00:00
exports.rotateFile = rotateFile;
2022-09-22 05:41:01 +00:00
exports.CdoCharset = PipeIPC.CdoCharset;
2023-10-30 04:41:43 +00:00
2024-03-20 02:13:58 +00:00
exports.VERSIONINFO = "File IO Library (file.js) version 0.2.11";
exports.AUTHOR = "abuse@catswords.net";
2022-09-22 05:41:01 +00:00
exports.global = global;
exports.require = global.require;