所以,我有一个HTA,当我试图创建或打开一个文本文件来记录时,我遇到了问题。
它在Windows 7 64位和IE 11上运行良好。当我在Windows 10 64bit上尝试时,我得到以下错误:
无法完成操作,由于错误8070000c.
错误所在行在下面:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile('log\log.txt',2,true); // <-- Error is on this line!
好像是权限错误。我已经找了大约两个小时了,还是不知道是什么原因/解决办法。
同样,它在Windows 7上运行良好,但在Windows 10上不行。
相同的HTA如下:
<!-- saved from url=(0014)about:internet -->
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<HTA:Application Border='yes' ContextMenu='no' Caption='no' Scroll='auto' Singleinstance='yes' Windowstate='maximize' application='yes'>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<meta http-equiv='X-UA-Compatible' content='IE=9' />
<title>KIOSK</title>
<link rel='stylesheet' href='css/style.css'>
<link rel='stylesheet' href='https//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css'>
<script type='text/javascript' src='jQuery/jquery-3.1.0.js'></script>
<script type='text/javascript'>
// Log File.
function writeToLog(dStamp, msg) {
var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('\'));
dir = dir + '\log\log.txt';
// alert(dir);
//Create FSO Object
var fso = new ActiveXObject("Scripting.FileSystemObject");
//First, make sure the LOG exists
if (!fso.fileExists("log\log.txt")) {
//alert("FILE DONT EXIST!");
try {
var f = fso.OpenTextFile(dir,2,true);
f.close();
} catch(e) {
alert("The following error was found: " + e);
return;
}
} else {
dtTime = new Date();
//This function was added because if the log file gets over a certain size, I want to rename it and make a new one
var f = fso.OpenTextFile(dir,1,false);
var fSize = f.Size;
if (fSize > 1000000) { //If file is larger than 1Mb, rename and create new.
fileExists = true;
fileNumber = 0;
newFileName = f + "-" + (dtTime.getMonth() + 1) + "-" + dtTime.getDate() + "-" + dtTime.getFullYear();
while (fileExists) {
if (fso.fileExists(newFileName)) {
fileNumber++;
newFileName = f + "-" + (dtTime.getMonth() + 1) + "-" + dtTime.getDate() + "-" + dtTime.getFullYear() + "_" + fileNumber;
fileExists = true;
} else {
fileExists = false;
}
}
fso.MoveFile(f, newFileName);
//And create my new one
f = fso.OpenTextFile(dir,2,true);
f.close();
}
}
//Now, open the log file
var f = fso.OpenTextFile(dir, 8);
//Now, write to the log file
//First, check to see if we want a date stamp before this message
if (dStamp == true) {
dtTime = new Date();
nLine = dtTime + "t" + msg;
} else {
nLine = "t" + msg;
}
f.WriteLine(nLine);
//Finally, close the log file... until next time...
f.close();
}
</script>
<body ondragstart='return false' style='text-align:center;'>
<input type="button" onclick="writeToLog(true,'Test Log HTA');" value="Test Log" />
</body>
</html>
看起来我有一些不正确的值被传递给OpenTextFile,这基本上是导致我所有的问题。更正和"工作"的脚本如下,如果有人需要它。:)
// Log File.
function writeToLog(dStamp, msg) {
var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('\'));
dir = dir + '\log\log.txt';
// alert(dir);
//Create FSO Object
var fso = new ActiveXObject("Scripting.FileSystemObject");
//First, make sure the LOG exists
if (!fso.fileExists(dir)) {
//alert("FILE DONT EXIST!");
try {
var f = fso.CreateTextFile(dir, true);
f.close();
} catch(e) {
alert("The following error was found: " + e);
return;
}
} else {
dtTime = new Date();
//This function was added because if the log file gets over a certain size, I want to rename it and make a new one
var f = fso.OpenTextFile(dir,1,true,0);
var fSize = f.Size;
if (fSize > 1000000) { //If file is larger than 1Mb, rename and create new.
fileExists = true;
fileNumber = 0;
newFileName = f + "-" + (dtTime.getMonth() + 1) + "-" + dtTime.getDate() + "-" + dtTime.getFullYear();
while (fileExists) {
if (fso.fileExists(newFileName)) {
fileNumber++;
newFileName = f + "-" + (dtTime.getMonth() + 1) + "-" + dtTime.getDate() + "-" + dtTime.getFullYear() + "_" + fileNumber;
fileExists = true;
} else {
fileExists = false;
}
}
fso.MoveFile(f, newFileName);
//And create my new one
f = fso.CreateTextFile(dir, true);
f.close();
}
}
//Now, open the log file
var f = fso.OpenTextFile(dir,8,true,0);
//Now, write to the log file
//First, check to see if we want a date stamp before this message
if (dStamp == true) {
dtTime = new Date();
nLine = dtTime + "t" + msg;
} else {
nLine = "t" + msg;
}
f.WriteLine(nLine);
//Finally, close the log file... until next time...
f.close();
}