HTA错误:由于错误8070000c无法完成操作

时间:2016-09-28 20:01:26

标签: javascript hta

所以,我有一个HTA,我在尝试创建或打开要记录的文本文件时遇到问题。

在IE 11上的Windows 7 64位上运行正常。 当我在Windows 10 64bit上尝试它时,我收到以下错误:

由于错误8070000c无法完成操作。

错误所在的行如下:

   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var f = fso.OpenTextFile('log\\log.txt',2,true); // <-- Error is on this line!

似乎是一个权限错误。我搜索了大约2个小时,无法找出原因/解决方案。

同样,它在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>

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

看起来我将一些不正确的值传递给OpenTextFile,这基本上导致了我的所有问题。 纠正的&#34;工作&#34;如果有人需要它,脚本在下面。 :)

// 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();
}