Update file.js

This commit is contained in:
Namhyeon Go 2022-01-27 12:42:55 +09:00 committed by GitHub
parent 98b8538fb9
commit ce4026b59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,14 @@
//////////////////////////////////////////////////////////////////////////////////
//
// file.js
// file.js
//
// Common routines. Defines LIB object which contains the API, as well as
// a global console.log function.
// Common routines. Defines LIB object which contains the API, as well as
// a global console.log function.
//
/////////////////////////////////////////////////////////////////////////////////
var LIB = require('lib/std');
var LIB = require("lib/std");
var SHELL = require("lib/shell");
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
@ -18,43 +19,43 @@ exports.global = global;
exports.require = global.require;
/////////////////////////////////////////////////////////////////////////////////
// exports.fileExists
// fileExists
/////////////////////////////////////////////////////////////////////////////////
exports.fileExists = function(FN) {
function fileExists(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FileExists(FN);
FSO = null;
return exists;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.folderExists
// folderExists
/////////////////////////////////////////////////////////////////////////////////
exports.folderExists = function(FN) {
function folderExists(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FolderExists(FN);
FSO = null;
return exists;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.fileGet
// fileGet
/////////////////////////////////////////////////////////////////////////////////
exports.fileGet = function(FN) {
function fileGet(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var file = FSO.GetFile(FN);
FSO = null;
return file;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.readFile
// Read the conents of the pass filename and return as a string
// readFile
// Read the conents of the pass filename and return as a string
/////////////////////////////////////////////////////////////////////////////////
exports.readFile = function(FN, charset) {
function readFile(FN, charset) {
if(typeof(charset) === "undefined") {
var FSO = CreateObject("Scripting.FileSystemObject");
var T = null;
@ -78,14 +79,14 @@ exports.readFile = function(FN, charset) {
fsT = null;
return T;
}
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.writeFile
// Write the passed content to named disk file
// writeFile
// Write the passed content to named disk file
/////////////////////////////////////////////////////////////////////////////////
exports.writeFile = function(FN, content, charset) {
function writeFile(FN, content, charset) {
var Stream_No_UTF8_BOM = function(objStream) {
var _objStream = CreateObject("ADODB.Stream");
_objStream.Type = 1;
@ -94,46 +95,59 @@ exports.writeFile = function(FN, content, charset) {
objStream.Position = 3;
objStream.CopyTo(_objStream);
objStream.Flush();
objStream.Close();
//objStream.Close();
return _objStream;
};
var ok;
var ok = false;
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);
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
while (!ok) {
// [lib/file] Can not overwrite a file with ADODB.Stream SaveToFile() #32
SHELL.exec(["del", FN]);
// ascii:Scripting.FileSystemObject, unicode:ADODB.Stream
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);
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
}
fsT.Close();
fsT = null;
ok = true;
} catch (e) {
console.log("ADODB.Stream: ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
}
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;
}
} 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;
}
return ok;
};
exports.writeBinaryFile = function(FN, DATA) {
return ok;
}
/////////////////////////////////////////////////////////////////////////////////
// writeBinaryFile
/////////////////////////////////////////////////////////////////////////////////
function writeBinaryFile(FN, DATA) {
var adTypeText = 1;
var adSaveCreateOverWrite = 2;
var BinaryStream = CreateObject("ADODB.Stream");
@ -142,58 +156,72 @@ exports.writeBinaryFile = function(FN, DATA) {
BinaryStream.Write(DATA);
BinaryStream.SaveToFile(FN, adSaveCreateOverWrite);
BinaryStream.Close();
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.moveFile
// moveFile
/////////////////////////////////////////////////////////////////////////////////
exports.moveFile = function(FROM, TO) {
function moveFile(FROM, TO) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.MoveFile(FROM, TO);
FSO = null;
return result;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.createFolder
// createFolder
/////////////////////////////////////////////////////////////////////////////////
exports.createFolder = function(FN) {
function createFolder(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.CreateFolder(FN);
FSO = null;
return result;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.deleteFolder
// deleteFolder
/////////////////////////////////////////////////////////////////////////////////
exports.deleteFolder = function(FN) {
function deleteFolder(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.DeleteFolder(FN);
FSO = null;
return result;
};
}
/////////////////////////////////////////////////////////////////////////////////
// exports.deleteFile
// deleteFile
/////////////////////////////////////////////////////////////////////////////////
exports.deleteFile = function(FN) {
function deleteFile(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.DeleteFile(FN);
FSO = null;
return result;
};
}
/////////////////////////////////////////////////////////////////////////////////
// includeFile
/////////////////////////////////////////////////////////////////////////////////
exports.includeFile = function(FN) {
function includeFile(FN) {
var fso = CreateObject("Scripting.FileSystemObject");
var fileStream = fso.openTextFile(FN);
var fileData = fileStream.readAll();
fileStream.Close();
eval(fileData);
};
}
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;