welsonjs/lib/file.js

216 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
// file.js
2020-06-28 14:22:57 +00:00
//
// Common routines. Defines LIB object which contains the API, as well as
2020-06-28 14:38:30 +00:00
// a global console.log function.
2020-06-28 14:22:57 +00:00
//
/////////////////////////////////////////////////////////////////////////////////
2020-06-28 14:22:57 +00:00
var LIB = require('lib/std');
2020-11-18 08:46:26 +00:00
var FileObject = function() {
this.interfaces = null;
this.interface = null;
this.filename = null;
this.charset = "utf-8";
this.isExists = false;
this.isFile = false;
this.isDirectory = false;
2020-06-28 14:22:57 +00:00
2020-11-18 08:46:26 +00:00
this.setInterface = function(interfaceName) {
this.interface = this.interfaces[interfaceName];
return this;
};
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-18 08:46:26 +00:00
this.create = function() {
this.interfaces = {
fso: CreateObject("Scripting.FileSystemObject"),
ado: CreateObject("ADODB.Stream")
};
this.setInterface("fso");
return this;
};
this.exists = function() {
try {
if (this.interface.FileExists(this.filename)) {
this.isExists = true;
this.isFile = true;
} else if (this.interface.folderExists(this.filename)) {
this.isExists = true;
this.isDirectory = true;
}
} catch (e) {
console.error("FileObject.exists() -> " + e.message);
}
return this.isExists;
};
this.open = function(filename) {
this.filename = filename;
if (!this.exists()) {
console.warn("FileObject.open() -> The file does not exists.");
}
return this;
};
this.create();
};
2020-06-28 14:22:57 +00:00
exports.fileExists = function(FN) {
2020-11-18 08:46:26 +00:00
return (new FileObject()).open(FN).exists();
};
exports.folderExists = function(FN) {
return (new FileObject()).open(FN).exists();
2020-06-28 14:22:57 +00:00
};
/////////////////////////////////////////////////////////////////////////////////
2020-11-18 08:46:26 +00:00
// Private APIs / Utility functions
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-11-18 08:46:26 +00:00
exports.VERSIONINFO = "File Lib (file.js) version 0.2";
exports.global = global;
exports.require = global.require;
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// exports.fileGet
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
exports.fileGet = function(FN) {
2020-06-28 15:12:12 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var file = FSO.GetFile(FN);
FSO = null;
return file;
2020-06-28 14:22:57 +00:00
};
/////////////////////////////////////////////////////////////////////////////////
// exports.readFile
2020-06-28 14:22:57 +00:00
// Read the conents of the pass filename and return as a string
/////////////////////////////////////////////////////////////////////////////////
exports.readFile = function(FN, charset) {
2020-06-28 15:12:12 +00:00
if(typeof(charset) === "undefined") {
var FSO = CreateObject("Scripting.FileSystemObject");
var T = null;
try {
var TS = FSO.OpenTextFile(FN, 1);
if (TS.AtEndOfStream) return "";
T = TS.ReadAll();
TS.Close();
TS = null;
} catch (e) {
console.log("ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
}
FSO = null;
return T;
} else {
2020-11-18 06:46:34 +00:00
try {
var fsT = CreateObject("ADODB.Stream");
var T = null;
fsT.CharSet = charset;
fsT.Open();
fsT.LoadFromFile(FN);
T = fsT.ReadText();
fsT = null;
return T;
} catch (e) {
console.error("readFile -> " + e.message);
}
2020-06-28 15:12:12 +00:00
}
2020-06-28 14:22:57 +00:00
};
/////////////////////////////////////////////////////////////////////////////////
// exports.writeFile
2020-06-28 14:22:57 +00:00
// Write the passed content to named disk file
/////////////////////////////////////////////////////////////////////////////////
exports.writeFile = function(FN, content, charset) {
2020-08-04 10:38:21 +00:00
var Stream_No_UTF8_BOM = function(objStream) {
var _objStream = CreateObject("ADODB.Stream");
_objStream.Type = 1;
_objStream.Mode = 3;
_objStream.Open();
objStream.Position = 3;
objStream.CopyTo(_objStream);
objStream.Flush();
objStream.Close();
return _objStream;
};
2020-06-28 15:12:12 +00:00
var ok;
2020-08-04 10:38:21 +00:00
2020-06-28 15:12:12 +00:00
if (charset) {
console.log("WRITE TO DISK USING ADODB.Stream CHARSET " + charset);
try {
var fsT = CreateObject("ADODB.Stream");
fsT.Type = 2; // save as text/string data.
fsT.Charset = charset; // Specify charset For the source text data.
fsT.Open();
fsT.WriteText(content);
2020-08-04 10:38:21 +00:00
if (charset == "utf-8") {
Stream_No_UTF8_BOM(fsT).SaveToFile(FN, 2); // save as binary to disk
} else {
fsT.SaveToFile(FN, 2); // save as binary to disk
}
2020-06-28 15:12:12 +00:00
ok = true;
} catch (e) {
console.log("ADODB.Stream: ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
}
} else {
console.log("WRITE TO DISK USING OpenTextFile CHARSET ascii");
var FSO = CreateObject("Scripting.FileSystemObject");
try {
var TS = FSO.OpenTextFile(FN, 2, true, 0); // ascii
TS.Write(content);
TS.Close();
TS = null;
ok = true;
} catch (e) {
console.log("OpenTextFile: ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
}
FSO = null;
2020-06-28 14:22:57 +00:00
}
2020-06-28 15:12:12 +00:00
return ok;
2020-06-28 14:22:57 +00:00
};
/////////////////////////////////////////////////////////////////////////////////
// exports.moveFile
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
exports.moveFile = function(FROM, TO) {
2020-06-28 15:12:12 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.MoveFile(FROM, TO);
FSO = null;
return res;
2020-06-28 14:22:57 +00:00
};
/////////////////////////////////////////////////////////////////////////////////
// exports.createFolder
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
exports.createFolder = function(FN) {
2020-06-28 15:12:12 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.CreateFolder(FN);
FSO = null;
return res;
2020-06-28 14:22:57 +00:00
};
2020-06-28 19:04:10 +00:00
/////////////////////////////////////////////////////////////////////////////////
// exports.deleteFile
2020-06-28 19:04:10 +00:00
/////////////////////////////////////////////////////////////////////////////////
exports.deleteFile = function(FN) {
2020-06-28 19:04:10 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.DeleteFile(FN);
FSO = null;
return res;
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////