如何在VBScript中将文本框写入.txt?

时间:2017-12-01 15:14:59

标签: vbscript hta

我需要写一个文本区域(框)值到.txt我在写入" input.txt"时得到权限被拒绝 从文本框中输入input.txt时,我被拒绝了。我使用下面的建议进行了编辑,并使用进程资源管理器杀死了input.txt的句柄。

使用下面的代码我现在可以将文本框文本保存到文本文件input.txt。 我还想出了如何调用批处理文件将该文本更改为加密形式,然后我添加了一个刷新按钮,将更改后的文本打开到第二个文本框作为最终输出结果,使文本现在能够成为密码在通过批处理文件encrypter.bat进行加密之后,在保存在input.txt中并在文件SR-Encrypted.txt中的第二个文本框中打开后加密,然后使用我添加的刷新按钮刷新页面..

谢谢agnar!

    <html>
    <script language="vbscript">
option explicit
    Const ForWriting = 2

    Dim objFSO, objFile, strFileName, objshell
    strFileName = "input.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objShell = CreateObject("WScript.Shell")

Sub Submitarea
  Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
  objfile.Write TextFile.Value
  objFile.Close
  MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub
</script>   
</html>
  </head>
    <title>Example</title>
    <script language="VBScript"> 
      Sub test
     set oFSO=CreateObject("Scripting.FileSystemObject")
     set oFile=oFSO.OpenTextFile("SR-Encrypted.txt",1)
     text=oFile.ReadAll
     document.all.ScriptArea.value=text
     Set objFile = nothing
     oFile.Close
      End Sub 
    </script>
  </head>

  <script language="vbscript">
Option Explicit

' This is the Sub that opens external files and reads in the contents.
' In this way, you can have separate files for data and libraries of functions
Sub Include(yourFile)
  Dim oFSO, oFileBeingReadIn   ' define Objects
  Dim sFileContents            ' define Strings

  Set oFSO = CreateObject("Scripting.FileSystemObject")
  Set oFileBeingReadIn = oFSO.OpenTextFile("try.vbs", 1)
  sFileContents = oFileBeingReadIn.ReadAll
  oFileBeingReadIn.Close
  ExecuteGlobal sFileContents
End Sub

' Here we call the Include Sub, then pass it the name of the file we want items from
Include "mySubLib"

</script>
  <body>
    <button onClick="test()">Refresh Message Encrypter-Decrypter</button>
  </body>
</html>
    </head>
 </html>
<body>
    <h1>Write File</h1>
    <p>How to Write to a File.</p>

    <textarea name="TextFile" id="TextFile" rows="20" cols="50"></textarea>

    <input type="button" value="Submit" onclick="Submitarea">

</body>
    <body>

    </body>
</html>

2 个答案:

答案 0 :(得分:1)

“权限被拒绝”错误最有可能发生,因为该文件已被打开。只有在您真正要写入文件时才打开文件,并在完成写入后立即关闭文件。此外,如果要将文本区域的内容写入文件,则需要实际编写文本区域的内容,而不是字符串“Txtarea”。

改变这个:

Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
Set objShell = CreateObject("WScript.Shell")

Sub Submitarea
  sTxtarea = TextFile.Value
  objfile.Write "Txtarea" & vbCrLf
  MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub

到此:

Set objShell = CreateObject("WScript.Shell")

Sub Submitarea
  Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
  objfile.Write TextFile.Value
  objFile.Close
  MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub

问题就会消失。

如果要附加到输出文件而不是替换其内容,请将OpenTextFile()方法的第二个参数从2更改为8。

答案 1 :(得分:0)

@ansgar wiechers编辑和建议都是正确的,并导致脚本正常运行。请参阅上面的代码以准确解决原始问题。 权限被拒绝是由input.txt上的打开句柄引起的。使用进程树我杀死了句柄,文件正常运行。 ansgar wiechers发布的建议修改了代码,这是另一个单独的问题,但是正确的代码和信息是在帖子中。谢谢。

相关问题