1

i need to write a text area (box) value to .txt i am getting permissions denied when writing to "input.txt" i got got permission denied when writing input.txt from the text box. i edited using suggestions found below and also killed handle for input.txt using process explorer.

using the code below i can now save textbox text to text file input.txt. i also figured out how to call upon a batch file to change that text into a cyphered form, and then i added a refresh button to open that changed text into a second textbox as a final output result which leaves the text now capable of being password encrypted after being saved in input.txt and opened in a second textbox from file SR-Encrypted.txt after being cyphered via batch file encrypter.bat then by refreshing the page using the refresh button i added..

thank you 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>
Steed
  • 61
  • 9
  • By searching SO!! type in "VBScript write to file" in the search box. https://stackoverflow.com/questions/34045891/how-to-create-text-file-and-write-to-it-in-vbscript – Sorceri Dec 01 '17 at 15:20
  • @Sorceri That doesn't solve the problem at hand. The OP already does write to the file, but not the data he actually wants to write. The file handling leaves room for improvement too. – Ansgar Wiechers Dec 01 '17 at 15:27
  • with the help of @AnsgarWiechers the text in the box should be being saved to "input.txt" which is great. but the permission denied still occurs even when input.txt is not open. – Steed Dec 01 '17 at 15:46
  • Check if something has an open handle to the file (with [Process Explorer](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer) or [`handle`](https://learn.microsoft.com/en-us/sysinternals/downloads/handle)). Also check the actual permissions of the file. – Ansgar Wiechers Dec 01 '17 at 16:09
  • process explorer does not show any open notepads .txt now i get variable undefined objshell – Steed Dec 02 '17 at 01:34
  • You need to look for handles (*Find -> Find Handle or DLL...*), not processes. Also, where *exactly* are you getting that new error, and what *exactly* does it say? Please do not paraphrase. [Edit] your question and show the complete, unaltered error message. – Ansgar Wiechers Dec 02 '17 at 05:54
  • @AnsgarWiechers everything is running great! u nailed it! is there a basic script i can add to be able to kill the input.txt handle in the future incase it gets left open?:: also i gave you an upvote! – Steed Dec 02 '17 at 07:02
  • *also i gave you an upvote!* No, you didn't. – Ansgar Wiechers Dec 03 '17 at 00:48
  • sorry @AnsgarWiechers it says i need a reputation of 15 for my upvotes to appear. dont worry once i make 15 i will come back and upvote you! thank you so much! – Steed Dec 03 '17 at 14:16
  • sorry i am new here haven't figured out how the site works but i think i am getting the hang of how the rating system and stuff goes. next time should i leave the original code? or is it better to update the old code to the final answer's code? – Steed Dec 03 '17 at 23:00

2 Answers2

1

The "permission denied" errors most likely occur because the file had already been opened. Open the file only when you actually want to write to it, and close it right away after you finished writing. Also, if you want to write the content of the text area to the file you need to actually write the content of the text area, not the string "Txtarea".

Change this:

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

to this:

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

and the problem will disappear.

Change the second parameter of the OpenTextFile() method from 2 to 8 if you want to append to the output file rather than replace its content.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

@ansgar wiechers edits and suggestions were all correct and resulted in the scripts functioning properly. see code above in question for accurate solution to the original question. permissions denied were caused by an open handle on input.txt. using process tree i killed the handle and the file functions properly. the suggestion posted by ansgar wiechers fixed the code which was another separate issue but the correct codes and information reguarding the original question are in the post. thank you.

Steed
  • 61
  • 9